Skip to content

Commit 3f51dcf

Browse files
code-asherkylecarbs
authored andcommitted
Get forked watcher & searcher working
1 parent 3222297 commit 3f51dcf

File tree

6 files changed

+40
-36
lines changed

6 files changed

+40
-36
lines changed

packages/protocol/src/browser/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class Client {
217217
clientMsg.setNewSession(newSess);
218218
this.connection.send(clientMsg.serializeBinary());
219219

220-
const serverProc = new ServerProcess(this.connection, id, options ? options.tty !== undefined : false);
220+
const serverProc = new ServerProcess(this.connection, id, options ? options.tty !== undefined : false, isBootstrapFork);
221221
serverProc.stdin.on("close", () => {
222222
const c = new CloseSessionInputMessage();
223223
c.setId(id);

packages/protocol/src/browser/command.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,36 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess {
3838
public readonly stdin = new stream.Writable();
3939
public readonly stdout = new stream.Readable({ read: (): boolean => true });
4040
public readonly stderr = new stream.Readable({ read: (): boolean => true });
41-
public pid: number | undefined;
4241

42+
private _pid: number | undefined;
4343
private _killed: boolean = false;
44+
private _connected: boolean = false;
4445

4546
public constructor(
4647
private readonly connection: ReadWriteConnection,
4748
private readonly id: number,
4849
private readonly hasTty: boolean = false,
50+
private readonly ipc: boolean = false,
4951
) {
5052
super();
5153
if (!this.hasTty) {
5254
delete this.resize;
5355
}
5456
}
5557

58+
public get pid(): number | undefined {
59+
return this._pid;
60+
}
61+
62+
public set pid(pid: number | undefined) {
63+
this._pid = pid;
64+
this._connected = true;
65+
}
66+
67+
public get connected(): boolean {
68+
return this._connected;
69+
}
70+
5671
public get killed(): boolean {
5772
return this._killed;
5873
}
@@ -68,9 +83,10 @@ export class ServerProcess extends events.EventEmitter implements ChildProcess {
6883
this.connection.send(client.serializeBinary());
6984

7085
this._killed = true;
86+
this._connected = false;
7187
}
7288

73-
public send(message: string | Uint8Array | any, ipc: boolean = false): void {
89+
public send(message: string | Uint8Array | any, ipc: boolean = this.ipc): void {
7490
const send = new WriteToSessionMessage();
7591
send.setId(this.id);
7692
send.setSource(ipc ? WriteToSessionMessage.Source.IPC : WriteToSessionMessage.Source.STDIN);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class CP {
4646

4747
public fork = (modulePath: string, args?: ReadonlyArray<string> | cp.ForkOptions, options?: cp.ForkOptions): cp.ChildProcess => {
4848
//@ts-ignore
49-
return this.client.fork(options && options.env && options.env.AMD_ENTRYPOINT || modulePath, args, options);
49+
return this.client.bootstrapFork(options && options.env && options.env.AMD_ENTRYPOINT || modulePath);
5050
}
5151

5252
public spawn = (command: string, args?: ReadonlyArray<string> | cp.SpawnOptions, options?: cp.SpawnOptions): cp.ChildProcess => {

packages/protocol/src/node/command.ts

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const handleNewSession = (connection: SendableConnection, newSession: New
6363
stdin: proc.stdin,
6464
stderr: proc.stderr,
6565
stdout: proc.stdout,
66+
stdio: proc.stdio,
6667
on: (...args: any[]) => (<any>proc.on)(...args),
6768
write: (d) => proc.stdin.write(d),
6869
kill: (s) => proc.kill(s || "SIGTERM"),

packages/server/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"bin": "./out/cli.js",
55
"files": [],
66
"scripts": {
7-
"start": "NODE_ENV=development ts-node -r tsconfig-paths/register src/cli.ts",
7+
"start": "ts-node -r tsconfig-paths/register src/cli.ts",
88
"build:webpack": "rm -rf ./out && export CLI=true && ../../node_modules/.bin/webpack --config ./webpack.config.js",
99
"build:nexe": "node scripts/nexe.js",
1010
"build:bootstrap-fork": "cd ../vscode && npm run build:bootstrap-fork; cp ./bin/bootstrap-fork.js ../server/build/bootstrap-fork.js",

packages/server/src/vscode/bootstrapFork.ts

+18-31
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ import * as cp from "child_process";
22
import * as fs from "fs";
33
import * as net from "net";
44
import * as path from "path";
5-
import { logger, field } from "@coder/logger/src";
5+
import { Logger, logger, field } from "@coder/logger/src";
66

7-
declare var __non_webpack_require__: typeof require;
7+
const getChildLogger = (modulePath: string): Logger => {
8+
const basename = modulePath.split("/").pop()!;
9+
let i = 0;
10+
for (; i < basename.length; i++) {
11+
const character = basename.charAt(i);
12+
if (character === character.toUpperCase()) {
13+
break;
14+
}
15+
}
16+
17+
return logger.named(basename.substring(0, i));
18+
};
819

920
export const requireModule = (modulePath: string): void => {
1021
process.env.AMD_ENTRYPOINT = modulePath;
@@ -16,6 +27,7 @@ export const requireModule = (modulePath: string): void => {
1627
process.emit("message", JSON.parse(data.toString()), undefined);
1728
});
1829

30+
// tslint:disable-next-line no-any
1931
process.send = (message: any): void => {
2032
socket.write(JSON.stringify(message));
2133
};
@@ -28,22 +40,13 @@ export const requireModule = (modulePath: string): void => {
2840
/**
2941
* Uses the internal bootstrap-fork.js to load a module
3042
* @example
31-
* const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain", true);
43+
* const cp = forkModule("vs/code/electron-browser/sharedProcess/sharedProcessMain");
3244
* cp.stdout.on("data", (data) => console.log(data.toString("utf8")));
3345
* cp.stderr.on("data", (data) => console.log(data.toString("utf8")));
3446
* @param modulePath Path of the VS Code module to load.
35-
* @param stdio Whether to use stdio (spawn) or send/onMessage (fork).
3647
*/
37-
export const forkModule = (modulePath: string, stdio?: boolean): cp.ChildProcess => {
38-
const basename = modulePath.split("/").pop()!;
39-
let i = 0;
40-
for (; i < basename.length; i++) {
41-
const character = basename.charAt(i);
42-
if (character === character.toUpperCase()) {
43-
break;
44-
}
45-
}
46-
const childLogger = logger.named(basename.substring(0, i));
48+
export const forkModule = (modulePath: string): cp.ChildProcess => {
49+
const childLogger = getChildLogger(modulePath);
4750
childLogger.debug("Forking...", field("module", modulePath));
4851

4952
let proc: cp.ChildProcess | undefined;
@@ -53,27 +56,11 @@ export const forkModule = (modulePath: string, stdio?: boolean): cp.ChildProcess
5356
stdio: [null, null, null, "pipe"],
5457
};
5558
if (process.env.CLI === "true") {
56-
proc = stdio ? cp.spawn(process.execPath, args, options) : cp.fork(process.execPath, args, options);
59+
proc = cp.spawn(process.execPath, args, options);
5760
} else {
5861
proc = cp.spawn(process.execArgv[0], ["-r", "tsconfig-paths/register", process.argv[1], ...args], options);
5962
}
6063

61-
proc.stdout.on("data", (message) => {
62-
childLogger.debug("stdout", field("message", message.toString().trim()));
63-
});
64-
65-
proc.stderr.on("data", (message) => {
66-
childLogger.debug("stderr", field("message", message.toString().trim()));
67-
});
68-
69-
proc.stdin.on("data", (message) => {
70-
childLogger.debug("stdin", field("message", message.toString().trim()));
71-
});
72-
73-
proc.on("message", (message) => {
74-
childLogger.debug("message", field("message", message.toString().trim()));
75-
});
76-
7764
proc.on("exit", (exitCode) => {
7865
childLogger.debug(`Exited with ${exitCode}`);
7966
});

0 commit comments

Comments
 (0)