Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/main.ts
5240 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as path from 'node:path';
7
import * as fs from 'original-fs';
8
import * as os from 'node:os';
9
import { performance } from 'node:perf_hooks';
10
import { configurePortable } from './bootstrap-node.js';
11
import { bootstrapESM } from './bootstrap-esm.js';
12
import { app, protocol, crashReporter, Menu, contentTracing } from 'electron';
13
import minimist from 'minimist';
14
import { product } from './bootstrap-meta.js';
15
import { parse } from './vs/base/common/jsonc.js';
16
import { getUserDataPath } from './vs/platform/environment/node/userDataPath.js';
17
import * as perf from './vs/base/common/performance.js';
18
import { resolveNLSConfiguration } from './vs/base/node/nls.js';
19
import { getUNCHost, addUNCHostToAllowlist } from './vs/base/node/unc.js';
20
import { INLSConfiguration } from './vs/nls.js';
21
import { NativeParsedArgs } from './vs/platform/environment/common/argv.js';
22
23
perf.mark('code/didStartMain');
24
25
perf.mark('code/willLoadMainBundle', {
26
// When built, the main bundle is a single JS file with all
27
// dependencies inlined. As such, we mark `willLoadMainBundle`
28
// as the start of the main bundle loading process.
29
startTime: Math.floor(performance.timeOrigin)
30
});
31
perf.mark('code/didLoadMainBundle');
32
33
// Enable portable support
34
const portable = configurePortable(product);
35
36
const args = parseCLIArgs();
37
// Configure static command line arguments
38
const argvConfig = configureCommandlineSwitchesSync(args);
39
// Enable sandbox globally unless
40
// 1) disabled via command line using either
41
// `--no-sandbox` or `--disable-chromium-sandbox` argument.
42
// 2) argv.json contains `disable-chromium-sandbox: true`.
43
if (args['sandbox'] &&
44
!args['disable-chromium-sandbox'] &&
45
!argvConfig['disable-chromium-sandbox']) {
46
app.enableSandbox();
47
} else if (app.commandLine.hasSwitch('no-sandbox') &&
48
!app.commandLine.hasSwitch('disable-gpu-sandbox')) {
49
// Disable GPU sandbox whenever --no-sandbox is used.
50
app.commandLine.appendSwitch('disable-gpu-sandbox');
51
} else {
52
app.commandLine.appendSwitch('no-sandbox');
53
app.commandLine.appendSwitch('disable-gpu-sandbox');
54
}
55
56
// Set userData path before app 'ready' event
57
const userDataPath = getUserDataPath(args, product.nameShort ?? 'code-oss-dev');
58
if (process.platform === 'win32') {
59
const userDataUNCHost = getUNCHost(userDataPath);
60
if (userDataUNCHost) {
61
addUNCHostToAllowlist(userDataUNCHost); // enables to use UNC paths in userDataPath
62
}
63
}
64
app.setPath('userData', userDataPath);
65
66
// Resolve code cache path
67
const codeCachePath = getCodeCachePath();
68
69
// Disable default menu (https://github.com/electron/electron/issues/35512)
70
Menu.setApplicationMenu(null);
71
72
// Configure crash reporter
73
perf.mark('code/willStartCrashReporter');
74
// If a crash-reporter-directory is specified we store the crash reports
75
// in the specified directory and don't upload them to the crash server.
76
//
77
// Appcenter crash reporting is enabled if
78
// * enable-crash-reporter runtime argument is set to 'true'
79
// * --disable-crash-reporter command line parameter is not set
80
//
81
// Disable crash reporting in all other cases.
82
if (args['crash-reporter-directory'] || (argvConfig['enable-crash-reporter'] && !args['disable-crash-reporter'])) {
83
configureCrashReporter();
84
}
85
perf.mark('code/didStartCrashReporter');
86
87
// Set logs path before app 'ready' event if running portable
88
// to ensure that no 'logs' folder is created on disk at a
89
// location outside of the portable directory
90
// (https://github.com/microsoft/vscode/issues/56651)
91
if (portable.isPortable) {
92
app.setAppLogsPath(path.join(userDataPath, 'logs'));
93
}
94
95
// Register custom schemes with privileges
96
protocol.registerSchemesAsPrivileged([
97
{
98
scheme: 'vscode-webview',
99
privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, allowServiceWorkers: true, codeCache: true }
100
},
101
{
102
scheme: 'vscode-file',
103
privileges: { secure: true, standard: true, supportFetchAPI: true, corsEnabled: true, codeCache: true }
104
}
105
]);
106
107
// Global app listeners
108
registerListeners();
109
110
/**
111
* We can resolve the NLS configuration early if it is defined
112
* in argv.json before `app.ready` event. Otherwise we can only
113
* resolve NLS after `app.ready` event to resolve the OS locale.
114
*/
115
let nlsConfigurationPromise: Promise<INLSConfiguration> | undefined = undefined;
116
117
// Use the most preferred OS language for language recommendation.
118
// The API might return an empty array on Linux, such as when
119
// the 'C' locale is the user's only configured locale.
120
// No matter the OS, if the array is empty, default back to 'en'.
121
const osLocale = processZhLocale((app.getPreferredSystemLanguages()?.[0] ?? 'en').toLowerCase());
122
const userLocale = getUserDefinedLocale(argvConfig);
123
if (userLocale) {
124
nlsConfigurationPromise = resolveNLSConfiguration({
125
userLocale,
126
osLocale,
127
commit: product.commit,
128
userDataPath,
129
nlsMetadataPath: import.meta.dirname
130
});
131
}
132
133
// Pass in the locale to Electron so that the
134
// Windows Control Overlay is rendered correctly on Windows.
135
// For now, don't pass in the locale on macOS due to
136
// https://github.com/microsoft/vscode/issues/167543.
137
// If the locale is `qps-ploc`, the Microsoft
138
// Pseudo Language Language Pack is being used.
139
// In that case, use `en` as the Electron locale.
140
141
if (process.platform === 'win32' || process.platform === 'linux') {
142
const electronLocale = (!userLocale || userLocale === 'qps-ploc') ? 'en' : userLocale;
143
app.commandLine.appendSwitch('lang', electronLocale);
144
}
145
146
// Load our code once ready
147
app.once('ready', function () {
148
if (args['trace']) {
149
let traceOptions: Electron.TraceConfig | Electron.TraceCategoriesAndOptions;
150
if (args['trace-memory-infra']) {
151
const customCategories = args['trace-category-filter']?.split(',') || [];
152
customCategories.push('disabled-by-default-memory-infra', 'disabled-by-default-memory-infra.v8.code_stats');
153
traceOptions = {
154
included_categories: customCategories,
155
excluded_categories: ['*'],
156
memory_dump_config: {
157
allowed_dump_modes: ['light', 'detailed'],
158
triggers: [
159
{
160
type: 'periodic_interval',
161
mode: 'detailed',
162
min_time_between_dumps_ms: 10000
163
},
164
{
165
type: 'periodic_interval',
166
mode: 'light',
167
min_time_between_dumps_ms: 1000
168
}
169
]
170
}
171
};
172
} else {
173
traceOptions = {
174
categoryFilter: args['trace-category-filter'] || '*',
175
traceOptions: args['trace-options'] || 'record-until-full,enable-sampling'
176
};
177
}
178
179
contentTracing.startRecording(traceOptions).finally(() => onReady());
180
} else {
181
onReady();
182
}
183
});
184
185
async function onReady() {
186
perf.mark('code/mainAppReady');
187
188
try {
189
const [, nlsConfig] = await Promise.all([
190
mkdirpIgnoreError(codeCachePath),
191
resolveNlsConfiguration()
192
]);
193
194
await startup(codeCachePath, nlsConfig);
195
} catch (error) {
196
console.error(error);
197
}
198
}
199
200
/**
201
* Main startup routine
202
*/
203
async function startup(codeCachePath: string | undefined, nlsConfig: INLSConfiguration): Promise<void> {
204
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
205
process.env['VSCODE_CODE_CACHE_PATH'] = codeCachePath || '';
206
207
// Bootstrap ESM
208
await bootstrapESM();
209
210
// Load Main
211
await import('./vs/code/electron-main/main.js');
212
perf.mark('code/didRunMainBundle');
213
}
214
215
function configureCommandlineSwitchesSync(cliArgs: NativeParsedArgs) {
216
const SUPPORTED_ELECTRON_SWITCHES = [
217
218
// alias from us for --disable-gpu
219
'disable-hardware-acceleration',
220
221
// override for the color profile to use
222
'force-color-profile',
223
224
// disable LCD font rendering, a Chromium flag
225
'disable-lcd-text',
226
227
// bypass any specified proxy for the given semi-colon-separated list of hosts
228
'proxy-bypass-list',
229
230
'remote-debugging-port'
231
];
232
233
if (process.platform === 'linux') {
234
235
// Force enable screen readers on Linux via this flag
236
SUPPORTED_ELECTRON_SWITCHES.push('force-renderer-accessibility');
237
238
// override which password-store is used on Linux
239
SUPPORTED_ELECTRON_SWITCHES.push('password-store');
240
}
241
242
const SUPPORTED_MAIN_PROCESS_SWITCHES = [
243
244
// Persistently enable proposed api via argv.json: https://github.com/microsoft/vscode/issues/99775
245
'enable-proposed-api',
246
247
// Log level to use. Default is 'info'. Allowed values are 'error', 'warn', 'info', 'debug', 'trace', 'off'.
248
'log-level',
249
250
// Use an in-memory storage for secrets
251
'use-inmemory-secretstorage',
252
253
// Enables display tracking to restore maximized windows under RDP: https://github.com/electron/electron/issues/47016
254
'enable-rdp-display-tracking',
255
];
256
257
// Read argv config
258
const argvConfig = readArgvConfigSync();
259
260
Object.keys(argvConfig).forEach(argvKey => {
261
const argvValue = argvConfig[argvKey];
262
263
// Append Electron flags to Electron
264
if (SUPPORTED_ELECTRON_SWITCHES.indexOf(argvKey) !== -1) {
265
if (argvValue === true || argvValue === 'true') {
266
if (argvKey === 'disable-hardware-acceleration') {
267
app.disableHardwareAcceleration(); // needs to be called explicitly
268
} else {
269
app.commandLine.appendSwitch(argvKey);
270
}
271
} else if (typeof argvValue === 'string' && argvValue) {
272
if (argvKey === 'password-store') {
273
// Password store
274
// TODO@TylerLeonhardt: Remove this migration in 3 months
275
let migratedArgvValue = argvValue;
276
if (argvValue === 'gnome' || argvValue === 'gnome-keyring') {
277
migratedArgvValue = 'gnome-libsecret';
278
}
279
app.commandLine.appendSwitch(argvKey, migratedArgvValue);
280
} else {
281
app.commandLine.appendSwitch(argvKey, argvValue);
282
}
283
}
284
}
285
286
// Append main process flags to process.argv
287
else if (SUPPORTED_MAIN_PROCESS_SWITCHES.indexOf(argvKey) !== -1) {
288
switch (argvKey) {
289
case 'enable-proposed-api':
290
if (Array.isArray(argvValue)) {
291
argvValue.forEach(id => id && typeof id === 'string' && process.argv.push('--enable-proposed-api', id));
292
} else {
293
console.error(`Unexpected value for \`enable-proposed-api\` in argv.json. Expected array of extension ids.`);
294
}
295
break;
296
297
case 'log-level':
298
if (typeof argvValue === 'string') {
299
process.argv.push('--log', argvValue);
300
} else if (Array.isArray(argvValue)) {
301
for (const value of argvValue) {
302
process.argv.push('--log', value);
303
}
304
}
305
break;
306
307
case 'use-inmemory-secretstorage':
308
if (argvValue) {
309
process.argv.push('--use-inmemory-secretstorage');
310
}
311
break;
312
313
case 'enable-rdp-display-tracking':
314
if (argvValue) {
315
process.argv.push('--enable-rdp-display-tracking');
316
}
317
break;
318
}
319
}
320
});
321
322
// Following features are enabled from the runtime:
323
// `NetAdapterMaxBufSizeFeature` - Specify the max buffer size for NetToMojoPendingBuffer, refs https://github.com/microsoft/vscode/issues/268800
324
// `DocumentPolicyIncludeJSCallStacksInCrashReports` - https://www.electronjs.org/docs/latest/api/web-frame-main#framecollectjavascriptcallstack-experimental
325
// `EarlyEstablishGpuChannel` - Refs https://issues.chromium.org/issues/40208065
326
// `EstablishGpuChannelAsync` - Refs https://issues.chromium.org/issues/40208065
327
const featuresToEnable =
328
`NetAdapterMaxBufSizeFeature:NetAdapterMaxBufSize/8192,DocumentPolicyIncludeJSCallStacksInCrashReports,EarlyEstablishGpuChannel,EstablishGpuChannelAsync,${app.commandLine.getSwitchValue('enable-features')}`;
329
app.commandLine.appendSwitch('enable-features', featuresToEnable);
330
331
// Following features are disabled from the runtime:
332
// `CalculateNativeWinOcclusion` - Disable native window occlusion tracker (https://groups.google.com/a/chromium.org/g/embedder-dev/c/ZF3uHHyWLKw/m/VDN2hDXMAAAJ)
333
const featuresToDisable =
334
`CalculateNativeWinOcclusion,${app.commandLine.getSwitchValue('disable-features')}`;
335
app.commandLine.appendSwitch('disable-features', featuresToDisable);
336
337
// Blink features to configure.
338
// `FontMatchingCTMigration` - Siwtch font matching on macOS to Appkit (Refs https://github.com/microsoft/vscode/issues/224496#issuecomment-2270418470).
339
// `StandardizedBrowserZoom` - Disable zoom adjustment for bounding box (https://github.com/microsoft/vscode/issues/232750#issuecomment-2459495394)
340
const blinkFeaturesToDisable =
341
`FontMatchingCTMigration,StandardizedBrowserZoom,${app.commandLine.getSwitchValue('disable-blink-features')}`;
342
app.commandLine.appendSwitch('disable-blink-features', blinkFeaturesToDisable);
343
344
// Support JS Flags
345
const jsFlags = getJSFlags(cliArgs);
346
if (jsFlags) {
347
app.commandLine.appendSwitch('js-flags', jsFlags);
348
}
349
350
// Use portal version 4 that supports current_folder option
351
// to address https://github.com/microsoft/vscode/issues/213780
352
// Runtime sets the default version to 3, refs https://github.com/electron/electron/pull/44426
353
app.commandLine.appendSwitch('xdg-portal-required-version', '4');
354
355
// Increase the maximum number of active WebGL contexts as each terminal may
356
// use up to 2
357
app.commandLine.appendSwitch('max-active-webgl-contexts', '32');
358
359
// Disable Skia Graphite backend.
360
// Refs https://github.com/microsoft/vscode/issues/284162
361
app.commandLine.appendSwitch('disable-skia-graphite');
362
363
return argvConfig;
364
}
365
366
interface IArgvConfig {
367
[key: string]: string | string[] | boolean | undefined;
368
readonly locale?: string;
369
readonly 'disable-lcd-text'?: boolean;
370
readonly 'proxy-bypass-list'?: string;
371
readonly 'disable-hardware-acceleration'?: boolean;
372
readonly 'force-color-profile'?: string;
373
readonly 'enable-crash-reporter'?: boolean;
374
readonly 'crash-reporter-id'?: string;
375
readonly 'enable-proposed-api'?: string[];
376
readonly 'log-level'?: string | string[];
377
readonly 'disable-chromium-sandbox'?: boolean;
378
readonly 'use-inmemory-secretstorage'?: boolean;
379
readonly 'enable-rdp-display-tracking'?: boolean;
380
readonly 'remote-debugging-port'?: string;
381
}
382
383
function readArgvConfigSync(): IArgvConfig {
384
385
// Read or create the argv.json config file sync before app('ready')
386
const argvConfigPath = getArgvConfigPath();
387
let argvConfig: IArgvConfig | undefined = undefined;
388
try {
389
argvConfig = parse(fs.readFileSync(argvConfigPath).toString());
390
} catch (error) {
391
if (error && error.code === 'ENOENT') {
392
createDefaultArgvConfigSync(argvConfigPath);
393
} else {
394
console.warn(`Unable to read argv.json configuration file in ${argvConfigPath}, falling back to defaults (${error})`);
395
}
396
}
397
398
// Fallback to default
399
if (!argvConfig) {
400
argvConfig = {};
401
}
402
403
return argvConfig;
404
}
405
406
function createDefaultArgvConfigSync(argvConfigPath: string): void {
407
try {
408
409
// Ensure argv config parent exists
410
const argvConfigPathDirname = path.dirname(argvConfigPath);
411
if (!fs.existsSync(argvConfigPathDirname)) {
412
fs.mkdirSync(argvConfigPathDirname);
413
}
414
415
// Default argv content
416
const defaultArgvConfigContent = [
417
'// This configuration file allows you to pass permanent command line arguments to VS Code.',
418
'// Only a subset of arguments is currently supported to reduce the likelihood of breaking',
419
'// the installation.',
420
'//',
421
'// PLEASE DO NOT CHANGE WITHOUT UNDERSTANDING THE IMPACT',
422
'//',
423
'// NOTE: Changing this file requires a restart of VS Code.',
424
'{',
425
' // Use software rendering instead of hardware accelerated rendering.',
426
' // This can help in cases where you see rendering issues in VS Code.',
427
' // "disable-hardware-acceleration": true',
428
'}'
429
];
430
431
// Create initial argv.json with default content
432
fs.writeFileSync(argvConfigPath, defaultArgvConfigContent.join('\n'));
433
} catch (error) {
434
console.error(`Unable to create argv.json configuration file in ${argvConfigPath}, falling back to defaults (${error})`);
435
}
436
}
437
438
function getArgvConfigPath(): string {
439
const vscodePortable = process.env['VSCODE_PORTABLE'];
440
if (vscodePortable) {
441
return path.join(vscodePortable, 'argv.json');
442
}
443
444
let dataFolderName = product.dataFolderName;
445
if (process.env['VSCODE_DEV']) {
446
dataFolderName = `${dataFolderName}-dev`;
447
}
448
449
return path.join(os.homedir(), dataFolderName!, 'argv.json');
450
}
451
452
function configureCrashReporter(): void {
453
let crashReporterDirectory = args['crash-reporter-directory'];
454
let submitURL = '';
455
if (crashReporterDirectory) {
456
crashReporterDirectory = path.normalize(crashReporterDirectory);
457
458
if (!path.isAbsolute(crashReporterDirectory)) {
459
console.error(`The path '${crashReporterDirectory}' specified for --crash-reporter-directory must be absolute.`);
460
app.exit(1);
461
}
462
463
if (!fs.existsSync(crashReporterDirectory)) {
464
try {
465
fs.mkdirSync(crashReporterDirectory, { recursive: true });
466
} catch (error) {
467
console.error(`The path '${crashReporterDirectory}' specified for --crash-reporter-directory does not seem to exist or cannot be created.`);
468
app.exit(1);
469
}
470
}
471
472
// Crashes are stored in the crashDumps directory by default, so we
473
// need to change that directory to the provided one
474
console.log(`Found --crash-reporter-directory argument. Setting crashDumps directory to be '${crashReporterDirectory}'`);
475
app.setPath('crashDumps', crashReporterDirectory);
476
}
477
478
// Otherwise we configure the crash reporter from product.json
479
else {
480
const appCenter = product.appCenter;
481
if (appCenter) {
482
const isWindows = (process.platform === 'win32');
483
const isLinux = (process.platform === 'linux');
484
const isDarwin = (process.platform === 'darwin');
485
const crashReporterId = argvConfig['crash-reporter-id'];
486
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
487
if (crashReporterId && uuidPattern.test(crashReporterId)) {
488
if (isWindows) {
489
switch (process.arch) {
490
case 'x64':
491
submitURL = appCenter['win32-x64'];
492
break;
493
case 'arm64':
494
submitURL = appCenter['win32-arm64'];
495
break;
496
}
497
} else if (isDarwin) {
498
if (product.darwinUniversalAssetId) {
499
submitURL = appCenter['darwin-universal'];
500
} else {
501
switch (process.arch) {
502
case 'x64':
503
submitURL = appCenter['darwin'];
504
break;
505
case 'arm64':
506
submitURL = appCenter['darwin-arm64'];
507
break;
508
}
509
}
510
} else if (isLinux) {
511
submitURL = appCenter['linux-x64'];
512
}
513
submitURL = submitURL.concat('&uid=', crashReporterId, '&iid=', crashReporterId, '&sid=', crashReporterId);
514
// Send the id for child node process that are explicitly starting crash reporter.
515
// For vscode this is ExtensionHost process currently.
516
const argv = process.argv;
517
const endOfArgsMarkerIndex = argv.indexOf('--');
518
if (endOfArgsMarkerIndex === -1) {
519
argv.push('--crash-reporter-id', crashReporterId);
520
} else {
521
// if the we have an argument "--" (end of argument marker)
522
// we cannot add arguments at the end. rather, we add
523
// arguments before the "--" marker.
524
argv.splice(endOfArgsMarkerIndex, 0, '--crash-reporter-id', crashReporterId);
525
}
526
}
527
}
528
}
529
530
// Start crash reporter for all processes
531
const productName = (product.crashReporter ? product.crashReporter.productName : undefined) || product.nameShort;
532
const companyName = (product.crashReporter ? product.crashReporter.companyName : undefined) || 'Microsoft';
533
const uploadToServer = Boolean(!process.env['VSCODE_DEV'] && submitURL && !crashReporterDirectory);
534
crashReporter.start({
535
companyName,
536
productName: process.env['VSCODE_DEV'] ? `${productName} Dev` : productName,
537
submitURL,
538
uploadToServer,
539
compress: true,
540
ignoreSystemCrashHandler: true
541
});
542
}
543
544
function getJSFlags(cliArgs: NativeParsedArgs): string | null {
545
const jsFlags: string[] = [];
546
547
// Add any existing JS flags we already got from the command line
548
if (cliArgs['js-flags']) {
549
jsFlags.push(cliArgs['js-flags']);
550
}
551
552
if (process.platform === 'linux') {
553
// Fix cppgc crash on Linux with 16KB page size.
554
// Refs https://issues.chromium.org/issues/378017037
555
// The fix from https://github.com/electron/electron/commit/6c5b2ef55e08dc0bede02384747549c1eadac0eb
556
// only affects non-renderer process.
557
// The following will ensure that the flag will be
558
// applied to the renderer process as well.
559
// TODO(deepak1556): Remove this once we update to
560
// Chromium >= 134.
561
jsFlags.push('--nodecommit_pooled_pages');
562
}
563
564
return jsFlags.length > 0 ? jsFlags.join(' ') : null;
565
}
566
567
function parseCLIArgs(): NativeParsedArgs {
568
return minimist(process.argv, {
569
string: [
570
'user-data-dir',
571
'locale',
572
'js-flags',
573
'crash-reporter-directory'
574
],
575
boolean: [
576
'disable-chromium-sandbox',
577
],
578
default: {
579
'sandbox': true
580
},
581
alias: {
582
'no-sandbox': 'sandbox'
583
}
584
});
585
}
586
587
function registerListeners(): void {
588
589
/**
590
* macOS: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
591
* the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
592
*/
593
const macOpenFiles: string[] = [];
594
(globalThis as { macOpenFiles?: string[] }).macOpenFiles = macOpenFiles;
595
app.on('open-file', function (event, path) {
596
macOpenFiles.push(path);
597
});
598
599
/**
600
* macOS: react to open-url requests.
601
*/
602
const openUrls: string[] = [];
603
const onOpenUrl =
604
function (event: { preventDefault: () => void }, url: string) {
605
event.preventDefault();
606
607
openUrls.push(url);
608
};
609
610
app.on('will-finish-launching', function () {
611
app.on('open-url', onOpenUrl);
612
});
613
614
(globalThis as { getOpenUrls?: () => string[] }).getOpenUrls = function () {
615
app.removeListener('open-url', onOpenUrl);
616
617
return openUrls;
618
};
619
}
620
621
function getCodeCachePath(): string | undefined {
622
623
// explicitly disabled via CLI args
624
if (process.argv.indexOf('--no-cached-data') > 0) {
625
return undefined;
626
}
627
628
// running out of sources
629
if (process.env['VSCODE_DEV']) {
630
return undefined;
631
}
632
633
// require commit id
634
const commit = product.commit;
635
if (!commit) {
636
return undefined;
637
}
638
639
return path.join(userDataPath, 'CachedData', commit);
640
}
641
642
async function mkdirpIgnoreError(dir: string | undefined): Promise<string | undefined> {
643
if (typeof dir === 'string') {
644
try {
645
await fs.promises.mkdir(dir, { recursive: true });
646
647
return dir;
648
} catch (error) {
649
// ignore
650
}
651
}
652
653
return undefined;
654
}
655
656
//#region NLS Support
657
658
function processZhLocale(appLocale: string): string {
659
if (appLocale.startsWith('zh')) {
660
const region = appLocale.split('-')[1];
661
662
// On Windows and macOS, Chinese languages returned by
663
// app.getPreferredSystemLanguages() start with zh-hans
664
// for Simplified Chinese or zh-hant for Traditional Chinese,
665
// so we can easily determine whether to use Simplified or Traditional.
666
// However, on Linux, Chinese languages returned by that same API
667
// are of the form zh-XY, where XY is a country code.
668
// For China (CN), Singapore (SG), and Malaysia (MY)
669
// country codes, assume they use Simplified Chinese.
670
// For other cases, assume they use Traditional.
671
if (['hans', 'cn', 'sg', 'my'].includes(region)) {
672
return 'zh-cn';
673
}
674
675
return 'zh-tw';
676
}
677
678
return appLocale;
679
}
680
681
/**
682
* Resolve the NLS configuration
683
*/
684
async function resolveNlsConfiguration(): Promise<INLSConfiguration> {
685
686
// First, we need to test a user defined locale.
687
// If it fails we try the app locale.
688
// If that fails we fall back to English.
689
690
const nlsConfiguration = nlsConfigurationPromise ? await nlsConfigurationPromise : undefined;
691
if (nlsConfiguration) {
692
return nlsConfiguration;
693
}
694
695
// Try to use the app locale which is only valid
696
// after the app ready event has been fired.
697
698
let userLocale = app.getLocale();
699
if (!userLocale) {
700
return {
701
userLocale: 'en',
702
osLocale,
703
resolvedLanguage: 'en',
704
defaultMessagesFile: path.join(import.meta.dirname, 'nls.messages.json'),
705
706
// NLS: below 2 are a relic from old times only used by vscode-nls and deprecated
707
locale: 'en',
708
availableLanguages: {}
709
};
710
}
711
712
// See above the comment about the loader and case sensitiveness
713
userLocale = processZhLocale(userLocale.toLowerCase());
714
715
return resolveNLSConfiguration({
716
userLocale,
717
osLocale,
718
commit: product.commit,
719
userDataPath,
720
nlsMetadataPath: import.meta.dirname
721
});
722
}
723
724
/**
725
* Language tags are case insensitive however an ESM loader is case sensitive
726
* To make this work on case preserving & insensitive FS we do the following:
727
* the language bundles have lower case language tags and we always lower case
728
* the locale we receive from the user or OS.
729
*/
730
function getUserDefinedLocale(argvConfig: IArgvConfig): string | undefined {
731
const locale = args['locale'];
732
if (locale) {
733
return locale.toLowerCase(); // a directly provided --locale always wins
734
}
735
736
return typeof argvConfig?.locale === 'string' ? argvConfig.locale.toLowerCase() : undefined;
737
}
738
739
//#endregion
740
741