forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconfigSettings.ts
More file actions
564 lines (484 loc) · 21.8 KB
/
configSettings.ts
File metadata and controls
564 lines (484 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
'use strict';
// eslint-disable-next-line camelcase
import * as path from 'path';
import * as fs from 'fs';
import {
ConfigurationChangeEvent,
ConfigurationTarget,
Disposable,
Event,
EventEmitter,
Uri,
WorkspaceConfiguration,
} from 'vscode';
import { LanguageServerType } from '../activation/types';
import './extensions';
import { IInterpreterAutoSelectionProxyService } from '../interpreter/autoSelection/types';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import { sendSettingTelemetry } from '../telemetry/envFileTelemetry';
import { ITestingSettings } from '../testing/configuration/types';
import { IWorkspaceService } from './application/types';
import { WorkspaceService } from './application/workspace';
import { DEFAULT_INTERPRETER_SETTING, isTestExecution, PYREFLY_EXTENSION_ID } from './constants';
import {
IAutoCompleteSettings,
IDefaultLanguageServer,
IExperiments,
IExtensions,
IInterpreterPathService,
IInterpreterSettings,
IPythonSettings,
IREPLSettings,
ITerminalSettings,
Resource,
} from './types';
import { debounceSync } from './utils/decorators';
import { SystemVariables } from './variables/systemVariables';
import { getOSType, OSType, isWindows } from './utils/platform';
import { untildify } from './helpers';
export class PythonSettings implements IPythonSettings {
private get onDidChange(): Event<ConfigurationChangeEvent | undefined> {
return this.changed.event;
}
// eslint-disable-next-line class-methods-use-this
public static onConfigChange(): Event<ConfigurationChangeEvent | undefined> {
return PythonSettings.configChanged.event;
}
public get pythonPath(): string {
return this._pythonPath;
}
public set pythonPath(value: string) {
if (this._pythonPath === value) {
return;
}
// Add support for specifying just the directory where the python executable will be located.
// E.g. virtual directory name.
try {
this._pythonPath = this.getPythonExecutable(value);
} catch (ex) {
this._pythonPath = value;
}
}
public get defaultInterpreterPath(): string {
return this._defaultInterpreterPath;
}
public set defaultInterpreterPath(value: string) {
if (this._defaultInterpreterPath === value) {
return;
}
// Add support for specifying just the directory where the python executable will be located.
// E.g. virtual directory name.
try {
this._defaultInterpreterPath = this.getPythonExecutable(value);
} catch (ex) {
this._defaultInterpreterPath = value;
}
}
private static pythonSettings: Map<string, PythonSettings> = new Map<string, PythonSettings>();
public envFile = '';
public venvPath = '';
public interpreter!: IInterpreterSettings;
public venvFolders: string[] = [];
public activeStateToolPath = '';
public condaPath = '';
public pipenvPath = '';
public poetryPath = '';
public pixiToolPath = '';
public devOptions: string[] = [];
public autoComplete!: IAutoCompleteSettings;
public testing!: ITestingSettings;
public terminal!: ITerminalSettings;
public globalModuleInstallation = false;
public REPL!: IREPLSettings;
public experiments!: IExperiments;
public languageServer: LanguageServerType = LanguageServerType.Node;
public languageServerIsDefault = true;
protected readonly changed = new EventEmitter<ConfigurationChangeEvent | undefined>();
private static readonly configChanged = new EventEmitter<ConfigurationChangeEvent | undefined>();
private workspaceRoot: Resource;
private disposables: Disposable[] = [];
private _pythonPath = 'python';
private _defaultInterpreterPath = '';
private readonly workspace: IWorkspaceService;
constructor(
workspaceFolder: Resource,
private readonly interpreterAutoSelectionService: IInterpreterAutoSelectionProxyService,
workspace: IWorkspaceService,
private readonly interpreterPathService: IInterpreterPathService,
private readonly defaultLS: IDefaultLanguageServer | undefined,
private readonly extensions: IExtensions,
) {
this.workspace = workspace || new WorkspaceService();
this.workspaceRoot = workspaceFolder;
this.initialize();
}
public static getInstance(
resource: Uri | undefined,
interpreterAutoSelectionService: IInterpreterAutoSelectionProxyService,
workspace: IWorkspaceService,
interpreterPathService: IInterpreterPathService,
defaultLS: IDefaultLanguageServer | undefined,
extensions: IExtensions,
): PythonSettings {
workspace = workspace || new WorkspaceService();
const workspaceFolderUri = PythonSettings.getSettingsUriAndTarget(resource, workspace).uri;
const workspaceFolderKey = workspaceFolderUri ? workspaceFolderUri.fsPath : '';
if (!PythonSettings.pythonSettings.has(workspaceFolderKey)) {
const settings = new PythonSettings(
workspaceFolderUri,
interpreterAutoSelectionService,
workspace,
interpreterPathService,
defaultLS,
extensions,
);
PythonSettings.pythonSettings.set(workspaceFolderKey, settings);
settings.onDidChange((event) => PythonSettings.debounceConfigChangeNotification(event));
// Pass null to avoid VSC from complaining about not passing in a value.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const config = workspace.getConfiguration('editor', resource || (null as any));
const formatOnType = config ? config.get('formatOnType', false) : false;
sendTelemetryEvent(EventName.FORMAT_ON_TYPE, undefined, { enabled: formatOnType });
}
return PythonSettings.pythonSettings.get(workspaceFolderKey)!;
}
@debounceSync(1)
// eslint-disable-next-line class-methods-use-this
protected static debounceConfigChangeNotification(event?: ConfigurationChangeEvent): void {
PythonSettings.configChanged.fire(event);
}
public static getSettingsUriAndTarget(
resource: Uri | undefined,
workspace?: IWorkspaceService,
): { uri: Uri | undefined; target: ConfigurationTarget } {
workspace = workspace || new WorkspaceService();
const workspaceFolder = resource ? workspace.getWorkspaceFolder(resource) : undefined;
let workspaceFolderUri: Uri | undefined = workspaceFolder ? workspaceFolder.uri : undefined;
if (!workspaceFolderUri && Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0) {
workspaceFolderUri = workspace.workspaceFolders[0].uri;
}
const target = workspaceFolderUri ? ConfigurationTarget.WorkspaceFolder : ConfigurationTarget.Global;
return { uri: workspaceFolderUri, target };
}
public static dispose(): void {
if (!isTestExecution()) {
throw new Error('Dispose can only be called from unit tests');
}
PythonSettings.pythonSettings.forEach((item) => item && item.dispose());
PythonSettings.pythonSettings.clear();
}
public static toSerializable(settings: IPythonSettings): IPythonSettings {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const clone: any = {};
const keys = Object.entries(settings);
keys.forEach((e) => {
const [k, v] = e;
if (!k.includes('Manager') && !k.includes('Service') && !k.includes('onDid')) {
clone[k] = v;
}
});
return clone as IPythonSettings;
}
public dispose(): void {
this.disposables.forEach((disposable) => disposable && disposable.dispose());
this.disposables = [];
}
protected update(pythonSettings: WorkspaceConfiguration): void {
const workspaceRoot = this.workspaceRoot?.fsPath;
const systemVariables: SystemVariables = new SystemVariables(undefined, workspaceRoot, this.workspace);
this.pythonPath = this.getPythonPath(systemVariables, workspaceRoot);
const defaultInterpreterPath = systemVariables.resolveAny(pythonSettings.get<string>('defaultInterpreterPath'));
this.defaultInterpreterPath = defaultInterpreterPath || DEFAULT_INTERPRETER_SETTING;
if (this.defaultInterpreterPath === DEFAULT_INTERPRETER_SETTING) {
const autoSelectedPythonInterpreter = this.interpreterAutoSelectionService.getAutoSelectedInterpreter(
this.workspaceRoot,
);
this.defaultInterpreterPath = autoSelectedPythonInterpreter?.path ?? this.defaultInterpreterPath;
}
this.defaultInterpreterPath = getAbsolutePath(this.defaultInterpreterPath, workspaceRoot);
this.venvPath = systemVariables.resolveAny(pythonSettings.get<string>('venvPath'))!;
this.venvFolders = systemVariables.resolveAny(pythonSettings.get<string[]>('venvFolders'))!;
const activeStateToolPath = systemVariables.resolveAny(pythonSettings.get<string>('activeStateToolPath'))!;
this.activeStateToolPath =
activeStateToolPath && activeStateToolPath.length > 0
? getAbsolutePath(activeStateToolPath, workspaceRoot)
: activeStateToolPath;
const condaPath = systemVariables.resolveAny(pythonSettings.get<string>('condaPath'))!;
this.condaPath = condaPath && condaPath.length > 0 ? getAbsolutePath(condaPath, workspaceRoot) : condaPath;
const pipenvPath = systemVariables.resolveAny(pythonSettings.get<string>('pipenvPath'))!;
this.pipenvPath = pipenvPath && pipenvPath.length > 0 ? getAbsolutePath(pipenvPath, workspaceRoot) : pipenvPath;
const poetryPath = systemVariables.resolveAny(pythonSettings.get<string>('poetryPath'))!;
this.poetryPath = poetryPath && poetryPath.length > 0 ? getAbsolutePath(poetryPath, workspaceRoot) : poetryPath;
const pixiToolPath = systemVariables.resolveAny(pythonSettings.get<string>('pixiToolPath'))!;
this.pixiToolPath =
pixiToolPath && pixiToolPath.length > 0 ? getAbsolutePath(pixiToolPath, workspaceRoot) : pixiToolPath;
this.interpreter = pythonSettings.get<IInterpreterSettings>('interpreter') ?? {
infoVisibility: 'onPythonRelated',
};
// Get as a string and verify; don't just accept.
let userLS = pythonSettings.get<string>('languageServer');
userLS = systemVariables.resolveAny(userLS);
// Validate the user's input; if invalid, set it to the default.
if (
!userLS ||
userLS === 'Default' ||
userLS === 'Microsoft' ||
!Object.values(LanguageServerType).includes(userLS as LanguageServerType)
) {
if (
this.extensions.getExtension(PYREFLY_EXTENSION_ID) &&
pythonSettings.get<boolean>('pyrefly.disableLanguageServices') !== true
) {
this.languageServer = LanguageServerType.None;
} else {
this.languageServer = this.defaultLS?.defaultLSType ?? LanguageServerType.None;
}
this.languageServerIsDefault = true;
} else if (userLS === 'JediLSP') {
// Switch JediLSP option to Jedi.
this.languageServer = LanguageServerType.Jedi;
this.languageServerIsDefault = false;
} else {
this.languageServer = userLS as LanguageServerType;
this.languageServerIsDefault = false;
}
const autoCompleteSettings = systemVariables.resolveAny(
pythonSettings.get<IAutoCompleteSettings>('autoComplete'),
)!;
if (this.autoComplete) {
Object.assign<IAutoCompleteSettings, IAutoCompleteSettings>(this.autoComplete, autoCompleteSettings);
} else {
this.autoComplete = autoCompleteSettings;
}
const envFileSetting = pythonSettings.get<string>('envFile');
this.envFile = systemVariables.resolveAny(envFileSetting)!;
sendSettingTelemetry(this.workspace, envFileSetting);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.devOptions = systemVariables.resolveAny(pythonSettings.get<any[]>('devOptions'))!;
this.devOptions = Array.isArray(this.devOptions) ? this.devOptions : [];
this.globalModuleInstallation = pythonSettings.get<boolean>('globalModuleInstallation') === true;
const testSettings = systemVariables.resolveAny(pythonSettings.get<ITestingSettings>('testing'))!;
if (this.testing) {
Object.assign<ITestingSettings, ITestingSettings>(this.testing, testSettings);
} else {
this.testing = testSettings;
if (isTestExecution() && !this.testing) {
this.testing = {
pytestArgs: [],
unittestArgs: [],
promptToConfigure: true,
debugPort: 3000,
pytestEnabled: false,
unittestEnabled: false,
pytestPath: 'pytest',
autoTestDiscoverOnSaveEnabled: true,
autoTestDiscoverOnSavePattern: '**/*.py',
} as ITestingSettings;
}
}
// Support for travis.
this.testing = this.testing
? this.testing
: {
promptToConfigure: true,
debugPort: 3000,
pytestArgs: [],
pytestEnabled: false,
pytestPath: 'pytest',
unittestArgs: [],
unittestEnabled: false,
autoTestDiscoverOnSaveEnabled: true,
autoTestDiscoverOnSavePattern: '**/*.py',
};
this.testing.pytestPath = getAbsolutePath(systemVariables.resolveAny(this.testing.pytestPath), workspaceRoot);
if (this.testing.cwd) {
this.testing.cwd = getAbsolutePath(systemVariables.resolveAny(this.testing.cwd), workspaceRoot);
}
// Resolve any variables found in the test arguments.
this.testing.pytestArgs = this.testing.pytestArgs.map((arg) => systemVariables.resolveAny(arg));
this.testing.unittestArgs = this.testing.unittestArgs.map((arg) => systemVariables.resolveAny(arg));
const terminalSettings = systemVariables.resolveAny(pythonSettings.get<ITerminalSettings>('terminal'))!;
if (this.terminal) {
Object.assign<ITerminalSettings, ITerminalSettings>(this.terminal, terminalSettings);
} else {
this.terminal = terminalSettings;
if (isTestExecution() && !this.terminal) {
this.terminal = {} as ITerminalSettings;
}
}
// Support for travis.
this.terminal = this.terminal
? this.terminal
: {
executeInFileDir: true,
focusAfterLaunch: false,
launchArgs: [],
activateEnvironment: true,
activateEnvInCurrentTerminal: false,
shellIntegration: {
enabled: false,
activate: false,
},
};
this.REPL = pythonSettings.get<IREPLSettings>('REPL')!;
const experiments = pythonSettings.get<IExperiments>('experiments')!;
if (this.experiments) {
Object.assign<IExperiments, IExperiments>(this.experiments, experiments);
} else {
this.experiments = experiments;
}
// Note we directly access experiment settings using workspace service in ExperimentService class.
// Any changes here specific to these settings should propogate their as well.
this.experiments = this.experiments
? this.experiments
: {
enabled: true,
optInto: [],
optOutFrom: [],
};
}
// eslint-disable-next-line class-methods-use-this
protected getPythonExecutable(pythonPath: string): string {
return getPythonExecutable(pythonPath);
}
protected onWorkspaceFoldersChanged(): void {
// If an activated workspace folder was removed, delete its key
const workspaceKeys = this.workspace.workspaceFolders!.map((workspaceFolder) => workspaceFolder.uri.fsPath);
const activatedWkspcKeys = Array.from(PythonSettings.pythonSettings.keys());
const activatedWkspcFoldersRemoved = activatedWkspcKeys.filter((item) => workspaceKeys.indexOf(item) < 0);
if (activatedWkspcFoldersRemoved.length > 0) {
for (const folder of activatedWkspcFoldersRemoved) {
PythonSettings.pythonSettings.delete(folder);
}
}
}
public register(): void {
PythonSettings.pythonSettings = new Map();
this.initialize();
}
private onDidChanged(event?: ConfigurationChangeEvent) {
const currentConfig = this.workspace.getConfiguration('python', this.workspaceRoot);
this.update(currentConfig);
// If workspace config changes, then we could have a cascading effect of on change events.
// Let's defer the change notification.
this.debounceChangeNotification(event);
}
public initialize(): void {
this.disposables.push(this.workspace.onDidChangeWorkspaceFolders(this.onWorkspaceFoldersChanged, this));
this.disposables.push(
this.interpreterAutoSelectionService.onDidChangeAutoSelectedInterpreter(() => {
this.onDidChanged();
}),
);
this.disposables.push(
this.workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration('python')) {
this.onDidChanged(event);
}
}),
);
if (this.interpreterPathService) {
this.disposables.push(
this.interpreterPathService.onDidChange(() => {
this.onDidChanged();
}),
);
}
const initialConfig = this.workspace.getConfiguration('python', this.workspaceRoot);
if (initialConfig) {
this.update(initialConfig);
}
}
@debounceSync(1)
protected debounceChangeNotification(event?: ConfigurationChangeEvent): void {
this.changed.fire(event);
}
private getPythonPath(systemVariables: SystemVariables, workspaceRoot: string | undefined) {
this.pythonPath = systemVariables.resolveAny(this.interpreterPathService.get(this.workspaceRoot))!;
if (
!process.env.CI_DISABLE_AUTO_SELECTION &&
(this.pythonPath.length === 0 || this.pythonPath === 'python') &&
this.interpreterAutoSelectionService
) {
const autoSelectedPythonInterpreter = this.interpreterAutoSelectionService.getAutoSelectedInterpreter(
this.workspaceRoot,
);
if (autoSelectedPythonInterpreter) {
this.pythonPath = autoSelectedPythonInterpreter.path;
if (this.workspaceRoot) {
this.interpreterAutoSelectionService
.setWorkspaceInterpreter(this.workspaceRoot, autoSelectedPythonInterpreter)
.ignoreErrors();
}
}
}
return getAbsolutePath(this.pythonPath, workspaceRoot);
}
}
function getAbsolutePath(pathToCheck: string, rootDir: string | undefined): string {
if (!rootDir) {
rootDir = __dirname;
}
pathToCheck = untildify(pathToCheck) as string;
if (isTestExecution() && !pathToCheck) {
return rootDir;
}
if (pathToCheck.indexOf(path.sep) === -1) {
return pathToCheck;
}
return path.isAbsolute(pathToCheck) ? pathToCheck : path.resolve(rootDir, pathToCheck);
}
function getPythonExecutable(pythonPath: string): string {
pythonPath = untildify(pythonPath) as string;
// If only 'python'.
if (
pythonPath === 'python' ||
pythonPath.indexOf(path.sep) === -1 ||
path.basename(pythonPath) === path.dirname(pythonPath)
) {
return pythonPath;
}
if (isValidPythonPath(pythonPath)) {
return pythonPath;
}
// Keep python right on top, for backwards compatibility.
const KnownPythonExecutables = [
'python',
'python4',
'python3.6',
'python3.5',
'python3',
'python2.7',
'python2',
'python3.7',
'python3.8',
'python3.9',
];
for (let executableName of KnownPythonExecutables) {
// Suffix with 'python' for linux and 'osx', and 'python.exe' for 'windows'.
if (isWindows()) {
executableName = `${executableName}.exe`;
if (isValidPythonPath(path.join(pythonPath, executableName))) {
return path.join(pythonPath, executableName);
}
if (isValidPythonPath(path.join(pythonPath, 'Scripts', executableName))) {
return path.join(pythonPath, 'Scripts', executableName);
}
} else {
if (isValidPythonPath(path.join(pythonPath, executableName))) {
return path.join(pythonPath, executableName);
}
if (isValidPythonPath(path.join(pythonPath, 'bin', executableName))) {
return path.join(pythonPath, 'bin', executableName);
}
}
}
return pythonPath;
}
function isValidPythonPath(pythonPath: string): boolean {
return (
fs.existsSync(pythonPath) &&
path.basename(getOSType() === OSType.Windows ? pythonPath.toLowerCase() : pythonPath).startsWith('python')
);
}