Skip to content

Commit

Permalink
Ensure extension works with non-latin alphabets (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderRonde committed Feb 24, 2024
1 parent e3781ec commit 915dbc6
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions server/src/lib/proc.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
import type {
ChildProcessWithoutNullStreams,
SpawnOptionsWithoutStdio,
ChildProcess,
SpawnSyncOptionsWithStringEncoding,
} from 'child_process';
import { processNotification } from './notificationChannels';
import type { _Connection } from 'vscode-languageserver';
import { spawn } from 'child_process';
import { exec, spawn } from 'child_process';

export class ProcessSpawner {
public constructor(private readonly _connection: _Connection) {}

private async _getCodePage(): Promise<number | null> {
return new Promise<number | null>((resolve) => {
// Get code page, which is sort of equivalent to charset/encoding
const chcpProc = spawn('chcp');
let stdout = '';
chcpProc.stdout.on('data', (chunk: Buffer | string) => {
stdout += chunk.toString();
});
chcpProc.on('exit', (code) => {
if (code === 0) {
const match = /:\s*(\d+)/.exec(stdout);
if (match) {
resolve(Number(match[1]));
return;
}
}
resolve(null);
});
chcpProc.on('error', () => {
resolve(null);
});
});
}

/**
* Spawns a process in a way that guarantees that it will be killed
* after given timeout. If VSCode is killed in the meantime, we still
Expand All @@ -18,9 +42,27 @@ export class ProcessSpawner {
binStr: string,
args: string[],
timeout: number,
options?: SpawnOptionsWithoutStdio
): Promise<ChildProcessWithoutNullStreams> {
const proc = spawn(binStr, args, options);
options: SpawnSyncOptionsWithStringEncoding
): Promise<ChildProcess> {
const proc = await (async () => {
if (process.platform === 'win32') {
const codePage = await this._getCodePage();
if (codePage && codePage !== 850) {
// Set codepage to 850 aka Latin 1
return exec(
`@chcp 850 >nul & cmd /d/s/c ${binStr} ${args.join(
' '
)}`,
{
cwd: options.cwd,
encoding: 'utf-8',
}
);
}
}
return spawn(binStr, args, options);
})();

if (proc.pid) {
await this._connection.sendNotification(processNotification, {
pid: proc.pid,
Expand Down

0 comments on commit 915dbc6

Please sign in to comment.