Skip to content

Commit fa10898

Browse files
author
Kartik Raj
committed
Only activate Pylance when in a untrusted workspace
1 parent 5cd7827 commit fa10898

File tree

6 files changed

+99
-57
lines changed

6 files changed

+99
-57
lines changed

src/client/activation/activationManager.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
4747
@inject(IExperimentService) private readonly experiments: IExperimentService,
4848
@inject(IInterpreterPathService) private readonly interpreterPathService: IInterpreterPathService,
4949
) {
50-
this.componentsToActivate = this.workspaceService.isVirtualWorkspace
51-
? [ComponentId.interpreter, ComponentId.languageServer, ComponentId.common]
52-
: undefined;
50+
this.componentsToActivate = this.getComponentsToActivate();
51+
}
52+
53+
private getComponentsToActivate(): ComponentId[] | undefined {
54+
if (!this.workspaceService.isTrusted) {
55+
return [ComponentId.languageServer];
56+
}
57+
if (this.workspaceService.isVirtualWorkspace) {
58+
return [ComponentId.interpreter, ComponentId.languageServer, ComponentId.common];
59+
}
60+
return undefined;
5361
}
5462

5563
public dispose(): void {
@@ -86,18 +94,21 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
8694
}
8795
this.activatedWorkspaces.add(key);
8896

89-
if (this.experiments.inExperimentSync(DeprecatePythonPath.experiment)) {
90-
await this.interpreterPathService.copyOldInterpreterStorageValuesToNew(resource);
97+
if (!this.componentsToActivate || this.componentsToActivate.includes(ComponentId.interpreter)) {
98+
if (this.experiments.inExperimentSync(DeprecatePythonPath.experiment)) {
99+
await this.interpreterPathService.copyOldInterpreterStorageValuesToNew(resource);
100+
}
101+
await sendActivationTelemetry(this.fileSystem, this.workspaceService, resource);
102+
await this.autoSelection.autoSelectInterpreter(resource);
91103
}
92104

93-
await sendActivationTelemetry(this.fileSystem, this.workspaceService, resource);
94-
95-
await this.autoSelection.autoSelectInterpreter(resource);
96105
const activationServices = this.activationServices.filter((s) =>
97106
this.componentsToActivate ? this.componentsToActivate.includes(s.componentId) : true,
98107
);
99108
await Promise.all(activationServices.map((item) => item.activate(resource)));
100-
await this.appDiagnostics.performPreStartupHealthCheck(resource);
109+
if (!this.componentsToActivate || this.componentsToActivate.includes(ComponentId.interpreter)) {
110+
await this.appDiagnostics.performPreStartupHealthCheck(resource);
111+
}
101112
}
102113

103114
public async initialize(): Promise<void> {

src/client/activation/activationService.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class LanguageServerExtensionActivationService
5858

5959
private readonly output: OutputChannel;
6060

61-
private readonly interpreterService: IInterpreterService;
61+
private readonly interpreterService?: IInterpreterService;
6262

6363
private readonly languageServerChangeHandler: LanguageServerChangeHandler;
6464

@@ -70,14 +70,16 @@ export class LanguageServerExtensionActivationService
7070
) {
7171
this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
7272
this.configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
73-
this.interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
7473
this.output = this.serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
7574

7675
const disposables = serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
7776
disposables.push(this);
7877
disposables.push(this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)));
7978
disposables.push(this.workspaceService.onDidChangeWorkspaceFolders(this.onWorkspaceFoldersChanged, this));
80-
disposables.push(this.interpreterService.onDidChangeInterpreter(this.onDidChangeInterpreter.bind(this)));
79+
if (this.workspaceService.isTrusted) {
80+
this.interpreterService = this.serviceContainer.get<IInterpreterService>(IInterpreterService);
81+
disposables.push(this.interpreterService.onDidChangeInterpreter(this.onDidChangeInterpreter.bind(this)));
82+
}
8183

8284
this.languageServerChangeHandler = new LanguageServerChangeHandler(
8385
this.getCurrentLanguageServerType(),
@@ -94,7 +96,7 @@ export class LanguageServerExtensionActivationService
9496
const stopWatch = new StopWatch();
9597
// Get a new server and dispose of the old one (might be the same one)
9698
this.resource = resource;
97-
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
99+
const interpreter = await this.interpreterService?.getActiveInterpreter(resource);
98100
const key = await this.getKey(resource, interpreter);
99101

100102
// If we have an old server with a different key, then deactivate it as the
@@ -306,7 +308,7 @@ export class LanguageServerExtensionActivationService
306308
resource,
307309
workspacePathNameForGlobalWorkspaces,
308310
);
309-
interpreter = interpreter || (await this.interpreterService.getActiveInterpreter(resource));
311+
interpreter = interpreter || (await this.interpreterService?.getActiveInterpreter(resource));
310312
const interperterPortion = interpreter ? `${interpreter.path}-${interpreter.envName}` : '';
311313
return `${resourcePortion}-${interperterPortion}`;
312314
}

