From 261ae66bd27ac81fd9751bac6ad56c2f54d3d33b Mon Sep 17 00:00:00 2001 From: "John H. Ayad" Date: Tue, 28 Nov 2023 01:34:46 +0000 Subject: [PATCH] Improvement: Pass along python interpreter to jedi-language-server (#22466) Fixes https://github.com/microsoft/vscode-python/issues/22495 ## Before this PR Overriding the Python interpreter to a different environment that isn't the current globally-activated base environment would yield no completions when Jedi is used as the underlying language server. Example [stackoverflow question](https://stackoverflow.com/questions/62018436/vscode-intellisense-code-completion-doesnt-work-when-i-am-not-in-base-conda-e) hitting the same issue. ## After this PR We now pass along the interpreter path to jedi-language-server as part of the initial options under `workspace.environmentPath` ([ref](https://github.com/pappasam/jedi-language-server/#workspaceenvironmentpath)) --- src/client/activation/jedi/analysisOptions.ts | 4 ++++ .../jedi/jediAnalysisOptions.unit.test.ts | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/client/activation/jedi/analysisOptions.ts b/src/client/activation/jedi/analysisOptions.ts index 4778c4e1523f..d05267f041b3 100644 --- a/src/client/activation/jedi/analysisOptions.ts +++ b/src/client/activation/jedi/analysisOptions.ts @@ -16,6 +16,8 @@ import { ILanguageServerOutputChannel } from '../types'; export class JediLanguageServerAnalysisOptions extends LanguageServerAnalysisOptionsWithEnv { private resource: Resource | undefined; + private interpreter: PythonEnvironment | undefined; + constructor( envVarsProvider: IEnvironmentVariablesProvider, lsOutputChannel: ILanguageServerOutputChannel, @@ -28,6 +30,7 @@ export class JediLanguageServerAnalysisOptions extends LanguageServerAnalysisOpt public async initialize(resource: Resource, interpreter: PythonEnvironment | undefined) { this.resource = resource; + this.interpreter = interpreter; return super.initialize(resource, interpreter); } @@ -76,6 +79,7 @@ export class JediLanguageServerAnalysisOptions extends LanguageServerAnalysisOpt }, workspace: { extraPaths: distinctExtraPaths, + environmentPath: this.interpreter?.envPath, symbols: { // 0 means remove limit on number of workspace symbols returned maxSymbols: 0, diff --git a/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts b/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts index 8104ed2730b0..39ae0cfb21a9 100644 --- a/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts +++ b/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts @@ -12,6 +12,8 @@ import { WorkspaceService } from '../../../client/common/application/workspace'; import { ConfigurationService } from '../../../client/common/configuration/service'; import { IConfigurationService } from '../../../client/common/types'; import { IEnvironmentVariablesProvider } from '../../../client/common/variables/types'; +import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info'; +import { Architecture } from '../../../client/common/utils/platform'; suite('Jedi LSP - analysis Options', () => { const workspacePath = path.join('this', 'is', 'fake', 'workspace', 'path'); @@ -74,6 +76,25 @@ suite('Jedi LSP - analysis Options', () => { expect(result.initializationOptions.workspace.symbols.maxSymbols).to.deep.equal(0); }); + test('With interpreter path', async () => { + when(workspaceService.getWorkspaceFolder(anything())).thenReturn(undefined); + // eslint-disable-next-line @typescript-eslint/no-explicit-any + when(configurationService.getSettings(anything())).thenReturn({} as any); + const pythonEnvironment: PythonEnvironment = { + envPath: '.../.venv', + id: 'base_env', + envType: EnvironmentType.Conda, + path: '.../.venv/bin/python', + architecture: Architecture.x86, + sysPrefix: 'prefix/path', + }; + analysisOptions.initialize(undefined, pythonEnvironment); + + const result = await analysisOptions.getAnalysisOptions(); + + expect(result.initializationOptions.workspace.environmentPath).to.deep.equal('.../.venv'); + }); + test('Without extraPaths provided and no workspace', async () => { when(workspaceService.getWorkspaceFolder(anything())).thenReturn(undefined); // eslint-disable-next-line @typescript-eslint/no-explicit-any