Skip to content

Set python.pythonPath in workspace/configuration LSP call #11084

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 6 commits into from
Apr 13, 2020
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
1 change: 1 addition & 0 deletions news/1 Enhancements/11083.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure the language server can query pythonPath when in the Deprecate PythonPath experiment.
54 changes: 50 additions & 4 deletions src/client/activation/languageClientMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import {
WorkspaceEdit
} from 'vscode';
import {
ConfigurationParams,
ConfigurationRequest,
HandleDiagnosticsSignature,
HandlerResult,
Middleware,
PrepareRenameSignature,
ProvideCodeActionsSignature,
Expand All @@ -51,13 +54,14 @@ import {
ProvideWorkspaceSymbolsSignature,
ResolveCodeLensSignature,
ResolveCompletionItemSignature,
ResolveDocumentLinkSignature
ResolveDocumentLinkSignature,
ResponseError
} from 'vscode-languageclient';

import { ProvideDeclarationSignature } from 'vscode-languageclient/lib/declaration';
import { HiddenFilePrefix } from '../common/constants';
import { CollectLSRequestTiming, CollectNodeLSRequestTiming } from '../common/experimentGroups';
import { IExperimentsManager, IPythonExtensionBanner } from '../common/types';
import { IConfigurationService, IExperimentsManager, IPythonExtensionBanner } from '../common/types';
import { StopWatch } from '../common/utils/stopWatch';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
Expand All @@ -80,11 +84,48 @@ export class LanguageClientMiddleware implements Middleware {
public nextWindow: number = 0;
public eventCount: number = 0;

public workspace = {
// tslint:disable:no-any
configuration: (
params: ConfigurationParams,
token: CancellationToken,
next: ConfigurationRequest.HandlerSignature
): HandlerResult<any[], void> => {
// Hand-collapse "Thenable<A> | Thenable<B> | Thenable<A|B>" into just "Thenable<A|B>" to make TS happy.
const result: any[] | ResponseError<void> | Thenable<any[] | ResponseError<void>> = next(params, token);

// For backwards compatibility, set python.pythonPath to the configured
// value as though it were in the user's settings.json file.
const addPythonPath = (settings: any[] | ResponseError<void>) => {
if (settings instanceof ResponseError) {
return settings;
}

params.items.forEach((item, i) => {
if (item.section === 'python') {
const uri = item.scopeUri ? Uri.parse(item.scopeUri) : undefined;
settings[i].pythonPath = this.configService.getSettings(uri).pythonPath;
}
});

return settings;
};

if (isThenable(result)) {
return result.then(addPythonPath);
}

return addPythonPath(result);
}
// tslint:enable:no-any
};

private connected = false; // Default to not forwarding to VS code.

public constructor(
private readonly surveyBanner: IPythonExtensionBanner,
experimentsManager: IExperimentsManager,
private readonly configService: IConfigurationService,
serverType: LanguageServerType,
public readonly serverVersion?: string
) {
Expand Down Expand Up @@ -413,8 +454,8 @@ function captureTelemetryForLSPMethod(method: string, debounceMilliseconds: numb
const result = originalMethod.apply(this, args);

// tslint:disable-next-line:no-unsafe-any
if (result && typeof result.then === 'function') {
(result as Thenable<void>).then(() => {
if (result && isThenable<void>(result)) {
result.then(() => {
sendTelemetryEvent(eventName, stopWatch.elapsedTime, properties);
});
} else {
Expand All @@ -427,3 +468,8 @@ function captureTelemetryForLSPMethod(method: string, debounceMilliseconds: numb
return descriptor;
};
}

// tslint:disable-next-line: no-any
function isThenable<T>(v: any): v is Thenable<T> {
return typeof v?.then === 'function';
}
5 changes: 4 additions & 1 deletion src/client/activation/languageServer/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { inject, injectable, named } from 'inversify';
import { traceDecorators } from '../../common/logger';
import {
BANNER_NAME_LS_SURVEY,
IConfigurationService,
IDisposable,
IExperimentsManager,
IPythonExtensionBanner,
Expand Down Expand Up @@ -43,7 +44,8 @@ export class DotNetLanguageServerManager implements ILanguageServerManager {
@named(BANNER_NAME_LS_SURVEY)
private readonly surveyBanner: IPythonExtensionBanner,
@inject(ILanguageServerFolderService) private readonly folderService: ILanguageServerFolderService,
@inject(IExperimentsManager) private readonly experimentsManager: IExperimentsManager
@inject(IExperimentsManager) private readonly experimentsManager: IExperimentsManager,
@inject(IConfigurationService) private readonly configService: IConfigurationService
) {}
public dispose() {
if (this.languageProxy) {
Expand Down Expand Up @@ -107,6 +109,7 @@ export class DotNetLanguageServerManager implements ILanguageServerManager {
options.middleware = this.middleware = new LanguageClientMiddleware(
this.surveyBanner,
this.experimentsManager,
this.configService,
LanguageServerType.Microsoft,
versionPair?.version.format()
);
Expand Down
5 changes: 4 additions & 1 deletion src/client/activation/node/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { inject, injectable, named } from 'inversify';
import { traceDecorators } from '../../common/logger';
import {
BANNER_NAME_LS_SURVEY,
IConfigurationService,
IDisposable,
IExperimentsManager,
IPythonExtensionBanner,
Expand Down Expand Up @@ -41,7 +42,8 @@ export class NodeLanguageServerManager implements ILanguageServerManager {
@named(BANNER_NAME_LS_SURVEY)
private readonly surveyBanner: IPythonExtensionBanner,
@inject(ILanguageServerFolderService) private readonly folderService: ILanguageServerFolderService,
@inject(IExperimentsManager) private readonly experimentsManager: IExperimentsManager
@inject(IExperimentsManager) private readonly experimentsManager: IExperimentsManager,
@inject(IConfigurationService) private readonly configService: IConfigurationService
) {}

public dispose() {
Expand Down Expand Up @@ -103,6 +105,7 @@ export class NodeLanguageServerManager implements ILanguageServerManager {
options.middleware = this.middleware = new LanguageClientMiddleware(
this.surveyBanner,
this.experimentsManager,
this.configService,
LanguageServerType.Node,
versionPair?.version.format()
);
Expand Down
8 changes: 6 additions & 2 deletions src/test/activation/languageServer/manager.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
ILanguageServerFolderService,
ILanguageServerProxy
} from '../../../client/activation/types';
import { ConfigurationService } from '../../../client/common/configuration/service';
import { ExperimentsManager } from '../../../client/common/experiments';
import { IExperimentsManager, IPythonExtensionBanner } from '../../../client/common/types';
import { IConfigurationService, IExperimentsManager, IPythonExtensionBanner } from '../../../client/common/types';
import { ServiceContainer } from '../../../client/ioc/container';
import { IServiceContainer } from '../../../client/ioc/types';
import { ProposeLanguageServerBanner } from '../../../client/languageServices/proposeLanguageServerBanner';
Expand All @@ -37,6 +38,7 @@ suite('Language Server - Manager', () => {
let surveyBanner: IPythonExtensionBanner;
let folderService: ILanguageServerFolderService;
let experimentsManager: IExperimentsManager;
let configService: IConfigurationService;
const languageClientOptions = ({ x: 1 } as any) as LanguageClientOptions;
setup(() => {
serviceContainer = mock(ServiceContainer);
Expand All @@ -46,13 +48,15 @@ suite('Language Server - Manager', () => {
surveyBanner = mock(ProposeLanguageServerBanner);
folderService = mock(DotNetLanguageServerFolderService);
experimentsManager = mock(ExperimentsManager);
configService = mock(ConfigurationService);
manager = new DotNetLanguageServerManager(
instance(serviceContainer),
instance(analysisOptions),
instance(lsExtension),
instance(surveyBanner),
instance(folderService),
instance(experimentsManager)
instance(experimentsManager),
instance(configService)
);
});

Expand Down