Skip to content

Commit

Permalink
fix: don't overwrite custom NODE_OPTIONS (#1771)
Browse files Browse the repository at this point in the history
Fixes #1746
  • Loading branch information
connor4312 committed Aug 2, 2023
1 parent 4d7c704 commit 670bb82
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 64 deletions.
8 changes: 2 additions & 6 deletions src/targets/node/autoAttachLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
BootloaderEnvironment,
IAutoAttachInfo,
IBootloaderEnvironment,
variableDelimiter,
} from './bootloader/environment';
import { bootloaderDefaultPath, watchdogPath } from './bundlePaths';
import { Capability, INodeBinaryProvider, NodeBinary } from './nodeBinaryProvider';
Expand Down Expand Up @@ -154,11 +153,8 @@ export class AutoAttachLauncher
],
}),
);
variables.prepend('NODE_OPTIONS', bootloaderEnv.NODE_OPTIONS + ' ');
variables.append(
'VSCODE_INSPECTOR_OPTIONS',
variableDelimiter + bootloaderEnv.VSCODE_INSPECTOR_OPTIONS,
);
variables.prepend('NODE_OPTIONS', bootloaderEnv.NODE_OPTIONS);
variables.append('VSCODE_INSPECTOR_OPTIONS', bootloaderEnv.VSCODE_INSPECTOR_OPTIONS);
}

private readSmartPatterns() {
Expand Down
31 changes: 3 additions & 28 deletions src/targets/node/extensionHostAttacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*--------------------------------------------------------*/

import { injectable } from 'inversify';
import { getSourceSuffix } from '../../adapter/templates';
import Cdp from '../../cdp/api';
import { DebugType } from '../../common/contributionUtils';
import { ILogger, LogTag } from '../../common/logging';
import { delay } from '../../common/promiseUtil';
import { ILogger } from '../../common/logging';
import { Semver } from '../../common/semver';
import {
AnyLaunchConfiguration,
Expand Down Expand Up @@ -143,35 +141,12 @@ export class ExtensionHostAttacher extends NodeAttacherBase<IExtensionHostAttach
return;
}

let vars = await this.resolveEnvironment(
const vars = await this.resolveEnvironment(
run,
new NodeBinary('node', Semver.parse(process.versions.node)),
{ openerId: targetId },
);

vars = vars.update('ELECTRON_RUN_AS_NODE', null);

for (let retries = 0; retries < 200; retries++) {
const result = await cdp.Runtime.evaluate({
contextId: 1,
returnByValue: true,
expression:
`Object.assign(process.env, ${JSON.stringify(vars.defined())})` + getSourceSuffix(),
});

if (!result) {
this.logger.error(LogTag.RuntimeTarget, 'Undefined result setting child environment vars');
} else if (result.exceptionDetails) {
this.logger.error(
LogTag.RuntimeTarget,
'Error setting child environment vars',
result.exceptionDetails,
);
} else {
return;
}

await delay(50);
}
return this.appendEnvironmentVariables(cdp, vars.update('ELECTRON_RUN_AS_NODE', null));
}
}
24 changes: 1 addition & 23 deletions src/targets/node/nodeAttacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import * as l10n from '@vscode/l10n';
import { inject, injectable } from 'inversify';
import { IPortLeaseTracker } from '../../adapter/portLeaseTracker';
import { getSourceSuffix } from '../../adapter/templates';
import Cdp from '../../cdp/api';
import { CancellationTokenSource } from '../../common/cancellation';
import { DebugType } from '../../common/contributionUtils';
Expand Down Expand Up @@ -217,27 +216,6 @@ export class NodeAttacher extends NodeAttacherBase<INodeAttachConfiguration> {
}

const vars = await this.resolveEnvironment(run, binary, { requireLease: leasePath, openerId });
for (let retries = 0; retries < 5; retries++) {
const result = await cdp.Runtime.evaluate({
contextId: 1,
returnByValue: true,
expression:
`typeof process === 'undefined' || process.pid === undefined ? 'process not defined' : Object.assign(process.env, ${JSON.stringify(
vars.defined(),
)})` + getSourceSuffix(),
});

if (!result) {
this.logger.error(LogTag.RuntimeTarget, 'Undefined result setting child environment vars');
return;
}

if (!result.exceptionDetails && result.result.value !== 'process not defined') {
return;
}

this.logger.error(LogTag.RuntimeTarget, 'Error setting child environment vars', result);
await delay(50);
}
return this.appendEnvironmentVariables(cdp, vars);
}
}
42 changes: 39 additions & 3 deletions src/targets/node/nodeAttacherBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,49 @@
*--------------------------------------------------------*/

