Skip to content

Commit 02a92fc

Browse files
author
Kartik Raj
authored
Ensure interpreter path isn't truncated for workspace-relative paths when storing value (#20661)
For #20660 I'm not quite sure why this was done. It doesn't make sense to do this only for display.
1 parent 377067f commit 02a92fc

File tree

4 files changed

+21
-61
lines changed

4 files changed

+21
-61
lines changed

src/client/interpreter/configuration/services/workspaceFolderUpdaterService.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as path from 'path';
21
import { ConfigurationTarget, Uri } from 'vscode';
32
import { IInterpreterPathService } from '../../../common/types';
43
import { IPythonPathUpdaterService } from '../types';
@@ -11,9 +10,6 @@ export class WorkspaceFolderPythonPathUpdaterService implements IPythonPathUpdat
1110
if (pythonPathValue && pythonPathValue.workspaceFolderValue === pythonPath) {
1211
return;
1312
}
14-
if (pythonPath && pythonPath.startsWith(this.workspaceFolder.fsPath)) {
15-
pythonPath = path.relative(this.workspaceFolder.fsPath, pythonPath);
16-
}
1713
await this.interpreterPathService.update(this.workspaceFolder, ConfigurationTarget.WorkspaceFolder, pythonPath);
1814
}
1915
}

src/client/interpreter/configuration/services/workspaceUpdaterService.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as path from 'path';
21
import { ConfigurationTarget, Uri } from 'vscode';
32
import { IInterpreterPathService } from '../../../common/types';
43
import { IPythonPathUpdaterService } from '../types';
@@ -11,9 +10,6 @@ export class WorkspacePythonPathUpdaterService implements IPythonPathUpdaterServ
1110
if (pythonPathValue && pythonPathValue.workspaceValue === pythonPath) {
1211
return;
1312
}
14-
if (pythonPath && pythonPath.startsWith(this.workspace.fsPath)) {
15-
pythonPath = path.relative(this.workspace.fsPath, pythonPath);
16-
}
1713
await this.interpreterPathService.update(this.workspace, ConfigurationTarget.Workspace, pythonPath);
1814
}
1915
}

src/client/interpreter/interpreterService.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -186,28 +186,28 @@ export class InterpreterService implements Disposable, IInterpreterService {
186186
// However we need not wait on the update to take place, as we can use the value directly.
187187
if (!path) {
188188
path = this.configService.getSettings(resource).pythonPath;
189-
}
190-
if (pathUtils.basename(path) === path) {
191-
// Value can be `python`, `python3`, `python3.9` etc.
192-
// Note the following triggers autoselection if no interpreter is explictly
193-
// selected, i.e the value is `python`.
194-
// During shutdown we might not be able to get items out of the service container.
195-
const pythonExecutionFactory = this.serviceContainer.tryGet<IPythonExecutionFactory>(
196-
IPythonExecutionFactory,
197-
);
198-
const pythonExecutionService = pythonExecutionFactory
199-
? await pythonExecutionFactory.create({ resource })
200-
: undefined;
201-
const fullyQualifiedPath = pythonExecutionService
202-
? await pythonExecutionService.getExecutablePath().catch((ex) => {
203-
traceError(ex);
204-
})
205-
: undefined;
206-
// Python path is invalid or python isn't installed.
207-
if (!fullyQualifiedPath) {
208-
return undefined;
189+
if (pathUtils.basename(path) === path) {
190+
// Value can be `python`, `python3`, `python3.9` etc.
191+
// Note the following triggers autoselection if no interpreter is explictly
192+
// selected, i.e the value is `python`.
193+
// During shutdown we might not be able to get items out of the service container.
194+
const pythonExecutionFactory = this.serviceContainer.tryGet<IPythonExecutionFactory>(
195+
IPythonExecutionFactory,
196+
);
197+
const pythonExecutionService = pythonExecutionFactory
198+
? await pythonExecutionFactory.create({ resource })
199+
: undefined;
200+
const fullyQualifiedPath = pythonExecutionService
201+
? await pythonExecutionService.getExecutablePath().catch((ex) => {
202+
traceError(ex);
203+
})
204+
: undefined;
205+
// Python path is invalid or python isn't installed.
206+
if (!fullyQualifiedPath) {
207+
return undefined;
208+
}
209+
path = fullyQualifiedPath;
209210
}
210-
path = fullyQualifiedPath;
211211
}
212212
return this.getInterpreterDetails(path);
213213
}

src/test/interpreters/pythonPathUpdaterFactory.unit.test.ts

-32
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,6 @@ suite('Python Path Settings Updater', () => {
9494
await updater.updatePythonPath(pythonPath);
9595
interpreterPathService.verifyAll();
9696
});
97-
test('Python Path should be truncated for workspace-relative paths', async () => {
98-
const workspaceFolderPath = path.join('user', 'desktop', 'development');
99-
const workspaceFolder = Uri.file(workspaceFolderPath);
100-
const pythonPath = Uri.file(path.join(workspaceFolderPath, 'env', 'bin', 'python')).fsPath;
101-
const expectedPythonPath = path.join('env', 'bin', 'python');
102-
interpreterPathService.setup((i) => i.inspect(workspaceFolder)).returns(() => ({}));
103-
interpreterPathService
104-
.setup((i) => i.update(workspaceFolder, ConfigurationTarget.WorkspaceFolder, expectedPythonPath))
105-
.returns(() => Promise.resolve())
106-
.verifiable(TypeMoq.Times.once());
107-
108-
const updater = updaterServiceFactory.getWorkspaceFolderPythonPathConfigurationService(workspaceFolder);
109-
await updater.updatePythonPath(pythonPath);
110-
interpreterPathService.verifyAll();
111-
});
11297
});
11398
suite('Workspace (multiroot scenario)', () => {
11499
setup(() => setupMocks());
@@ -142,23 +127,6 @@ suite('Python Path Settings Updater', () => {
142127
const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder);
143128
await updater.updatePythonPath(pythonPath);
144129

145-
interpreterPathService.verifyAll();
146-
});
147-
test('Python Path should be truncated for workspace-relative paths', async () => {
148-
const workspaceFolderPath = path.join('user', 'desktop', 'development');
149-
const workspaceFolder = Uri.file(workspaceFolderPath);
150-
const pythonPath = Uri.file(path.join(workspaceFolderPath, 'env', 'bin', 'python')).fsPath;
151-
const expectedPythonPath = path.join('env', 'bin', 'python');
152-
153-
interpreterPathService.setup((i) => i.inspect(workspaceFolder)).returns(() => ({}));
154-
interpreterPathService
155-
.setup((i) => i.update(workspaceFolder, ConfigurationTarget.Workspace, expectedPythonPath))
156-
.returns(() => Promise.resolve())
157-
.verifiable(TypeMoq.Times.once());
158-
159-
const updater = updaterServiceFactory.getWorkspacePythonPathConfigurationService(workspaceFolder);
160-
await updater.updatePythonPath(pythonPath);
161-
162130
interpreterPathService.verifyAll();
163131
});
164132
});

0 commit comments

Comments
 (0)