From 59537e7cbe34376602204a2db7a09e80ad4a6533 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 7 Feb 2023 15:51:52 +0000 Subject: [PATCH] Ensure interpreter path isn\'t truncated for workspace-relative paths when storing value --- .../services/workspaceFolderUpdaterService.ts | 4 -- .../services/workspaceUpdaterService.ts | 4 -- src/client/interpreter/interpreterService.ts | 42 +++++++++---------- .../pythonPathUpdaterFactory.unit.test.ts | 32 -------------- 4 files changed, 21 insertions(+), 61 deletions(-) diff --git a/src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts b/src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts index 088d7129f3bf..8c9656b3febf 100644 --- a/src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts +++ b/src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts @@ -1,4 +1,3 @@ -import * as path from 'path'; import { ConfigurationTarget, Uri } from 'vscode'; import { IInterpreterPathService } from '../../../common/types'; import { IPythonPathUpdaterService } from '../types'; @@ -11,9 +10,6 @@ export class WorkspaceFolderPythonPathUpdaterService implements IPythonPathUpdat if (pythonPathValue && pythonPathValue.workspaceFolderValue === pythonPath) { return; } - if (pythonPath && pythonPath.startsWith(this.workspaceFolder.fsPath)) { - pythonPath = path.relative(this.workspaceFolder.fsPath, pythonPath); - } await this.interpreterPathService.update(this.workspaceFolder, ConfigurationTarget.WorkspaceFolder, pythonPath); } } diff --git a/src/client/interpreter/configuration/services/workspaceUpdaterService.ts b/src/client/interpreter/configuration/services/workspaceUpdaterService.ts index 7e99dc58fb7f..65bcd0b30e39 100644 --- a/src/client/interpreter/configuration/services/workspaceUpdaterService.ts +++ b/src/client/interpreter/configuration/services/workspaceUpdaterService.ts @@ -1,4 +1,3 @@ -import * as path from 'path'; import { ConfigurationTarget, Uri } from 'vscode'; import { IInterpreterPathService } from '../../../common/types'; import { IPythonPathUpdaterService } from '../types'; @@ -11,9 +10,6 @@ export class WorkspacePythonPathUpdaterService implements IPythonPathUpdaterServ if (pythonPathValue && pythonPathValue.workspaceValue === pythonPath) { return; } - if (pythonPath && pythonPath.startsWith(this.workspace.fsPath)) { - pythonPath = path.relative(this.workspace.fsPath, pythonPath); - } await this.interpreterPathService.update(this.workspace, ConfigurationTarget.Workspace, pythonPath); } } diff --git a/src/client/interpreter/interpreterService.ts b/src/client/interpreter/interpreterService.ts index 59ce435bb4d8..151b86508b8c 100644 --- a/src/client/interpreter/interpreterService.ts +++ b/src/client/interpreter/interpreterService.ts @@ -186,28 +186,28 @@ export class InterpreterService implements Disposable, IInterpreterService { // However we need not wait on the update to take place, as we can use the value directly. if (!path) { path = this.configService.getSettings(resource).pythonPath; - } - if (pathUtils.basename(path) === path) { - // Value can be `python`, `python3`, `python3.9` etc. - // Note the following triggers autoselection if no interpreter is explictly - // selected, i.e the value is `python`. - // During shutdown we might not be able to get items out of the service container. - const pythonExecutionFactory = this.serviceContainer.tryGet( - IPythonExecutionFactory, - ); - const pythonExecutionService = pythonExecutionFactory - ? await pythonExecutionFactory.create({ resource }) - : undefined; - const fullyQualifiedPath = pythonExecutionService - ? await pythonExecutionService.getExecutablePath().catch((ex) => { - traceError(ex); - }) - : undefined; - // Python path is invalid or python isn't installed. - if (!fullyQualifiedPath) { - return undefined; + if (pathUtils.basename(path) === path) { + // Value can be `python`, `python3`, `python3.9` etc. + // Note the following triggers autoselection if no interpreter is explictly + // selected, i.e the value is `python`. + // During shutdown we might not be able to get items out of the service container. + const pythonExecutionFactory = this.serviceContainer.tryGet( + IPythonExecutionFactory, + ); + const pythonExecutionService = pythonExecutionFactory + ? await pythonExecutionFactory.create({ resource }) + : undefined; + const fullyQualifiedPath = pythonExecutionService + ? await pythonExecutionService.getExecutablePath().catch((ex) => { + traceError(ex); + }) + : undefined; + // Python path is invalid or python isn't installed. + if (!fullyQualifiedPath) { + return undefined; + } + path = fullyQualifiedPath; } - path = fullyQualifiedPath; } return this.getInterpreterDetails(path); } diff --git a/src/test/interpreters/pythonPathUpdaterFactory.unit.test.ts b/src/test/interpreters/pythonPathUpdaterFactory.unit.test.ts index 379655187e88..762c23d86c8e 100644 --- a/src/test/interpreters/pythonPathUpdaterFactory.unit.test.ts +++ b/src/test/interpreters/pythonPathUpdaterFactory.unit.test.ts @@ -94,21 +94,6 @@ suite('Python Path Settings Updater', () => { await updater.updatePythonPath(pythonPath); interpreterPathService.verifyAll(); }); - test('Python Path should be truncated for workspace-relative paths', async () => { - const workspaceFolderPath = path.join('user', 'desktop', 'development'); - const workspaceFolder = Uri.file(workspaceFolderPath); - const pythonPath = Uri.file(path.join(workspaceFolderPath, 'env', 'bin', 'python')).fsPath; - const expectedPythonPath = path.join('env', 'bin', 'python'); - interpreterPathService.setup((i) => i.inspect(workspaceFolder)).returns(() => ({})); - interpreterPathService - .setup((i) => i.update(workspaceFolder, ConfigurationTarget.WorkspaceFolder, expectedPythonPath)) - .returns(() => Promise.resolve()) - .verifiable(TypeMoq.Times.once()); - - const updater = updaterServiceFactory.getWorkspaceFolderPythonPathConfigurationService(workspaceFolder); - await updater.updatePythonPath(pythonPath); - interpreterPathService.verifyAll(); - }); }); suite('Workspace (multiroot scenario)', () => { setup(() => setupMocks()); @@ -142,23 +127,6 @@ suite('Python Path Settings Updater', () => { const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder); await updater.updatePythonPath(pythonPath); - interpreterPathService.verifyAll(); - }); - test('Python Path should be truncated for workspace-relative paths', async () => { - const workspaceFolderPath = path.join('user', 'desktop', 'development'); - const workspaceFolder = Uri.file(workspaceFolderPath); - const pythonPath = Uri.file(path.join(workspaceFolderPath, 'env', 'bin', 'python')).fsPath; - const expectedPythonPath = path.join('env', 'bin', 'python'); - - interpreterPathService.setup((i) => i.inspect(workspaceFolder)).returns(() => ({})); - interpreterPathService - .setup((i) => i.update(workspaceFolder, ConfigurationTarget.Workspace, expectedPythonPath)) - .returns(() => Promise.resolve()) - .verifiable(TypeMoq.Times.once()); - - const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder); - await updater.updatePythonPath(pythonPath); - interpreterPathService.verifyAll(); }); });