Skip to content

Commit 4d027f5

Browse files
author
Kartik Raj
authored
Ensure extension features are started when in Deprecate PythonPath experiment and opening a file without any folder opened (#12182)
* Ensure extension features are started when in Deprecate PythonPath experiment and opening a file without any folder opened. * Added comments
1 parent 015ce33 commit 4d027f5

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

news/2 Fixes/12177.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure extension features are started when in `Deprecate PythonPath` experiment and opening a file without any folder opened.

src/client/common/interpreterPathService.ts

Lines changed: 8 additions & 2 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 { traceError } from './logger';
1213
import { FileSystemPaths } from './platform/fs-paths';
1314
import {
1415
IDisposable,
@@ -105,7 +106,8 @@ export class InterpreterPathService implements IInterpreterPathService {
105106
return;
106107
}
107108
if (!resource) {
108-
throw new Error('Cannot update workspace settings as no workspace is opened');
109+
traceError('Cannot update workspace settings as no workspace is opened');
110+
return;
109111
}
110112
const settingKey = this.getSettingKey(resource, configTarget);
111113
const persistentSetting = this.persistentStateFactory.createGlobalPersistentState<string | undefined>(
@@ -149,7 +151,11 @@ export class InterpreterPathService implements IInterpreterPathService {
149151

150152
public async _copyWorkspaceFolderValueToNewStorage(resource: Resource, value: string | undefined): Promise<void> {
151153
// Copy workspace folder setting into the new storage if it hasn't been copied already
152-
const workspaceFolderKey = this.workspaceService.getWorkspaceFolderIdentifier(resource);
154+
const workspaceFolderKey = this.workspaceService.getWorkspaceFolderIdentifier(resource, '');
155+
if (workspaceFolderKey === '') {
156+
// No workspace folder is opened, simply return.
157+
return;
158+
}
153159
const flaggedWorkspaceFolderKeysStorage = this.persistentStateFactory.createGlobalPersistentState<string[]>(
154160
workspaceFolderKeysForWhichTheCopyIsDone_Key,
155161
[]

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ suite('Interpreter Path Service', async () => {
9292
test('If the one-off transfer to new storage has not happened yet for the workspace folder, do it and record the transfer', async () => {
9393
const update = sinon.stub(InterpreterPathService.prototype, 'update');
9494
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
95-
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
95+
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource, '')).returns(() => resource.fsPath);
9696
persistentStateFactory
9797
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceFolderKeysForWhichTheCopyIsDone_Key, []))
9898
.returns(() => persistentState.object);
@@ -112,7 +112,7 @@ suite('Interpreter Path Service', async () => {
112112
test('If the one-off transfer to new storage has already happened for the workspace folder, do not update and simply return', async () => {
113113
const update = sinon.stub(InterpreterPathService.prototype, 'update');
114114
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
115-
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource)).returns(() => resource.fsPath);
115+
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource, '')).returns(() => resource.fsPath);
116116
persistentStateFactory
117117
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceFolderKeysForWhichTheCopyIsDone_Key, []))
118118
.returns(() => persistentState.object);
@@ -126,6 +126,23 @@ suite('Interpreter Path Service', async () => {
126126
persistentState.verifyAll();
127127
});
128128

129+
test('If no folder is opened, do not do the one-off transfer to new storage for the workspace folder', async () => {
130+
const update = sinon.stub(InterpreterPathService.prototype, 'update');
131+
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string[]>>();
132+
workspaceService.setup((w) => w.getWorkspaceFolderIdentifier(resource, '')).returns(() => '');
133+
persistentStateFactory
134+
.setup((p) => p.createGlobalPersistentState<string[]>(workspaceFolderKeysForWhichTheCopyIsDone_Key, []))
135+
.returns(() => persistentState.object);
136+
persistentState.setup((p) => p.value).returns(() => ['...storedWorkspaceKeys']);
137+
persistentState.setup((p) => p.updateValue(TypeMoq.It.isAny())).verifiable(TypeMoq.Times.never());
138+
139+
interpreterPathService = new InterpreterPathService(persistentStateFactory.object, workspaceService.object, []);
140+
await interpreterPathService._copyWorkspaceFolderValueToNewStorage(resource, 'workspaceFolderPythonPath');
141+
142+
assert(update.notCalled);
143+
persistentState.verifyAll();
144+
});
145+
129146
test('If the one-off transfer to new storage has not happened yet for the workspace, do it and record the transfer', async () => {
130147
const workspaceFileUri = Uri.parse('path/to/workspaceFile');
131148
const expectedWorkspaceKey = fs.normCase(workspaceFileUri.fsPath);
@@ -395,7 +412,7 @@ suite('Interpreter Path Service', async () => {
395412
_didChangeInterpreterEmitter.verifyAll();
396413
});
397414

398-
test('Updating workspace settings throws error if no workspace is opened', async () => {
415+
test('Updating workspace settings simply returns if no workspace is opened', async () => {
399416
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
400417
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
401418
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
@@ -408,18 +425,13 @@ suite('Interpreter Path Service', async () => {
408425
.returns(() => Promise.resolve())
409426
.verifiable(TypeMoq.Times.never());
410427

411-
const promise = interpreterPathService.update(
412-
resourceOutsideOfWorkspace,
413-
ConfigurationTarget.Workspace,
414-
interpreterPath
415-
);
416-
await expect(promise).to.eventually.be.rejectedWith(Error);
428+
await interpreterPathService.update(resourceOutsideOfWorkspace, ConfigurationTarget.Workspace, interpreterPath);
417429

418430
persistentState.verifyAll();
419431
persistentStateFactory.verifyAll();
420432
});
421433

422-
test('Updating workspace folder settings throws error if no workspace is opened', async () => {
434+
test('Updating workspace folder settings simply returns if no workspace is opened', async () => {
423435
const expectedSettingKey = `WORKSPACE_FOLDER_INTERPRETER_PATH_${resource.fsPath}`;
424436
const persistentState = TypeMoq.Mock.ofType<IPersistentState<string | undefined>>();
425437
workspaceService.setup((w) => w.workspaceFolders).returns(() => undefined);
@@ -432,12 +444,7 @@ suite('Interpreter Path Service', async () => {
432444
.returns(() => Promise.resolve())
433445
.verifiable(TypeMoq.Times.never());
434446

435-
const promise = interpreterPathService.update(
436-
resourceOutsideOfWorkspace,
437-
ConfigurationTarget.Workspace,
438-
interpreterPath
439-
);
440-
await expect(promise).to.eventually.be.rejectedWith(Error);
447+
await interpreterPathService.update(resourceOutsideOfWorkspace, ConfigurationTarget.Workspace, interpreterPath);
441448

442449
persistentState.verifyAll();
443450
persistentStateFactory.verifyAll();

0 commit comments

Comments
 (0)