Repository navigation
Expand file tree
/
Copy pathinterpreterPathService.ts
More file actions
144 lines (135 loc) · 6.41 KB
/
Copy pathinterpreterPathService.ts
File metadata and controls
144 lines (135 loc) · 6.41 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as fs from 'fs-extra';
import { inject, injectable } from 'inversify';
import { ConfigurationChangeEvent, ConfigurationTarget, Event, EventEmitter, Uri } from 'vscode';
import { traceError } from '../logging';
import { IWorkspaceService } from './application/types';
import { PythonSettings } from './configSettings';
import { isTestExecution } from './constants';
import { FileSystemPaths } from './platform/fs-paths';
import {
IDisposable,
IDisposableRegistry,
IInterpreterPathService,
InspectInterpreterSettingType,
InterpreterConfigurationScope,
IPersistentState,
IPersistentStateFactory,
IPythonSettings,
Resource,
} from './types';
import { SystemVariables } from './variables/systemVariables';
export const defaultInterpreterPathSetting: keyof IPythonSettings = 'defaultInterpreterPath';
const CI_PYTHON_PATH = getCIPythonPath();
function getCIPythonPath(): string {
if (process.env.CI_PYTHON_PATH && fs.existsSync(process.env.CI_PYTHON_PATH)) {
return process.env.CI_PYTHON_PATH;
}
return 'python';
}
@injectable()
export class InterpreterPathService implements IInterpreterPathService {
public get onDidChange(): Event<InterpreterConfigurationScope> {
return this._didChangeInterpreterEmitter.event;
}
public _didChangeInterpreterEmitter = new EventEmitter<InterpreterConfigurationScope>();
private fileSystemPaths: FileSystemPaths;
constructor(
@inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
@inject(IDisposableRegistry) disposables: IDisposable[],
) {
disposables.push(this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)));
this.fileSystemPaths = FileSystemPaths.withDefaults();
}
public async onDidChangeConfiguration(event: ConfigurationChangeEvent) {
if (event.affectsConfiguration(`python.${defaultInterpreterPathSetting}`)) {
this._didChangeInterpreterEmitter.fire({ uri: undefined, configTarget: ConfigurationTarget.Global });
}
}
public inspect(resource: Resource): InspectInterpreterSettingType {
resource = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService).uri;
let workspaceFolderSetting: IPersistentState<string | undefined> | undefined;
let workspaceSetting: IPersistentState<string | undefined> | undefined;
if (resource) {
workspaceFolderSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
this.getSettingKey(resource, ConfigurationTarget.WorkspaceFolder),
undefined,
);
workspaceSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
this.getSettingKey(resource, ConfigurationTarget.Workspace),
undefined,
);
}
const defaultInterpreterPath: InspectInterpreterSettingType =
this.workspaceService.getConfiguration('python', resource)?.inspect<string>('defaultInterpreterPath') ?? {};
return {
globalValue: defaultInterpreterPath.globalValue,
workspaceFolderValue: workspaceFolderSetting?.value || defaultInterpreterPath.workspaceFolderValue,
workspaceValue: workspaceSetting?.value || defaultInterpreterPath.workspaceValue,
};
}
public get(resource: Resource): string {
const settings = this.inspect(resource);
const value =
settings.workspaceFolderValue ||
settings.workspaceValue ||
settings.globalValue ||
(isTestExecution() ? CI_PYTHON_PATH : 'python');
const systemVariables = new SystemVariables(
undefined,
this.workspaceService.getWorkspaceFolder(resource)?.uri.fsPath,
this.workspaceService,
);
return systemVariables.resolveAny(value)!;
}
public async update(
resource: Resource,
configTarget: ConfigurationTarget,
pythonPath: string | undefined,
): Promise<void> {
resource = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService).uri;
if (configTarget === ConfigurationTarget.Global) {
const pythonConfig = this.workspaceService.getConfiguration('python');
const globalValue = pythonConfig.inspect<string>('defaultInterpreterPath')!.globalValue;
if (globalValue !== pythonPath) {
await pythonConfig.update('defaultInterpreterPath', pythonPath, true);
this._didChangeInterpreterEmitter.fire({ uri: undefined, configTarget });
}
return;
}
if (!resource) {
traceError('Cannot update workspace settings as no workspace is opened');
return;
}
const settingKey = this.getSettingKey(resource, configTarget);
const persistentSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
settingKey,
undefined,
);
if (persistentSetting.value !== pythonPath) {
await persistentSetting.updateValue(pythonPath);
this._didChangeInterpreterEmitter.fire({ uri: resource, configTarget });
}
}
public getSettingKey(
resource: Uri,
configTarget: ConfigurationTarget.Workspace | ConfigurationTarget.WorkspaceFolder,
): string {
let settingKey: string;
const folderKey = this.workspaceService.getWorkspaceFolderIdentifier(resource);
if (configTarget === ConfigurationTarget.WorkspaceFolder) {
settingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${folderKey}`;
} else {
settingKey = this.workspaceService.workspaceFile
? `WORKSPACE_INTERPRETER_PATH_${this.fileSystemPaths.normCase(
this.workspaceService.workspaceFile.fsPath,
)}`
: // Only a single folder is opened, use fsPath of the folder as key
`WORKSPACE_FOLDER_INTERPRETER_PATH_${folderKey}`;
}
return settingKey;
}
}