Skip to content

Adding PYTHONSTARTUP with shell integration to environment variable collection #24104

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

Closed
wants to merge 6 commits into from
Closed
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 .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@typescript-eslint/no-dupe-class-members": "error",
"no-empty-function": "off",
"@typescript-eslint/no-empty-function": ["error"],
"no-inner-declarations": "warn",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-non-null-assertion": "off",
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/smoke-tests/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ runs:

# Bits from the VSIX are reused by smokeTest.ts to speed things up.
- name: Download VSIX
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: ${{ inputs.artifact_name }}

Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"quickPickItemTooltip",
"terminalDataWriteEvent",
"terminalExecuteCommandEvent",
"contribIssueReporter"
"contribIssueReporter",
"codeActionAI"
],
"author": {
"name": "Microsoft Corporation"
Expand All @@ -45,7 +46,7 @@
"theme": "dark"
},
"engines": {
"vscode": "^1.91.0"
"vscode": "^1.94.0-20240913"
},
"enableTelemetry": false,
"keywords": [
Expand Down Expand Up @@ -1192,7 +1193,8 @@
{
"filenames": [
"Pipfile",
"poetry.lock"
"poetry.lock",
"uv.lock"
],
"id": "toml"
},
Expand Down Expand Up @@ -1570,7 +1572,7 @@
"@types/sinon": "^17.0.3",
"@types/stack-trace": "0.0.29",
"@types/tmp": "^0.0.33",
"@types/vscode": "^1.81.0",
"@types/vscode": "^1.93.0",
"@types/which": "^2.0.1",
"@types/winreg": "^1.2.30",
"@types/xml2js": "^0.4.2",
Expand Down
17 changes: 16 additions & 1 deletion src/client/common/application/terminalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// Licensed under the MIT License.

import { injectable } from 'inversify';
import { Event, EventEmitter, Terminal, TerminalOptions, window } from 'vscode';
import {
Disposable,
Event,
EventEmitter,
Terminal,
TerminalOptions,
TerminalShellExecutionEndEvent,
TerminalShellIntegrationChangeEvent,
window,
} from 'vscode';
import { traceLog } from '../../logging';
import { ITerminalManager } from './types';

Expand All @@ -23,6 +32,12 @@ export class TerminalManager implements ITerminalManager {
public createTerminal(options: TerminalOptions): Terminal {
return monkeyPatchTerminal(window.createTerminal(options));
}
public onDidChangeTerminalShellIntegration(handler: (e: TerminalShellIntegrationChangeEvent) => void): Disposable {
return window.onDidChangeTerminalShellIntegration(handler);
}
public onDidEndTerminalShellExecution(handler: (e: TerminalShellExecutionEndEvent) => void): Disposable {
return window.onDidEndTerminalShellExecution(handler);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/client/common/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
StatusBarItem,
Terminal,
TerminalOptions,
TerminalShellExecutionEndEvent,
TerminalShellIntegrationChangeEvent,
TextDocument,
TextDocumentChangeEvent,
TextDocumentShowOptions,
Expand Down Expand Up @@ -936,6 +938,10 @@ export interface ITerminalManager {
* @return A new Terminal.
*/
createTerminal(options: TerminalOptions): Terminal;

onDidChangeTerminalShellIntegration(handler: (e: TerminalShellIntegrationChangeEvent) => void): Disposable;

onDidEndTerminalShellExecution(handler: (e: TerminalShellExecutionEndEvent) => void): Disposable;
}

export const IDebugService = Symbol('IDebugManager');
Expand Down
67 changes: 62 additions & 5 deletions src/client/common/terminal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ITerminalService,
TerminalCreationOptions,
TerminalShellType,
ITerminalExecutedCommand,
} from './types';

@injectable()
Expand All @@ -32,13 +33,15 @@ export class TerminalService implements ITerminalService, Disposable {
private terminalActivator: ITerminalActivator;
private terminalAutoActivator: ITerminalAutoActivation;
private readonly envVarScript = path.join(EXTENSION_ROOT_DIR, 'python_files', 'pythonrc.py');
private readonly executeCommandListeners: Set<Disposable> = new Set();
public get onDidCloseTerminal(): Event<void> {
return this.terminalClosed.event.bind(this.terminalClosed);
}
constructor(
@inject(IServiceContainer) private serviceContainer: IServiceContainer,
private readonly options?: TerminalCreationOptions,
) {
const a: TerminalService;
const disposableRegistry = this.serviceContainer.get<Disposable[]>(IDisposableRegistry);
disposableRegistry.push(this);
this.terminalHelper = this.serviceContainer.get<ITerminalHelper>(ITerminalHelper);
Expand All @@ -48,8 +51,12 @@ export class TerminalService implements ITerminalService, Disposable {
this.terminalActivator = this.serviceContainer.get<ITerminalActivator>(ITerminalActivator);
}
public dispose() {
if (this.terminal) {
this.terminal.dispose();
this.terminal?.dispose();

if (this.executeCommandListeners && this.executeCommandListeners.size > 0) {
this.executeCommandListeners.forEach((d) => {
d?.dispose();
});
}
}
public async sendCommand(command: string, args: string[], _?: CancellationToken): Promise<void> {
Expand All @@ -59,21 +66,70 @@ export class TerminalService implements ITerminalService, Disposable {
this.terminal!.show(true);
}

this.terminal!.sendText(text, true);
await this.executeCommand(text);
}
/** @deprecated */
public async sendText(text: string): Promise<void> {
await this.ensureTerminal();
if (!this.options?.hideFromUser) {
this.terminal!.show(true);
}
this.terminal!.sendText(text);
}
public async executeCommand(commandLine: string): Promise<ITerminalExecutedCommand | undefined> {
const terminal = this.terminal!;
if (!this.options?.hideFromUser) {
terminal.show(true);
}

// If terminal was just launched, wait some time for shell integration to onDidChangeShellIntegration.
if (!terminal.shellIntegration) {
const promise = new Promise<boolean>((resolve) => {
const shellIntegrationChangeEventListener = this.terminalManager.onDidChangeTerminalShellIntegration(
() => {
this.executeCommandListeners.delete(shellIntegrationChangeEventListener);
resolve(true);
},
);
const TIMEOUT_DURATION = 3000;
setTimeout(() => {
this.executeCommandListeners.add(shellIntegrationChangeEventListener);
resolve(true);
}, TIMEOUT_DURATION);
});
await promise;
}

if (terminal.shellIntegration) {
const execution = terminal.shellIntegration.executeCommand(commandLine);
return await new Promise((resolve) => {
const listener = this.terminalManager.onDidEndTerminalShellExecution((e) => {
if (e.execution === execution) {
this.executeCommandListeners.delete(listener);
resolve({ execution, exitCode: e.exitCode });
}
});
if (listener) {
this.executeCommandListeners.add(listener);
}
});
} else {
terminal.sendText(commandLine);
}

return undefined;
}

public async show(preserveFocus: boolean = true): Promise<void> {
await this.ensureTerminal(preserveFocus);
if (!this.options?.hideFromUser) {
this.terminal!.show(preserveFocus);
}
}
// When terminal is launched
// Copy pythonrc into <userdata>/pythonrc.py
// Update environment variable collection to include PYTHONSTARTUP=<userdata>/pythonrc.py

public async ensureTerminal(preserveFocus: boolean = true): Promise<void> {
if (this.terminal) {
return;
Expand All @@ -89,18 +145,19 @@ export class TerminalService implements ITerminalService, Disposable {
// Sometimes the terminal takes some time to start up before it can start accepting input.
await new Promise((resolve) => setTimeout(resolve, 100));

await this.terminalActivator.activateEnvironmentInTerminal(this.terminal!, {
await this.terminalActivator.activateEnvironmentInTerminal(this.terminal, {
resource: this.options?.resource,
preserveFocus,
interpreter: this.options?.interpreter,
hideFromUser: this.options?.hideFromUser,
});

if (!this.options?.hideFromUser) {
this.terminal!.show(preserveFocus);
this.terminal.show(preserveFocus);
}

this.sendTelemetry().ignoreErrors();
return;
}
private terminalCloseHandler(terminal: Terminal) {
if (terminal === this.terminal) {
Expand Down
6 changes: 5 additions & 1 deletion src/client/common/terminal/syncTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as internalScripts from '../process/internal/scripts';
import { createDeferred, Deferred } from '../utils/async';
import { noop } from '../utils/misc';
import { TerminalService } from './service';
import { ITerminalService } from './types';
import { ITerminalService, ITerminalExecutedCommand } from './types';

enum State {
notStarted = 0,
Expand Down Expand Up @@ -146,9 +146,13 @@ export class SynchronousTerminalService implements ITerminalService, Disposable
lockFile.dispose();
}
}
/** @deprecated */
public sendText(text: string): Promise<void> {
return this.terminalService.sendText(text);
}
public executeCommand(commandLine: string): Promise<ITerminalExecutedCommand | undefined> {
return this.terminalService.executeCommand(commandLine);
}
public show(preserveFocus?: boolean | undefined): Promise<void> {
return this.terminalService.show(preserveFocus);
}
Expand Down
9 changes: 8 additions & 1 deletion src/client/common/terminal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

'use strict';

import { CancellationToken, Event, Terminal, Uri } from 'vscode';
import { CancellationToken, Event, Terminal, Uri, TerminalShellExecution } from 'vscode';
import { PythonEnvironment } from '../../pythonEnvironments/info';
import { IEventNamePropertyMapping } from '../../telemetry/index';
import { IDisposable, Resource } from '../types';
Expand Down Expand Up @@ -52,10 +52,17 @@ export interface ITerminalService extends IDisposable {
cancel?: CancellationToken,
swallowExceptions?: boolean,
): Promise<void>;
/** @deprecated */
sendText(text: string): Promise<void>;
executeCommand(commandLine: string): Promise<ITerminalExecutedCommand | undefined>;
show(preserveFocus?: boolean): Promise<void>;
}

export interface ITerminalExecutedCommand {
execution: TerminalShellExecution;
exitCode: number | undefined;
}

export const ITerminalServiceFactory = Symbol('ITerminalServiceFactory');

export type TerminalCreationOptions = {
Expand Down
8 changes: 7 additions & 1 deletion src/client/extensionActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ import { TerminalProvider } from './providers/terminalProvider';
import { setExtensionInstallTelemetryProperties } from './telemetry/extensionInstallTelemetry';
import { registerTypes as tensorBoardRegisterTypes } from './tensorBoard/serviceRegistry';
import { registerTypes as commonRegisterTerminalTypes } from './terminals/serviceRegistry';
import { ICodeExecutionHelper, ICodeExecutionManager, ITerminalAutoActivation } from './terminals/types';
import {
ICodeExecutionHelper,
ICodeExecutionManager,
IPythonStartupEnvVarService,
ITerminalAutoActivation,
} from './terminals/types';
import { registerTypes as unitTestsRegisterTypes } from './testing/serviceRegistry';

// components
Expand Down Expand Up @@ -176,6 +181,7 @@ async function activateLegacy(ext: ExtensionState, startupStopWatch: StopWatch):
serviceContainer.get<IApplicationDiagnostics>(IApplicationDiagnostics).register();

serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register();
serviceManager.get<IPythonStartupEnvVarService>(IPythonStartupEnvVarService).register();

serviceManager.get<ICodeExecutionManager>(ICodeExecutionManager).registerCommands();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService {
this.configurationService.updateSetting('REPL.enableREPLSmartSend', false, resource);
}
} else {
await this.getTerminalService(resource).sendText(code);
await this.getTerminalService(resource).executeCommand(code);
}
}

Expand Down
Loading