Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/main.ts
3285 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 && 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
// `DocumentPolicyIncludeJSCallStacksInCrashReports` - https://www.electronjs.org/docs/latest/api/web-frame-main#framecollectjavascriptcallstack-experimental
324
// `EarlyEstablishGpuChannel` - Refs https://issues.chromium.org/issues/40208065
325
// `EstablishGpuChannelAsync` - Refs https://issues.chromium.org/issues/40208065
326
const featuresToEnable =
327
`DocumentPolicyIncludeJSCallStacksInCrashReports,EarlyEstablishGpuChannel,EstablishGpuChannelAsync,${app.commandLine.getSwitchValue('enable-features')}`;
328
app.commandLine.appendSwitch('enable-features', featuresToEnable);
329
330
// Following features are disabled from the runtime:
331
// `CalculateNativeWinOcclusion` - Disable native window occlusion tracker (https://groups.google.com/a/chromium.org/g/embedder-dev/c/ZF3uHHyWLKw/m/VDN2hDXMAAAJ)
332
// `FontationsLinuxSystemFonts` - Revert to FreeType for system fonts on Linux Refs https://github.com/microsoft/vscode/issues/260391
333
const featuresToDisable =
334
`CalculateNativeWinOcclusion,FontationsLinuxSystemFonts,${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
return argvConfig;
356
}
357
358
interface IArgvConfig {
359
[key: string]: string | string[] | boolean | undefined;
360
readonly locale?: string;
361
readonly 'disable-lcd-text'?: boolean;
362
readonly 'proxy-bypass-list'?: string;
363
readonly 'disable-hardware-acceleration'?: boolean;
364
readonly 'force-color-profile'?: string;
365
readonly 'enable-crash-reporter'?: boolean;
366
readonly 'crash-reporter-id'?: string;
367
readonly 'enable-proposed-api'?: string[];
368
readonly 'log-level'?: string | string[];
369
readonly 'disable-chromium-sandbox'?: boolean;
370
readonly 'use-inmemory-secretstorage'?: boolean;
371
readonly 'enable-rdp-display-tracking'?: boolean;
372
readonly 'remote-debugging-port'?: string;
373
}
374
375
function readArgvConfigSync(): IArgvConfig {
376
377
// Read or create the argv.json config file sync before app('ready')
378
const argvConfigPath = getArgvConfigPath();
379
let argvConfig: IArgvConfig | undefined = undefined;
380
try {
381
argvConfig = parse(fs.readFileSync(argvConfigPath).toString());
382
} catch (error) {
383
if (error && error.code === 'ENOENT') {
384
createDefaultArgvConfigSync(argvConfigPath);
385
} else {
386
console.warn(`Unable to read argv.json configuration file in ${argvConfigPath}, falling back to defaults (${error})`);
387
}
388
}
389
390
// Fallback to default
391
if (!argvConfig) {
392
argvConfig = {};
393
}
394
395
return argvConfig;
396
}
397
398
function createDefaultArgvConfigSync(argvConfigPath: string): void {
399
try {
400
401
// Ensure argv config parent exists
402
const argvConfigPathDirname = path.dirname(argvConfigPath);
403
if (!fs.existsSync(argvConfigPathDirname)) {
404
fs.mkdirSync(argvConfigPathDirname);
405
}
406
407
// Default argv content
408
const defaultArgvConfigContent = [
409
'// This configuration file allows you to pass permanent command line arguments to VS Code.',
410
'// Only a subset of arguments is currently supported to reduce the likelihood of breaking',
411
'// the installation.',
412
'//',
413
'// PLEASE DO NOT CHANGE WITHOUT UNDERSTANDING THE IMPACT',
414
'//',
415
'// NOTE: Changing this file requires a restart of VS Code.',
416
'{',
417
' // Use software rendering instead of hardware accelerated rendering.',
418
' // This can help in cases where you see rendering issues in VS Code.',
419
' // "disable-hardware-acceleration": true',
420
'}'
421
];
422
423
// Create initial argv.json with default content
424
fs.writeFileSync(argvConfigPath, defaultArgvConfigContent.join('\n'));
425
} catch (error) {
426
console.error(`Unable to create argv.json configuration file in ${argvConfigPath}, falling back to defaults (${error})`);
427
}
428
}
429
430
function getArgvConfigPath(): string {
431
const vscodePortable = process.env['VSCODE_PORTABLE'];
432
if (vscodePortable) {
433
return path.join(vscodePortable, 'argv.json');
434
}
435
436
let dataFolderName = product.dataFolderName;
437
if (process.env['VSCODE_DEV']) {
438
dataFolderName = `${dataFolderName}-dev`;
439
}
440
441
return path.join(os.homedir(), dataFolderName!, 'argv.json');
442
}
443
444
function configureCrashReporter(): void {
445
let crashReporterDirectory = args['crash-reporter-directory'];
446
let submitURL = '';
447
if (crashReporterDirectory) {
448
crashReporterDirectory = path.normalize(crashReporterDirectory);
449
450
if (!path.isAbsolute(crashReporterDirectory)) {
451
console.error(`The path '${crashReporterDirectory}' specified for --crash-reporter-directory must be absolute.`);
452
app.exit(1);
453
}
454
455
if (!fs.existsSync(crashReporterDirectory)) {
456
try {
457
fs.mkdirSync(crashReporterDirectory, { recursive: true });
458
} catch (error) {
459
console.error(`The path '${crashReporterDirectory}' specified for --crash-reporter-directory does not seem to exist or cannot be created.`);
460
app.exit(1);
461
}
462
}
463
464
// Crashes are stored in the crashDumps directory by default, so we
465
// need to change that directory to the provided one
466
console.log(`Found --crash-reporter-directory argument. Setting crashDumps directory to be '${crashReporterDirectory}'`);
467
app.setPath('crashDumps', crashReporterDirectory);
468
}
469
470
// Otherwise we configure the crash reporter from product.json
471
else {
472
const appCenter = product.appCenter;
473
if (appCenter) {
474
const isWindows = (process.platform === 'win32');
475
const isLinux = (process.platform === 'linux');
476
const isDarwin = (process.platform === 'darwin');
477
const crashReporterId = argvConfig['crash-reporter-id'];
478
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
479
if (crashReporterId && uuidPattern.test(crashReporterId)) {
480
if (isWindows) {
481
switch (process.arch) {
482
case 'x64':
483
submitURL = appCenter['win32-x64'];
484
break;
485
case 'arm64':
486
submitURL = appCenter['win32-arm64'];
487
break;
488
}
489
} else if (isDarwin) {
490
if (product.darwinUniversalAssetId) {
491
submitURL = appCenter['darwin-universal'];
492
} else {
493
switch (process.arch) {
494
case 'x64':
495
submitURL = appCenter['darwin'];
496
break;
497
case 'arm64':
498
submitURL = appCenter['darwin-arm64'];
499
break;
500
}
501
}
502
} else if (isLinux) {
503
submitURL = appCenter['linux-x64'];
504
}
505
submitURL = submitURL.concat('&uid=', crashReporterId, '&iid=', crashReporterId, '&sid=', crashReporterId);
506
// Send the id for child node process that are explicitly starting crash reporter.
507
// For vscode this is ExtensionHost process currently.
508
const argv = process.argv;
509
const endOfArgsMarkerIndex = argv.indexOf('--');
510
if (endOfArgsMarkerIndex === -1) {
511
argv.push('--crash-reporter-id', crashReporterId);
512
} else {
513
// if the we have an argument "--" (end of argument marker)
514
// we cannot add arguments at the end. rather, we add
515
// arguments before the "--" marker.
516
argv.splice(endOfArgsMarkerIndex, 0, '--crash-reporter-id', crashReporterId);
517
}
518
}
519
}
520
}
521
522
// Start crash reporter for all processes
523
const productName = (product.crashReporter ? product.crashReporter.productName : undefined) || product.nameShort;
524
const companyName = (product.crashReporter ? product.crashReporter.companyName : undefined) || 'Microsoft';
525
const uploadToServer = Boolean(!process.env['VSCODE_DEV'] && submitURL && !crashReporterDirectory);
526
crashReporter.start({
527
companyName,
528
productName: process.env['VSCODE_DEV'] ? `${productName} Dev` : productName,
529
submitURL,
530
uploadToServer,
531
compress: true
532
});
533
}
534
535
function getJSFlags(cliArgs: NativeParsedArgs): string | null {
536
const jsFlags: string[] = [];
537
538
// Add any existing JS flags we already got from the command line
539
if (cliArgs['js-flags']) {
540
jsFlags.push(cliArgs['js-flags']);
541
}
542
543
if (process.platform === 'linux') {
544
// Fix cppgc crash on Linux with 16KB page size.
545
// Refs https://issues.chromium.org/issues/378017037
546
// The fix from https://github.com/electron/electron/commit/6c5b2ef55e08dc0bede02384747549c1eadac0eb
547
// only affects non-renderer process.
548
// The following will ensure that the flag will be
549
// applied to the renderer process as well.
550
// TODO(deepak1556): Remove this once we update to
551
// Chromium >= 134.
552
jsFlags.push('--nodecommit_pooled_pages');
553
}
554
555
return jsFlags.length > 0 ? jsFlags.join(' ') : null;
556
}
557
558
function parseCLIArgs(): NativeParsedArgs {
559
return minimist(process.argv, {
560
string: [
561
'user-data-dir',
562
'locale',
563
'js-flags',
564
'crash-reporter-directory'
565
],
566
boolean: [
567
'disable-chromium-sandbox',
568
],
569
default: {
570
'sandbox': true
571
},
572
alias: {
573
'no-sandbox': 'sandbox'
574
}
575
});
576
}
577
578
function registerListeners(): void {
579
580
/**
581
* macOS: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
582
* the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
583
*/
584
const macOpenFiles: string[] = [];
585
(globalThis as any)['macOpenFiles'] = macOpenFiles;
586
app.on('open-file', function (event, path) {
587
macOpenFiles.push(path);
588
});
589
590
/**
591
* macOS: react to open-url requests.
592
*/
593
const openUrls: string[] = [];
594
const onOpenUrl =
595
function (event: { preventDefault: () => void }, url: string) {
596
event.preventDefault();
597
598
openUrls.push(url);
599
};
600
601
app.on('will-finish-launching', function () {
602
app.on('open-url', onOpenUrl);
603
});
604
605
(globalThis as any)['getOpenUrls'] = function () {
606
app.removeListener('open-url', onOpenUrl);
607
608
return openUrls;
609
};
610
}
611
612
function getCodeCachePath(): string | undefined {
613
614
// explicitly disabled via CLI args
615
if (process.argv.indexOf('--no-cached-data') > 0) {
616
return undefined;
617
}
618
619
// running out of sources
620
if (process.env['VSCODE_DEV']) {
621
return undefined;
622
}
623
624
// require commit id
625
const commit = product.commit;
626
if (!commit) {
627
return undefined;
628
}
629
630
return path.join(userDataPath, 'CachedData', commit);
631
}
632
633
async function mkdirpIgnoreError(dir: string | undefined): Promise<string | undefined> {
634
if (typeof dir === 'string') {
635
try {
636
await fs.promises.mkdir(dir, { recursive: true });
637
638
return dir;
639
} catch (error) {
640
// ignore
641
}
642
}
643
644
return undefined;
645
}
646
647
//#region NLS Support
648
649
function processZhLocale(appLocale: string): string {
650
if (appLocale.startsWith('zh')) {
651
const region = appLocale.split('-')[1];
652
653
// On Windows and macOS, Chinese languages returned by
654
// app.getPreferredSystemLanguages() start with zh-hans
655
// for Simplified Chinese or zh-hant for Traditional Chinese,
656
// so we can easily determine whether to use Simplified or Traditional.
657
// However, on Linux, Chinese languages returned by that same API
658
// are of the form zh-XY, where XY is a country code.
659
// For China (CN), Singapore (SG), and Malaysia (MY)
660
// country codes, assume they use Simplified Chinese.
661
// For other cases, assume they use Traditional.
662
if (['hans', 'cn', 'sg', 'my'].includes(region)) {
663
return 'zh-cn';
664
}
665
666
return 'zh-tw';
667
}
668
669
return appLocale;
670
}
671
672
/**
673
* Resolve the NLS configuration
674
*/
675
async function resolveNlsConfiguration(): Promise<INLSConfiguration> {
676
677
// First, we need to test a user defined locale.
678
// If it fails we try the app locale.
679
// If that fails we fall back to English.
680
681
const nlsConfiguration = nlsConfigurationPromise ? await nlsConfigurationPromise : undefined;
682
if (nlsConfiguration) {
683
return nlsConfiguration;
684
}
685
686
// Try to use the app locale which is only valid
687
// after the app ready event has been fired.
688
689
let userLocale = app.getLocale();
690
if (!userLocale) {
691
return {
692
userLocale: 'en',
693
osLocale,
694
resolvedLanguage: 'en',
695
defaultMessagesFile: path.join(import.meta.dirname, 'nls.messages.json'),
696
697
// NLS: below 2 are a relic from old times only used by vscode-nls and deprecated
698
locale: 'en',
699
availableLanguages: {}
700
};
701
}
702
703
// See above the comment about the loader and case sensitiveness
704
userLocale = processZhLocale(userLocale.toLowerCase());
705
706
return resolveNLSConfiguration({
707
userLocale,
708
osLocale,
709
commit: product.commit,
710
userDataPath,
711
nlsMetadataPath: import.meta.dirname
712
});
713
}
714
715
/**
716
* Language tags are case insensitive however an ESM loader is case sensitive
717
* To make this work on case preserving & insensitive FS we do the following:
718
* the language bundles have lower case language tags and we always lower case
719
* the locale we receive from the user or OS.
720
*/
721
function getUserDefinedLocale(argvConfig: IArgvConfig): string | undefined {
722
const locale = args['locale'];
723
if (locale) {
724
return locale.toLowerCase(); // a directly provided --locale always wins
725
}
726
727
return typeof argvConfig?.locale === 'string' ? argvConfig.locale.toLowerCase() : undefined;
728
}
729
730
//#endregion
731
732