Skip to content

Commit ab163b5

Browse files
committed
Preserve environment when spawning processes
1 parent fa6f928 commit ab163b5

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

packages/protocol/src/common/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,13 @@ export const isProxy = (value: any): value is ServerProxy => {
301301
export const isPromise = (value: any): value is Promise<any> => {
302302
return typeof value.then === "function" && typeof value.catch === "function";
303303
};
304+
305+
/**
306+
* When spawning VS Code tries to preserve the environment but since it's in
307+
* the browser, it doesn't work.
308+
*/
309+
export const preserveEnv = (options?: { env?: NodeJS.ProcessEnv } | null): void => {
310+
if (options && options.env) {
311+
options.env = { ...process.env, ...options.env };
312+
}
313+
};

packages/protocol/src/node/modules/child_process.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as cp from "child_process";
22
import { ServerProxy } from "../../common/proxy";
3+
import { preserveEnv } from "../../common/util";
34
import { WritableProxy, ReadableProxy } from "./stream";
45

56
export type ForkProvider = (modulePath: string, args?: string[], options?: cp.ForkOptions) => cp.ChildProcess;
@@ -74,14 +75,20 @@ export class ChildProcessModuleProxy {
7475
options?: { encoding?: string | null } & cp.ExecOptions | null,
7576
callback?: ((error: cp.ExecException | null, stdin: string | Buffer, stdout: string | Buffer) => void),
7677
): Promise<ChildProcessProxies> {
78+
preserveEnv(options);
79+
7780
return this.returnProxies(cp.exec(command, options, callback));
7881
}
7982

8083
public async fork(modulePath: string, args?: string[], options?: cp.ForkOptions): Promise<ChildProcessProxies> {
84+
preserveEnv(options);
85+
8186
return this.returnProxies((this.forkProvider || cp.fork)(modulePath, args, options));
8287
}
8388

8489
public async spawn(command: string, args?: string[], options?: cp.SpawnOptions): Promise<ChildProcessProxies> {
90+
preserveEnv(options);
91+
8592
return this.returnProxies(cp.spawn(command, args, options));
8693
}
8794

packages/protocol/src/node/modules/node-pty.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { EventEmitter } from "events";
33
import * as pty from "node-pty";
44
import { ServerProxy } from "../../common/proxy";
5+
import { preserveEnv } from "../../common/util";
56

67
/**
78
* Server-side IPty proxy.
@@ -67,6 +68,8 @@ export class NodePtyProcessProxy implements ServerProxy {
6768
*/
6869
export class NodePtyModuleProxy {
6970
public async spawn(file: string, args: string[] | string, options: pty.IPtyForkOptions): Promise<NodePtyProcessProxy> {
71+
preserveEnv(options);
72+
7073
return new NodePtyProcessProxy(pty.spawn(file, args, options));
7174
}
7275
}

0 commit comments

Comments
 (0)