Skip to content

Ensure interpreter path isn't truncated for workspace-relative paths when storing value #20661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as path from 'path';
import { ConfigurationTarget, Uri } from 'vscode';
import { IInterpreterPathService } from '../../../common/types';
import { IPythonPathUpdaterService } from '../types';
Expand All @@ -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);
}
Comment on lines -14 to -16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@karrtikr karrtikr Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I got it, this was because earlier we used python.pythonPath to store settings which can potentially be committed to source control, so we made sure it only contains relative paths.

That however is no longer necessary as we use internal VSCode memento for storing settings now.

await this.interpreterPathService.update(this.workspaceFolder, ConfigurationTarget.WorkspaceFolder, pythonPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as path from 'path';
import { ConfigurationTarget, Uri } from 'vscode';
import { IInterpreterPathService } from '../../../common/types';
import { IPythonPathUpdaterService } from '../types';
Expand All @@ -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);
}
}
42 changes: 21 additions & 21 deletions src/client/interpreter/interpreterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
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>(
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);
}
Expand Down
32 changes: 0 additions & 32 deletions src/test/interpreters/pythonPathUpdaterFactory.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
});
});
Expand Down