Skip to content

lib: Attempted to fix issue #57780, child_process.js #57956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ const {
SymbolDispose,
} = primordials;


const {
convertToValidSignal,
getSystemErrorName,
kEmptyObject,
promisify,
} = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
const { isWindows } = require('internal/util/platform');
let debug = require('internal/util/debuglog').debuglog(
'child_process',
(fn) => {
Expand Down Expand Up @@ -88,7 +90,7 @@ const child_process = require('internal/child_process');
const {
getValidStdio,
setupChannel,
ChildProcess,
ChildProcess: OriginalChildProcess,
stdioStringToArray,
} = child_process;

Expand All @@ -97,6 +99,65 @@ const MAX_BUFFER = 1024 * 1024;
const isZOS = process.platform === 'os390';
let addAbortListener;

// Enhanced ChildProcess class with Windows encoding support
class ChildProcess extends OriginalChildProcess {
Comment on lines +102 to +103
Copy link
Contributor

@aduh95 aduh95 Apr 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introducing a class just to "highjack" spawnings of cmd.exe on Windows doesn't feel right – it's unclear whether we'd want to fix #57780 in the first place tbh

constructor(options) {
super(options);
this._stdinEncodingReady = false;
this._stdinQueue = [];
this._windowsShellEncoding = null;
}

_handleEncodingSync() {
const handleReady = () => {
this._stdinEncodingReady = true;
this._stdinQueue.forEach(({ data, encoding, callback }) => {
this._writeStdin(data, encoding, callback);
});
this._stdinQueue = [];
};

// Force UTF-8 mode immediately
this._writeStdin('chcp 65001 > nul\r\n', null, () => {
this._windowsShellEncoding = 'utf8';
handleReady();
});
}

_writeStdin(data, encoding, callback) {
if (!this._stdinEncodingReady && this._windowsShellEncoding) {
this._stdinQueue.push({ data, encoding, callback });
return true;
}

if (encoding && this._windowsShellEncoding === 'utf8') {
try {
if (typeof data === 'string') {
data = Buffer.from(data, encoding);
} else if (Buffer.isBuffer(data)) {
data = Buffer.from(data.toString(), encoding);
}
} catch (err) {
process.nextTick(() => this.emit('error', err));
return false;
}
}

return this.stdin.write(data, callback);
}

spawn(options) {
if (isWindows &&
options.file.toLowerCase().endsWith('cmd.exe') &&
process.env.NODE_CHILD_PROCESS_NO_ENCODING_SYNC !== '1') {
this._windowsShellEncoding = 'gbk';
this._handleEncodingSync();
}

return super.spawn(options);
}
}

/**
* Spawns a new Node.js process + fork.
* @param {string|URL} modulePath
Expand Down Expand Up @@ -1020,3 +1081,4 @@ module.exports = {
spawn,
spawnSync,
};

30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"iconv-lite": "^0.6.3"
}
}
Comment on lines +1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can imagine, we're not going to want to have this file

31 changes: 31 additions & 0 deletions test-fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Testing for issue 57780, works on MacOS and Windows,link here:
// https://github.com/nodejs/node/issues/57780

const child_process = require('child_process');
const iconv = require('iconv-lite'); // Still needed for Windows testing

const isWindows = process.platform === 'win32';

async function amain() {
if (isWindows) {
// Windows: Test cmd.exe with UTF-8 + GBK
const cp = child_process.spawn('cmd', [], {
stdio: ['pipe', 'inherit', 'inherit'],
env: { ...process.env, CHCP: '65001' } // Force UTF-8
});

const utf8Cmd = 'echo utf8中文测试\r\n';
const gbkCmd = iconv.encode('echo gbk中文测试\r\n', 'gbk');

cp.stdin.write(utf8Cmd);
cp.stdin.write(gbkCmd);
cp.stdin.end();
} else {
// macOS: Test UTF-8 in bash
Comment on lines +23 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many more OSes which are neither Windows nor macOS

console.log('Testing UTF-8 on macOS:');
const bash = child_process.spawn('bash', ['-c', 'echo "macOS中文测试"']);
bash.stdout.pipe(process.stdout);
}
}

amain().catch(console.error);