Skip to content

Commit ebd0778

Browse files
Deepak Mohandeepak1556
andauthored
fix,win: remove global paths from node module lookup (#19)
Co-authored-by: deepak1556 <[email protected]>
1 parent 33fc5a9 commit ebd0778

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/bootstrap-node.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { IProductConfiguration } from './vs/base/common/product.js';
1111

1212
const require = createRequire(import.meta.url);
1313
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
const isWindows = process.platform === 'win32';
1415

1516
// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
1617
Error.stackTraceLimit = 100;
@@ -97,6 +98,35 @@ export function removeGlobalNodeJsModuleLookupPaths(): void {
9798

9899
return paths;
99100
};
101+
102+
const originalNodeModulePaths = Module._nodeModulePaths;
103+
Module._nodeModulePaths = function (from: string): string[] {
104+
let paths: string[] = originalNodeModulePaths(from);
105+
if (!isWindows) {
106+
return paths;
107+
}
108+
109+
// On Windows, remove drive(s) and users' home directory from search paths,
110+
// UNLESS 'from' is explicitly set to one of those.
111+
const isDrive = (p: string) => p.length >= 3 && p.endsWith(':\\');
112+
113+
if (!isDrive(from)) {
114+
paths = paths.filter(p => !isDrive(path.dirname(p)));
115+
}
116+
117+
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
118+
const userDir = path.dirname(path.join(process.env.HOMEDRIVE, process.env.HOMEPATH));
119+
120+
const isUsersDir = (p: string) => path.relative(p, userDir).length === 0;
121+
122+
// Check if 'from' is the same as 'userDir'
123+
if (!isUsersDir(from)) {
124+
paths = paths.filter(p => !isUsersDir(path.dirname(p)));
125+
}
126+
}
127+
128+
return paths;
129+
};
100130
}
101131

102132
/**

src/server-main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as readline from 'readline';
1212
import { performance } from 'perf_hooks';
1313
import { fileURLToPath } from 'url';
1414
import minimist from 'minimist';
15-
import { devInjectNodeModuleLookupPath } from './bootstrap-node.js';
15+
import { devInjectNodeModuleLookupPath, removeGlobalNodeJsModuleLookupPaths } from './bootstrap-node.js';
1616
import { bootstrapESM } from './bootstrap-esm.js';
1717
import { resolveNLSConfiguration } from './vs/base/node/nls.js';
1818
import { product } from './bootstrap-meta.js';
@@ -247,6 +247,9 @@ async function loadCode(nlsConfiguration: INLSConfiguration) {
247247
delete process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'];
248248
}
249249

250+
// Remove global paths from the node module lookup (node.js only)
251+
removeGlobalNodeJsModuleLookupPaths();
252+
250253
// Bootstrap ESM
251254
await bootstrapESM();
252255

0 commit comments

Comments
 (0)