Skip to content

Commit

Permalink
Improvement: Pass along python interpreter to jedi-language-server (#…
Browse files Browse the repository at this point in the history
…22466)

Fixes #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))
  • Loading branch information
johnhany97 authored Nov 28, 2023
1 parent 1639753 commit 261ae66
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/client/activation/jedi/analysisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions src/test/activation/jedi/jediAnalysisOptions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 261ae66

Please sign in to comment.