Skip to content

Commit

Permalink
fix: #2 - Specify encoding when executing exec.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jul 30, 2020
1 parent cd498bd commit e9280d1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/dprint-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ function execShell(
return new Promise<string>((resolve, reject) => {
let cancellationDisposable: vscode.Disposable | undefined;
try {
const process = exec(command, { cwd: vscode.workspace.rootPath }, (err, stdout, stderr) => {
const process = exec(command, {
cwd: vscode.workspace.rootPath,
encoding: "utf8",
}, (err, stdout, stderr) => {
if (err) {
cancellationDisposable?.dispose();
reject(stderr);
Expand Down

2 comments on commit e9280d1

@heroboy
Copy link

Choose a reason for hiding this comment

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

hello @dsherret , I think this is not correct on windows.
Consider this nodejs code:

const { exec } = require('child_process');
const { decode } = require('iconv-lite');
exec('abc', {
	cwd: __dirname,
	encoding: 'buffer'
}, function (e, stdout, stderr)
{
	if (e) console.log(e);
	console.log('stdout', decode(stdout, 'gbk'));
	console.log('stderr', decode(stderr, 'gbk'));
});

output is

Error: Command failed: abc
'abc' �����ڲ����ⲿ���Ҳ���ǿ����еij���
���������ļ���

    at ChildProcess.exithandler (child_process.js:302:12)
    at ChildProcess.emit (events.js:305:20)
    at maybeClose (internal/child_process.js:1028:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5) {
  killed: false,
  code: 1,
  signal: null,
  cmd: 'abc'
}
stdout
stderr 'abc' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

You will see the encoding is actually gbk on my machine.
Even you specify the encoding to utf-8, it just use utf-8 encoding to deocde the gbk buffer.
I think you should see more vscode source code to see how to do exec on windows.
Or you can try this this way on windows.
note: if you use chcp 65001 the output of cmd.exe will become English. But use dir to show some Chinese filename, the encoding is actually utf-8.

@dsherret
Copy link
Member Author

Choose a reason for hiding this comment

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

@heroboy thanks! I actually misread that SO post. I’m going to reopen the bug.

Please sign in to comment.