Skip to content

Commit 2b68877

Browse files
authored
Adopt import.meta.dirname and import.meta.filename (fix microsoft#259658) (microsoft#259845)
1 parent 70233e1 commit 2b68877

File tree

6 files changed

+11
-29
lines changed

6 files changed

+11
-29
lines changed

src/bootstrap-esm.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as path from 'path';
76
import * as fs from 'fs';
8-
import { fileURLToPath } from 'url';
97
import { createRequire, register } from 'node:module';
108
import { product, pkg } from './bootstrap-meta.js';
119
import './bootstrap-node.js';
1210
import * as performance from './vs/base/common/performance.js';
1311
import { INLSConfiguration } from './vs/nls.js';
1412

1513
const require = createRequire(import.meta.url);
16-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1714

1815
// Install a hook to module resolution to map 'fs' to 'original-fs'
1916
if (process.env['ELECTRON_RUN_AS_NODE'] || process.versions['electron']) {
@@ -43,7 +40,7 @@ if (process.env['VSCODE_DEV']) {
4340
} catch (error) { /* ignore */ }
4441
}
4542
globalThis._VSCODE_PACKAGE_JSON = { ...pkg };
46-
globalThis._VSCODE_FILE_ROOT = __dirname;
43+
globalThis._VSCODE_FILE_ROOT = import.meta.dirname;
4744

4845
//#region NLS helpers
4946

src/bootstrap-node.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55

66
import * as path from 'path';
77
import * as fs from 'fs';
8-
import { fileURLToPath } from 'url';
98
import { createRequire } from 'node:module';
109
import type { IProductConfiguration } from './vs/base/common/product.js';
1110

1211
const require = createRequire(import.meta.url);
13-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1412
const isWindows = process.platform === 'win32';
1513

1614
// increase number of stack frames(from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
@@ -133,7 +131,7 @@ export function removeGlobalNodeJsModuleLookupPaths(): void {
133131
* Helper to enable portable mode.
134132
*/
135133
export function configurePortable(product: Partial<IProductConfiguration>): { portableDataPath: string; isPortable: boolean } {
136-
const appRoot = path.dirname(__dirname);
134+
const appRoot = path.dirname(import.meta.dirname);
137135

138136
function getApplicationPath(): string {
139137
if (process.env['VSCODE_DEV']) {

src/cli.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import './bootstrap-cli.js'; // this MUST come before other imports as it changes global state
7-
import { dirname } from 'path';
8-
import { fileURLToPath } from 'url';
97
import { configurePortable } from './bootstrap-node.js';
108
import { bootstrapESM } from './bootstrap-esm.js';
119
import { resolveNLSConfiguration } from './vs/base/node/nls.js';
1210
import { product } from './bootstrap-meta.js';
1311

14-
const __dirname = dirname(fileURLToPath(import.meta.url));
15-
1612
// NLS
17-
const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: __dirname });
13+
const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: import.meta.dirname });
1814
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfiguration); // required for `bootstrap-esm` to pick up NLS messages
1915

2016
// Enable portable support

src/main.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import * as os from 'os';
99
import { performance } from 'perf_hooks';
1010
import { configurePortable } from './bootstrap-node.js';
1111
import { bootstrapESM } from './bootstrap-esm.js';
12-
import { fileURLToPath } from 'url';
1312
import { app, protocol, crashReporter, Menu, contentTracing } from 'electron';
1413
import minimist from 'minimist';
1514
import { product } from './bootstrap-meta.js';
@@ -21,8 +20,6 @@ import { getUNCHost, addUNCHostToAllowlist } from './vs/base/node/unc.js';
2120
import { INLSConfiguration } from './vs/nls.js';
2221
import { NativeParsedArgs } from './vs/platform/environment/common/argv.js';
2322

24-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
25-
2623
perf.mark('code/didStartMain');
2724

2825
perf.mark('code/willLoadMainBundle', {
@@ -129,7 +126,7 @@ if (userLocale) {
129126
osLocale,
130127
commit: product.commit,
131128
userDataPath,
132-
nlsMetadataPath: __dirname
129+
nlsMetadataPath: import.meta.dirname
133130
});
134131
}
135132