src/client/extension.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,13 @@ async function activateUnsafe(
133133
setTimeout(async () => {
134134
if (activatedServiceContainer) {
135135
const workspaceService = activatedServiceContainer.get<IWorkspaceService>(IWorkspaceService);
136-
const interpreterManager = activatedServiceContainer.get<IInterpreterService>(IInterpreterService);
137-
const workspaces = workspaceService.workspaceFolders ?? [];
138-
await interpreterManager
139-
.refresh(workspaces.length > 0 ? workspaces[0].uri : undefined)
140-
.catch((ex) => traceError('Python Extension: interpreterManager.refresh', ex));
136+
if (workspaceService.isTrusted) {
137+
const interpreterManager = activatedServiceContainer.get<IInterpreterService>(IInterpreterService);
138+
const workspaces = workspaceService.workspaceFolders ?? [];
139+
await interpreterManager
140+
.refresh(workspaces.length > 0 ? workspaces[0].uri : undefined)
141+
.catch((ex) => traceError('Python Extension: interpreterManager.refresh', ex));
142+
}
141143
}
142144

143145
runAfterActivation();

src/client/extensionActivation.ts

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import { getLoggingLevel } from './logging/settings';
4747
import { DebugService } from './common/application/debugService';
4848
import { DebugSessionEventDispatcher } from './debugger/extension/hooks/eventHandlerDispatcher';
4949
import { IDebugSessionEventHandlers } from './debugger/extension/hooks/types';
50+
import { WorkspaceService } from './common/application/workspace';
5051

5152
export async function activateComponents(
5253
// `ext` is passed to any extra activation funcs.
@@ -70,6 +71,10 @@ export async function activateComponents(
7071
// https://github.com/microsoft/vscode-python/issues/15380
7172
// These will go away eventually once everything is refactored into components.
7273
const legacyActivationResult = await activateLegacy(ext);
74+
const workspaceService = new WorkspaceService();
75+
if (!workspaceService.isTrusted) {
76+
return [legacyActivationResult];
77+
}
7378
const promises: Promise<ActivationResult>[] = [
7479
// More component activations will go here
7580
pythonEnvironments.activate(components.pythonEnvs, ext),
@@ -133,56 +138,64 @@ async function activateLegacy(ext: ExtensionState): Promise<ActivationResult> {
133138
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
134139
const cmdManager = serviceContainer.get<ICommandManager>(ICommandManager);
135140
languages.setLanguageConfiguration(PYTHON_LANGUAGE, getLanguageConfiguration());
136-
const interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService);
137-
interpreterManager.initialize();
138-
if (!workspaceService.isVirtualWorkspace) {
139-
const handlers = serviceManager.getAll<IDebugSessionEventHandlers>(IDebugSessionEventHandlers);
140-
const dispatcher = new DebugSessionEventDispatcher(handlers, DebugService.instance, disposables);
141-
dispatcher.registerEventHandlers();
141+
if (workspaceService.isTrusted) {
142+
const interpreterManager = serviceContainer.get<IInterpreterService>(IInterpreterService);
143+
interpreterManager.initialize();
144+
if (!workspaceService.isVirtualWorkspace) {
145+
const handlers = serviceManager.getAll<IDebugSessionEventHandlers>(IDebugSessionEventHandlers);
146+
const dispatcher = new DebugSessionEventDispatcher(handlers, DebugService.instance, disposables);
147+
dispatcher.registerEventHandlers();
142148

143-
const outputChannel = serviceManager.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
144-
disposables.push(cmdManager.registerCommand(Commands.ViewOutput, () => outputChannel.show()));
145-
cmdManager.executeCommand('setContext', 'python.vscode.channel', applicationEnv.channel).then(noop, noop);
149+
const outputChannel = serviceManager.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
150+
disposables.push(cmdManager.registerCommand(Commands.ViewOutput, () => outputChannel.show()));
151+
cmdManager.executeCommand('setContext', 'python.vscode.channel', applicationEnv.channel).then(noop, noop);
146152

147-
serviceContainer.get<IApplicationDiagnostics>(IApplicationDiagnostics).register();
153+
serviceContainer.get<IApplicationDiagnostics>(IApplicationDiagnostics).register();
148154

149-
serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register();
150-
const pythonSettings = configuration.getSettings();
155+
serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register();
156+
const pythonSettings = configuration.getSettings();
151157

152-
const sortImports = serviceContainer.get<ISortImportsEditingProvider>(ISortImportsEditingProvider);
153-
sortImports.registerCommands();
158+
const sortImports = serviceContainer.get<ISortImportsEditingProvider>(ISortImportsEditingProvider);
159+
sortImports.registerCommands();
154160

155-
serviceManager.get<ICodeExecutionManager>(ICodeExecutionManager).registerCommands();
161+
serviceManager.get<ICodeExecutionManager>(ICodeExecutionManager).registerCommands();
156162

157-
context.subscriptions.push(new LinterCommands(serviceManager));
163+
context.subscriptions.push(new LinterCommands(serviceManager));
158164

159-
if (pythonSettings && pythonSettings.formatting && pythonSettings.formatting.provider !== 'internalConsole') {
160-
const formatProvider = new PythonFormattingEditProvider(context, serviceContainer);
161-
context.subscriptions.push(languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider));
162-
context.subscriptions.push(languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider));
163-
}
165+
if (
166+
pythonSettings &&
167+
pythonSettings.formatting &&
168+
pythonSettings.formatting.provider !== 'internalConsole'
169+
) {
170+
const formatProvider = new PythonFormattingEditProvider(context, serviceContainer);
171+
context.subscriptions.push(languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider));
172+
context.subscriptions.push(
173+
languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider),
174+
);
175+
}
164176

165-
context.subscriptions.push(new ReplProvider(serviceContainer));
177+
context.subscriptions.push(new ReplProvider(serviceContainer));
166178

167-
const terminalProvider = new TerminalProvider(serviceContainer);
168-
terminalProvider.initialize(window.activeTerminal).ignoreErrors();
169-
context.subscriptions.push(terminalProvider);
179+
const terminalProvider = new TerminalProvider(serviceContainer);
180+
terminalProvider.initialize(window.activeTerminal).ignoreErrors();
181+
context.subscriptions.push(terminalProvider);
170182

171-
context.subscriptions.push(
172-
languages.registerCodeActionsProvider(PYTHON, new PythonCodeActionProvider(), {
173-
providedCodeActionKinds: [CodeActionKind.SourceOrganizeImports],
174-
}),
175-
);
183+
context.subscriptions.push(
184+
languages.registerCodeActionsProvider(PYTHON, new PythonCodeActionProvider(), {
185+
providedCodeActionKinds: [CodeActionKind.SourceOrganizeImports],
186+
}),
187+
);
176188

177-
serviceContainer
178-
.getAll<DebugConfigurationProvider>(IDebugConfigurationService)
179-
.forEach((debugConfigProvider) => {
180-
context.subscriptions.push(
181-
debug.registerDebugConfigurationProvider(DebuggerTypeName, debugConfigProvider),
182-
);
183-
});
189+
serviceContainer
190+
.getAll<DebugConfigurationProvider>(IDebugConfigurationService)
191+
.forEach((debugConfigProvider) => {
192+
context.subscriptions.push(
193+
debug.registerDebugConfigurationProvider(DebuggerTypeName, debugConfigProvider),
194+
);
195+
});
184196

185-
serviceContainer.get<IDebuggerBanner>(IDebuggerBanner).initialize();
197+
serviceContainer.get<IDebuggerBanner>(IDebuggerBanner).initialize();
198+
}
186199
}
187200

188201
// "activate" everything else

src/client/jupyter/jupyterIntegration.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { dirname } from 'path';
99
import { CancellationToken, Disposable, Event, Extension, Memento, Uri } from 'vscode';
1010
import * as lsp from 'vscode-languageserver-protocol';
1111
import { ILanguageServerCache, ILanguageServerConnection } from '../activation/types';
12+
import { IWorkspaceService } from '../common/application/types';
1213
import { JUPYTER_EXTENSION_ID } from '../common/constants';
1314
import { InterpreterUri, ModuleInstallFlags } from '../common/installer/types';
1415
import {
@@ -176,9 +177,13 @@ export class JupyterExtensionIntegration {
176177
@inject(IMemento) @named(GLOBAL_MEMENTO) private globalState: Memento,
177178
@inject(IInterpreterDisplay) private interpreterDisplay: IInterpreterDisplay,
178179
@inject(IComponentAdapter) private pyenvs: IComponentAdapter,
180+
@inject(IWorkspaceService) private workspaceService: IWorkspaceService,
179181
) {}
180182

181183
public registerApi(jupyterExtensionApi: JupyterExtensionApi): JupyterExtensionApi | undefined {
184+
if (!this.workspaceService.isTrusted) {
185+
return undefined;
186+
}
182187
// Forward python parts
183188
jupyterExtensionApi.registerPythonApi({
184189
onDidChangeInterpreter: this.interpreterService.onDidChangeInterpreter,

src/client/startupTelemetry.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
import { IWorkspaceService } from './common/application/types';
5+
import { WorkspaceService } from './common/application/workspace';
56
import { isTestExecution } from './common/constants';
67
import { DeprecatePythonPath } from './common/experiments/groups';
78
import { ITerminalHelper } from './common/terminal/types';
@@ -32,6 +33,10 @@ export async function sendStartupTelemetry(
3233
if (isTestExecution()) {
3334
return;
3435
}
36+
const workspaceService = new WorkspaceService();
37+
if (!workspaceService.isTrusted) {
38+
return;
39+
}
3540

3641
try {
3742
await activatedPromise;
@@ -48,6 +53,10 @@ export async function sendErrorTelemetry(
4853
durations: IStartupDurations,
4954
serviceContainer?: IServiceContainer,
5055
) {
56+
const workspaceService = new WorkspaceService();
57+
if (!workspaceService.isTrusted) {
58+
return;
59+
}
5160
try {
5261
let props: any = {};
5362
if (serviceContainer) {

0 commit comments

Comments
 (0)