Skip to content

Commit c1ed78f

Browse files
author
Kartik Raj
committed
Fix bug
1 parent dc2b505 commit c1ed78f

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

src/client/common/interpreterPathService.ts

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ConfigurationChangeEvent, ConfigurationTarget, Event, EventEmitter, Uri
99
import { IWorkspaceService } from './application/types';
1010
import { PythonSettings } from './configSettings';
1111
import { isTestExecution } from './constants';
12+
import { FileSystemPaths } from './platform/fs-paths';
1213
import {
1314
IDisposable,
1415
IDisposableRegistry,
@@ -22,6 +23,8 @@ import {
2223
} from './types';
2324

2425
export const workspaceKeysForWhichTheCopyIsDone_Key = 'workspaceKeysForWhichTheCopyIsDone_Key';
26+
export const workspaceFolderKeysForWhichTheCopyIsDone_Key = 'workspaceFolderKeysForWhichTheCopyIsDone_Key';
27+
export const isGlobalSettingCopiedKey = 'isGlobalSettingCopiedKey';
2528
export const defaultInterpreterPathSetting: keyof IPythonSettings = 'defaultInterpreterPath';
2629
const CI_PYTHON_PATH = getCIPythonPath();
2730

@@ -33,13 +36,18 @@ export function getCIPythonPath(): string {
3336
}
3437
@injectable()
3538
export class InterpreterPathService implements IInterpreterPathService {
39+
public get onDidChange(): Event<InterpreterConfigurationScope> {
40+
return this._didChangeInterpreterEmitter.event;
41+
}
3642
public _didChangeInterpreterEmitter = new EventEmitter<InterpreterConfigurationScope>();
43+
private fileSystemPaths: FileSystemPaths;
3744
constructor(
3845
@inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory,
3946
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
4047
@inject(IDisposableRegistry) disposables: IDisposable[]
4148
) {
4249
disposables.push(this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)));
50+
this.fileSystemPaths = FileSystemPaths.withDefaults();
4351
}
4452

4553
public async onDidChangeConfiguration(event: ConfigurationChangeEvent) {
@@ -110,10 +118,6 @@ export class InterpreterPathService implements IInterpreterPathService {
110118
}
111119
}
112120