@@ -691,7 +688,7 @@ async function resolveNlsConfiguration(): Promise<INLSConfiguration> {
691688
userLocale: 'en',
692689
osLocale,
693690
resolvedLanguage: 'en',
694-
defaultMessagesFile: path.join(__dirname, 'nls.messages.json'),
691+
defaultMessagesFile: path.join(import.meta.dirname, 'nls.messages.json'),
695692

696693
// NLS: below 2 are a relic from old times only used by vscode-nls and deprecated
697694
locale: 'en',
@@ -707,7 +704,7 @@ async function resolveNlsConfiguration(): Promise<INLSConfiguration> {
707704
osLocale,
708705
commit: product.commit,
709706
userDataPath,
710-
nlsMetadataPath: __dirname
707+
nlsMetadataPath: import.meta.dirname
711708
});
712709
}
713710

src/server-cli.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import './bootstrap-server.js'; // this MUST come before other imports as it changes global state
7-
import { dirname, join } from 'path';
8-
import { fileURLToPath } from 'url';
7+
import { join } from 'path';
98
import { devInjectNodeModuleLookupPath } from './bootstrap-node.js';
109
import { bootstrapESM } from './bootstrap-esm.js';
1110
import { resolveNLSConfiguration } from './vs/base/node/nls.js';
1211
import { product } from './bootstrap-meta.js';
1312

14-
const __dirname = dirname(fileURLToPath(import.meta.url));
15-
1613
// NLS
17-
const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: __dirname });
14+
const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: import.meta.dirname });
1815
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfiguration); // required for `bootstrap-esm` to pick up NLS messages
1916

2017
if (process.env['VSCODE_DEV']) {
2118
// When running out of sources, we need to load node modules from remote/node_modules,
2219
// which are compiled against nodejs, not electron
23-
process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] = process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] || join(__dirname, '..', 'remote', 'node_modules');
20+
process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] = process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] || join(import.meta.dirname, '..', 'remote', 'node_modules');
2421
devInjectNodeModuleLookupPath(process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH']);
2522
} else {
2623
delete process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'];

src/server-main.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { AddressInfo } from 'net';
1010
import * as os from 'os';
1111
import * as readline from 'readline';
1212
import { performance } from 'perf_hooks';
13-
import { fileURLToPath } from 'url';
1413
import minimist from 'minimist';
1514
import { devInjectNodeModuleLookupPath, removeGlobalNodeJsModuleLookupPaths } from './bootstrap-node.js';
1615
import { bootstrapESM } from './bootstrap-esm.js';
@@ -20,8 +19,6 @@ import * as perf from './vs/base/common/performance.js';
2019
import { INLSConfiguration } from './vs/nls.js';
2120
import { IServerAPI } from './vs/server/node/remoteExtensionHostAgentServer.js';
2221

23-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
24-
2522
perf.mark('code/server/start');
2623
(globalThis as any).vscodeServerStartTime = performance.now();
2724

@@ -45,7 +42,7 @@ const extensionInstallArgs = ['install-extension', 'install-builtin-extension',
4542

4643
const shouldSpawnCli = parsedArgs.help || parsedArgs.version || extensionLookupArgs.some(a => !!parsedArgs[a]) || (extensionInstallArgs.some(a => !!parsedArgs[a]) && !parsedArgs['start-server']);
4744

48-
const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: __dirname });
45+
const nlsConfiguration = await resolveNLSConfiguration({ userLocale: 'en', osLocale: 'en', commit: product.commit, userDataPath: '', nlsMetadataPath: import.meta.dirname });
4946

5047
if (shouldSpawnCli) {
5148
loadCode(nlsConfiguration).then((mod) => {
@@ -241,7 +238,7 @@ async function loadCode(nlsConfiguration: INLSConfiguration) {
241238
if (process.env['VSCODE_DEV']) {
242239
// When running out of sources, we need to load node modules from remote/node_modules,
243240
// which are compiled against nodejs, not electron
244-
process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] = process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] || path.join(__dirname, '..', 'remote', 'node_modules');
241+
process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] = process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'] || path.join(import.meta.dirname, '..', 'remote', 'node_modules');
245242
devInjectNodeModuleLookupPath(process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH']);
246243
} else {
247244
delete process.env['VSCODE_DEV_INJECT_NODE_MODULE_LOOKUP_PATH'];

0 commit comments

Comments
 (0)