Skip to content

Commit d212bf7

Browse files
committed
simplify find port, remote hacky env variables
1 parent 85f70fd commit d212bf7

3 files changed

Lines changed: 33 additions & 51 deletions

File tree

src/vs/workbench/contrib/debug/node/debugAdapter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ export class ExecutableDebugAdapter extends StreamDebugAdapter {
178178
if (options.env) {
179179
env = objects.mixin(env, options.env);
180180
}
181-
delete env.VSCODE_PREVENT_FOREIGN_INSPECT;
182181

183182
if (command === 'node') {
184183
if (Array.isArray(args) && args.length > 0) {

src/vs/workbench/services/extensions/electron-browser/extensionHost.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as platform from 'vs/base/common/platform';
1616
import { URI } from 'vs/base/common/uri';
1717
import { IRemoteConsoleLog, log } from 'vs/base/common/console';
1818
import { logRemoteEntry } from 'vs/workbench/services/extensions/common/remoteConsoleUtil';
19-
import { findFreePort, randomPort } from 'vs/base/node/ports';
19+
import { findFreePort } from 'vs/base/node/ports';
2020
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
2121
import { PersistentProtocol } from 'vs/base/parts/ipc/common/ipc.net';
2222
import { generateRandomPipeName, NodeSocket } from 'vs/base/parts/ipc/node/ipc.net';
@@ -129,10 +129,10 @@ export class ExtensionHostProcessWorker implements IExtensionHostStarter {
129129
if (!this._messageProtocol) {
130130
this._messageProtocol = Promise.all([
131131
this._tryListenOnPipe(),
132-
!this._environmentService.args['disable-inspect'] ? this._tryFindDebugPort() : Promise.resolve(null)
132+
!this._environmentService.args['disable-inspect'] ? this._tryFindDebugPort() : 0
133133
]).then(data => {
134134
const pipeName = data[0];
135-
const portData = data[1];
135+
const portNumber = data[1];
136136

137137
const opts = {
138138
env: objects.mixin(objects.deepClone(process.env), {
@@ -153,16 +153,11 @@ export class ExtensionHostProcessWorker implements IExtensionHostStarter {
153153
silent: true
154154
};
155155

156-
if (portData && portData.actual) {
156+
if (portNumber !== 0) {
157157
opts.execArgv = [
158158
'--nolazy',
159-
(this._isExtensionDevDebugBrk ? '--inspect-brk=' : '--inspect=') + portData.actual
159+
(this._isExtensionDevDebugBrk ? '--inspect-brk=' : '--inspect=') + portNumber
160160
];
161-
if (!portData.expected) {
162-
// No one asked for 'inspect' or 'inspect-brk', only us. We add another
163-
// option such that the extension host can manipulate the execArgv array
164-
opts.env.VSCODE_PREVENT_FOREIGN_INSPECT = true;
165-
}
166161
}
167162

168163
const crashReporterOptions = undefined; // TODO@electron pass this in as options to the extension host after verifying this actually works
@@ -221,11 +216,11 @@ export class ExtensionHostProcessWorker implements IExtensionHostStarter {
221216
this._extensionHostProcess.on('exit', (code: number, signal: string) => this._onExtHostProcessExit(code, signal));
222217

223218
// Notify debugger that we are ready to attach to the process if we run a development extension
224-
if (portData) {
225-
if (this._isExtensionDevHost && portData.actual && this._isExtensionDevDebug && this._environmentService.debugExtensionHost.debugId) {
226-
this._extensionHostDebugService.attachSession(this._environmentService.debugExtensionHost.debugId, portData.actual);
219+
if (portNumber) {
220+
if (this._isExtensionDevHost && portNumber && this._isExtensionDevDebug && this._environmentService.debugExtensionHost.debugId) {
221+
this._extensionHostDebugService.attachSession(this._environmentService.debugExtensionHost.debugId, portNumber);
227222
}
228-
this._inspectPort = portData.actual;
223+
this._inspectPort = portNumber;
229224
this._onDidSetInspectPort.fire();
230225
}
231226

@@ -279,29 +274,31 @@ export class ExtensionHostProcessWorker implements IExtensionHostStarter {
279274
/**
280275
* Find a free port if extension host debugging is enabled.
281276
*/
282-
private _tryFindDebugPort(): Promise<{ expected: number; actual: number }> {
283-
let expected: number;
284-
let startPort = randomPort();
285-
if (typeof this._environmentService.debugExtensionHost.port === 'number') {
286-
startPort = expected = this._environmentService.debugExtensionHost.port;
277+
private async _tryFindDebugPort(): Promise<number> {
278+
279+
if (typeof this._environmentService.debugExtensionHost.port !== 'number') {
280+
return 0;
287281
}
288-
return new Promise(resolve => {
289-
return findFreePort(startPort, 10 /* try 10 ports */, 5000 /* try up to 5 seconds */).then(port => {
290-
if (!port) {
291-
console.warn('%c[Extension Host] %cCould not find a free port for debugging', 'color: blue', 'color:');
292-
} else {
293-
if (expected && port !== expected) {
294-
console.warn(`%c[Extension Host] %cProvided debugging port ${expected} is not free, using ${port} instead.`, 'color: blue', 'color:');
295-
}
296-
if (this._isExtensionDevDebugBrk) {
297-
console.warn(`%c[Extension Host] %cSTOPPED on first line for debugging on port ${port}`, 'color: blue', 'color:');
298-
} else {
299-
console.info(`%c[Extension Host] %cdebugger listening on port ${port}`, 'color: blue', 'color:');
300-
}
301-
}
302-
return resolve({ expected, actual: port });
303-
});
304-
});
282+
283+
const expected = this._environmentService.debugExtensionHost.port;
284+
const port = await findFreePort(expected, 10 /* try 10 ports */, 5000 /* try up to 5 seconds */);
285+
286+
if (!port) {
287+
console.warn('%c[Extension Host] %cCould not find a free port for debugging', 'color: blue', 'color:');
288+
return 0;
289+
}
290+
291+
if (port !== expected) {
292+
console.warn(`%c[Extension Host] %cProvided debugging port ${expected} is not free, using ${port} instead.`, 'color: blue', 'color:');
293+
}
294+
if (this._isExtensionDevDebugBrk) {
295+
console.warn(`%c[Extension Host] %cSTOPPED on first line for debugging on port ${port}`, 'color: blue', 'color:');
296+
} else {
297+
console.info(`%c[Extension Host] %cdebugger listening on port ${port}`, 'color: blue', 'color:');
298+
}
299+
return port;
300+
301+
305302
}
306303

307304
private _tryExtHostHandshake(): Promise<PersistentProtocol> {

src/vs/workbench/services/extensions/node/extensionHostProcessSetup.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,6 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise<IRenderer
275275
});
276276
}
277277

278-
// patchExecArgv:
279-
(function () {
280-
// when encountering the prevent-inspect flag we delete this
281-
// and the prior flag
282-
if (process.env.VSCODE_PREVENT_FOREIGN_INSPECT) {
283-
for (let i = 0; i < process.execArgv.length; i++) {
284-
if (process.execArgv[i].match(/--inspect-brk=\d+|--inspect=\d+/)) {
285-
process.execArgv.splice(i, 1);
286-
break;
287-
}
288-
}
289-
}
290-
})();
291-
292278
export async function startExtensionHostProcess(): Promise<void> {
293279

294280
const protocol = await createExtHostProtocol();

0 commit comments

Comments
 (0)