113-
public get onDidChange(): Event<InterpreterConfigurationScope> {
114-
return this._didChangeInterpreterEmitter.event;
115-
}
116-
117121
public getSettingKey(
118122
resource: Uri,
119123
configTarget: ConfigurationTarget.Workspace | ConfigurationTarget.WorkspaceFolder
@@ -124,7 +128,9 @@ export class InterpreterPathService implements IInterpreterPathService {
124128
settingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${folderKey}`;
125129
} else {
126130
settingKey = this.workspaceService.workspaceFile
127-
? `WORKSPACE_INTERPRETER_PATH_${this.workspaceService.workspaceFile.fsPath}`
131+
? `WORKSPACE_INTERPRETER_PATH_${this.fileSystemPaths.normCase(
132+
this.workspaceService.workspaceFile.fsPath
133+
)}`
128134
: // Only a single folder is opened, use fsPath of the folder as key
129135
`WORKSPACE_FOLDER_INTERPRETER_PATH_${folderKey}`;
130136
}
@@ -133,23 +139,48 @@ export class InterpreterPathService implements IInterpreterPathService {
133139

134140
public async copyOldInterpreterStorageValuesToNew(resource: Resource): Promise<void> {
135141
resource = PythonSettings.getSettingsUriAndTarget(resource, this.workspaceService).uri;
136-
const workspaceKey = this.workspaceService.getWorkspaceFolderIdentifier(resource);
142+
const workspaceConfig = this.workspaceService.getConfiguration('python', resource);
143+
const oldSettings = workspaceConfig.inspect<string>('pythonPath')!;
144+
145+
// Copy workspace folder setting into the new storage if it hasn't been copied already
146+
const workspaceFolderKey = this.workspaceService.getWorkspaceFolderIdentifier(resource);
147+
const flaggedWorkspaceFolderKeysStorage = this.persistentStateFactory.createGlobalPersistentState<string[]>(
148+
workspaceFolderKeysForWhichTheCopyIsDone_Key,
149+
[]
150+
);
151+
const flaggedWorkspaceFolderKeys = flaggedWorkspaceFolderKeysStorage.value;
152+
const shouldUpdateWorkspaceFolderSetting = !flaggedWorkspaceFolderKeys.includes(workspaceFolderKey);
153+
if (shouldUpdateWorkspaceFolderSetting) {
154+
await this.update(resource, ConfigurationTarget.WorkspaceFolder, oldSettings.workspaceFolderValue);
155+
await flaggedWorkspaceFolderKeysStorage.updateValue([workspaceFolderKey, ...flaggedWorkspaceFolderKeys]);
156+
}
157+
158+
// Copy workspace setting into the new storage if it hasn't been copied already
159+
const workspaceKey = this.workspaceService.workspaceFile
160+
? this.fileSystemPaths.normCase(this.workspaceService.workspaceFile.fsPath)
161+
: undefined;
137162
const flaggedWorkspaceKeysStorage = this.persistentStateFactory.createGlobalPersistentState<string[]>(
138163
workspaceKeysForWhichTheCopyIsDone_Key,
139164
[]
140165
);
141166
const flaggedWorkspaceKeys = flaggedWorkspaceKeysStorage.value;
142-
if (flaggedWorkspaceKeys.includes(workspaceKey)) {
143-
// Only do a one-off import for each workspace
144-
return;
145-
} else {
167+
const shouldUpdateWorkspaceSetting = workspaceKey && !flaggedWorkspaceKeys.includes(workspaceKey);
168+
if (workspaceKey && shouldUpdateWorkspaceSetting) {
169+
await this.update(resource, ConfigurationTarget.Workspace, oldSettings.workspaceValue);
146170
await flaggedWorkspaceKeysStorage.updateValue([workspaceKey, ...flaggedWorkspaceKeys]);
147171
}
148-
const oldSettings = this.workspaceService.getConfiguration('python', resource)!.inspect<string>('pythonPath')!;
149-
await Promise.all([
150-
this.update(resource, ConfigurationTarget.WorkspaceFolder, oldSettings.workspaceFolderValue),
151-
this.update(resource, ConfigurationTarget.Workspace, oldSettings.workspaceValue),
152-
this.update(undefined, ConfigurationTarget.Global, oldSettings.globalValue)
153-
]);
172+
173+
// Move global setting into the new storage if it hasn't been moved already
174+
const isGlobalSettingCopiedStorage = this.persistentStateFactory.createGlobalPersistentState<boolean>(
175+
isGlobalSettingCopiedKey,
176+
false
177+
);
178+
const shouldUpdateGlobalSetting = !isGlobalSettingCopiedStorage.value;
179+
if (shouldUpdateGlobalSetting) {
180+
await this.update(undefined, ConfigurationTarget.Global, oldSettings.globalValue);
181+
// Make sure to delete the original setting after copying it
182+
await workspaceConfig.update('pythonPath', undefined, ConfigurationTarget.Global);
183+
await isGlobalSettingCopiedStorage.updateValue(true);
184+
}
154185
}
155186
}

src/test/common/interpreterPathService.unit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
import { InterpreterConfigurationScope, IPersistentState, IPersistentStateFactory } from '../../client/common/types';
2424
import { createDeferred, sleep } from '../../client/common/utils/async';
2525

26-
suite('Interpreter Path Service', async () => {
26+
suite('xInterpreter Path Service', async () => {
2727
let interpreterPathService: InterpreterPathService;
2828
let persistentStateFactory: TypeMoq.IMock<IPersistentStateFactory>;
2929
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
@@ -227,7 +227,7 @@ suite('Interpreter Path Service', async () => {
227227

228228
test('Workspace settings are correctly updated in case of multiroot folders', async () => {
229229
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
230-
const expectedSettingKey = `WORKSPACE_INTERPRETER_PATH_${workspaceFileUri.fsPath}`;
230+
const expectedSettingKey = 'WORKSPACE_INTERPRETER_PATH_PATH\\TO\\WORKSPACEFILE';
231231
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
232232
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
233233
workspaceService.setup((w) => w.workspaceFile).returns(() => workspaceFileUri);
@@ -404,7 +404,7 @@ suite('Interpreter Path Service', async () => {
404404

405405
test('Inspecting settings returns as expected in case of multiroot folders', async () => {
406406
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
407-
const expectedWorkspaceSettingKey = `WORKSPACE_INTERPRETER_PATH_${workspaceFileUri.fsPath}`;
407+
const expectedWorkspaceSettingKey = 'WORKSPACE_INTERPRETER_PATH_PATH\\TO\\WORKSPACEFILE';
408408
const expectedWorkspaceFolderSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
409409
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
410410
// A workspace file is present in case of multiroot workspace folders

0 commit comments

Comments
 (0)