import { injectable } from 'inversify';
import { getSourceSuffix } from '../../adapter/templates';
import Cdp from '../../cdp/api';
import { EnvironmentVars } from '../../common/environmentVars';
import { LogTag } from '../../common/logging';
import { delay } from '../../common/promiseUtil';
import { AnyNodeConfiguration } from '../../configuration';
import { NodeLauncherBase } from './nodeLauncherBase';

/**
* Base class that implements common matters for attachment.
*/
@injectable()
export abstract class NodeAttacherBase<
T extends AnyNodeConfiguration,
> extends NodeLauncherBase<T> {}
export abstract class NodeAttacherBase<T extends AnyNodeConfiguration> extends NodeLauncherBase<T> {
protected async appendEnvironmentVariables(cdp: Cdp.Api, vars: EnvironmentVars) {
const expression =
`typeof process==='undefined'||process.pid===undefined?'process not defined':(()=>{` +
Object.entries(vars.defined())
.map(([key, value]) => {
const k = JSON.stringify(key);
return `process.env[${k}]=(process.env[${k}]||'')+${JSON.stringify(value)}`;
})
.join(';') +
'})()' +
getSourceSuffix();

for (let retries = 0; retries < 5; retries++) {
const result = await cdp.Runtime.evaluate({
contextId: 1,
returnByValue: true,
expression,
});

if (!result) {
this.logger.error(LogTag.RuntimeTarget, 'Undefined result setting child environment vars');
return;
}

if (!result.exceptionDetails && result.result.value !== 'process not defined') {
return;
}

this.logger.error(LogTag.RuntimeTarget, 'Error setting child environment vars', result);
await delay(50);
}
}
}
10 changes: 7 additions & 3 deletions src/targets/node/nodeLauncherBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ import {
} from '../../targets/targets';
import { ITelemetryReporter } from '../../telemetry/telemetryReporter';
import { ISourcePathResolverFactory } from '../sourcePathResolverFactory';
import { IBootloaderEnvironment, IBootloaderInfo } from './bootloader/environment';
import {
IBootloaderEnvironment,
IBootloaderInfo,
variableDelimiter,
} from './bootloader/environment';
import { bootloaderDefaultPath } from './bundlePaths';
import { Capability, INodeBinaryProvider, NodeBinary } from './nodeBinaryProvider';
import { NodeSourcePathResolver } from './nodeSourcePathResolver';
Expand Down Expand Up @@ -312,8 +316,8 @@ export abstract class NodeLauncherBase<T extends AnyNodeConfiguration> implement
const env = {
// Require our bootloader first, to run it before any other bootloader
// we could have injected in the parent process.
NODE_OPTIONS: `--require ${bootloader.interpolatedPath}`,
VSCODE_INSPECTOR_OPTIONS: JSON.stringify(bootloaderInfo),
NODE_OPTIONS: ` --require ${bootloader.interpolatedPath} `,
VSCODE_INSPECTOR_OPTIONS: variableDelimiter + JSON.stringify(bootloaderInfo),
ELECTRON_RUN_AS_NODE: null,
} as IBootloaderEnvironment;

Expand Down
2 changes: 1 addition & 1 deletion src/test/node/node-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ describe('node runtime', () => {
const worker = await r.worker();
const optionsOut = worker.dap.once('output', o => o.output.includes('NODE_OPTIONS'));
handle.logger.logOutput(await optionsOut);
handle.assertLog({ customAssert: l => expect(l).to.contain('NODE_OPTIONS= --require') });
handle.assertLog({ customAssert: l => expect(l).to.contain('NODE_OPTIONS= --require') });
});

itIntegrates('allows simple port attachment', async ({ r }) => {
Expand Down

0 comments on commit 670bb82

Please sign in to comment.