GeezerVison is showing github.com — Use a terminal to get environment variables of an activated interpret… · explain116/vscode-python@d788d18
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: experiments.json +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 78 | 78 | "min": 20, | 79 | 79 | "max": 24 | 80 | 80 | }, | | 81 | + { | | 82 | + "name": "UseTerminalToGetActivatedEnvVars - enabled", | | 83 | + "salt": "UseTerminalToGetActivatedEnvVars", | | 84 | + "min": 0, | | 85 | + "max": 20 | | 86 | + }, | | 87 | + { | | 88 | + "name": "UseTerminalToGetActivatedEnvVars - control", | | 89 | + "salt": "UseTerminalToGetActivatedEnvVars", | | 90 | + "min": 20, | | 91 | + "max": 100 | | 92 | + }, | 81 | 93 | { | 82 | 94 | "name": "AA_testing - control", | 83 | 95 | "salt": "AA_testing", | |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Use a hidden terminal to retrieve environment variables of an actived Python Interpreter. |
Collapse file Copy file name to clipboardExpand all lines: package.json +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1465 | 1465 | "Reload - experiment", | 1466 | 1466 | "AA_testing - experiment", | 1467 | 1467 | "WebHostNotebook - experiment", | | 1468 | + "UseTerminalToGetActivatedEnvVars - experiment", | 1468 | 1469 | "All" | 1469 | 1470 | ] | 1470 | 1471 | }, | | 1484 | 1485 | "Reload - experiment", | 1485 | 1486 | "AA_testing - experiment", | 1486 | 1487 | "WebHostNotebook - experiment", | | 1488 | + "UseTerminalToGetActivatedEnvVars - experiment", | 1487 | 1489 | "All" | 1488 | 1490 | ] | 1489 | 1491 | }, | |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. | | 2 | +# Licensed under the MIT License. | | 3 | + | | 4 | +import os | | 5 | +import json | | 6 | +import sys | | 7 | + | | 8 | + | | 9 | +# Last argument is the target file into which we'll write the env variables as json. | | 10 | +json_file = sys.argv[-1] | | 11 | + | | 12 | +with open(json_file, 'w') as outfile: | | 13 | + json.dump(dict(os.environ), outfile) |
Collapse file Copy file name to clipboardExpand all lines: pythonFiles/shell_exec.py +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 34 | 34 | # Signal end of execution with failure state. | 35 | 35 | fp.write('FAIL\n') | 36 | 36 | fp.flush() | | 37 | + try: | | 38 | + # ALso log the error for use from the other side. | | 39 | + with open(lock_file + '.error', 'w') as fpError: | | 40 | + fpError.write(traceback.format_exc()) | | 41 | + except Exception: | | 42 | + pass |
Collapse file Copy file name to clipboardExpand all lines: src/client/common/constants.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -101,3 +101,6 @@ export function isUnitTestExecution(): boolean { | 101 | 101 | } | 102 | 102 |
| 103 | 103 | export * from '../constants'; | | 104 | + | | 105 | +// Terminals with this name prefix will not be automatically activated. | | 106 | +export const terminalNamePrefixNotToAutoActivate = 'VSC_NO_ACTIVATE'; |
Collapse file Copy file name to clipboardExpand all lines: src/client/common/experimentGroups.ts +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,17 @@ export enum WebHostNotebook { | 42 | 42 | experiment = 'WebHostNotebook - experiment' | 43 | 43 | } | 44 | 44 |
| | 45 | +/** | | 46 | + * Experiment to check whether to to use a terminal to generate the environment variables of activated environments. | | 47 | + * | | 48 | + * @export | | 49 | + * @enum {number} | | 50 | + */ | | 51 | +export enum UseTerminalToGetActivatedEnvVars { | | 52 | + control = 'UseTerminalToGetActivatedEnvVars - control', | | 53 | + experiment = 'UseTerminalToGetActivatedEnvVars - experiment' | | 54 | +} | | 55 | + | 45 | 56 | // Dummy experiment added to validate metrics of A/B testing | 46 | 57 | export enum ValidateABTesting { | 47 | 58 | control = 'AA_testing - control', | |
Collapse file Copy file name to clipboardExpand all lines: src/client/common/terminal/factory.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,10 +30,12 @@ export class TerminalServiceFactory implements ITerminalServiceFactory { | 30 | 30 | const title = isUri(arg1) ? undefined : arg1?.title || arg2; | 31 | 31 | const terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python'; | 32 | 32 | const interpreter = isUri(arg1) ? undefined : arg1?.interpreter; | | 33 | + const hideFromUser = isUri(arg1) ? false : arg1?.hideFromUser === true; | 33 | 34 | const env = isUri(arg1) ? undefined : arg1?.env; | 34 | 35 |
| 35 | 36 | const options: TerminalCreationOptions = { | 36 | 37 | env, | | 38 | + hideFromUser, | 37 | 39 | interpreter, | 38 | 40 | resource, | 39 | 41 | title | |
Collapse file Copy file name to clipboardExpand all lines: src/client/common/terminal/service.ts +12-5Lines changed: 12 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -39,12 +39,16 @@ export class TerminalService implements ITerminalService, Disposable { | 39 | 39 | public async sendCommand(command: string, args: string[], _?: CancellationToken): Promise<void> { | 40 | 40 | await this.ensureTerminal(); | 41 | 41 | const text = this.terminalHelper.buildCommandForTerminal(this.terminalShellType, command, args); | 42 | | - this.terminal!.show(true); | | 42 | + if (!this.options?.hideFromUser) { | | 43 | + this.terminal!.show(true); | | 44 | + } | 43 | 45 | this.terminal!.sendText(text, true); | 44 | 46 | } | 45 | 47 | public async sendText(text: string): Promise<void> { | 46 | 48 | await this.ensureTerminal(); | 47 | | - this.terminal!.show(true); | | 49 | + if (!this.options?.hideFromUser) { | | 50 | + this.terminal!.show(true); | | 51 | + } | 48 | 52 | this.terminal!.sendText(text); | 49 | 53 | } | 50 | 54 | public async show(preserveFocus: boolean = true): Promise<void> { | @@ -56,18 +60,21 @@ export class TerminalService implements ITerminalService, Disposable { | 56 | 60 | return; | 57 | 61 | } | 58 | 62 | this.terminalShellType = this.terminalHelper.identifyTerminalShell(this.terminal); | 59 | | - this.terminal = this.terminalManager.createTerminal({ name: this.options?.title || 'Python', env: this.options?.env }); | | 63 | + this.terminal = this.terminalManager.createTerminal({ name: this.options?.title || 'Python', env: this.options?.env, hideFromUser: this.options?.hideFromUser }); | 60 | 64 |
| 61 | 65 | // Sometimes the terminal takes some time to start up before it can start accepting input. | 62 | 66 | await new Promise(resolve => setTimeout(resolve, 100)); | 63 | 67 |
| 64 | 68 | await this.terminalActivator.activateEnvironmentInTerminal(this.terminal!, { | 65 | 69 | resource: this.options?.resource, | 66 | 70 | preserveFocus, | 67 | | - interpreter: this.options?.interpreter | | 71 | + interpreter: this.options?.interpreter, | | 72 | + hideFromUser: this.options?.hideFromUser | 68 | 73 | }); | 69 | 74 |
| 70 | | - this.terminal!.show(preserveFocus); | | 75 | + if (!this.options?.hideFromUser) { | | 76 | + this.terminal!.show(preserveFocus); | | 77 | + } | 71 | 78 |
| 72 | 79 | this.sendTelemetry().ignoreErrors(); | 73 | 80 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/client/common/terminal/syncTerminalService.ts +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,7 +49,8 @@ class ExecutionState implements Disposable { | 49 | 49 | } | 50 | 50 | this.state = state; | 51 | 51 | if (state & State.errored) { | 52 | | - this._completed.reject(new Error(`Command failed with errors, check the terminal for details. Command: ${this.command.join(' ')}`)); | | 52 | + const errorContents = await this.fs.readFile(`${this.lockFile}.error`).catch(() => ''); | | 53 | + this._completed.reject(new Error(`Command failed with errors, check the terminal for details. Command: ${this.command.join(' ')}\n${errorContents}`)); | 53 | 54 | } else if (state & State.completed) { | 54 | 55 | this._completed.resolve(); | 55 | 56 | } | @@ -115,7 +116,7 @@ export class SynchronousTerminalService implements ITerminalService, Disposable | 115 | 116 | } | 116 | 117 | } | 117 | 118 | } | 118 | | - public async sendCommand(command: string, args: string[], cancel?: CancellationToken): Promise<void> { | | 119 | + public async sendCommand(command: string, args: string[], cancel?: CancellationToken, swallowExceptions: boolean = true): Promise<void> { | 119 | 120 | if (!cancel) { | 120 | 121 | return this.terminalService.sendCommand(command, args); | 121 | 122 | } | @@ -129,7 +130,8 @@ export class SynchronousTerminalService implements ITerminalService, Disposable | 129 | 130 | ...args, | 130 | 131 | lockFile.filePath.fileToCommandArgument() | 131 | 132 | ]); | 132 | | - await Cancellation.race(() => state.completed.catch(noop), cancel); | | 133 | + const promise = swallowExceptions ? state.completed : state.completed.catch(noop); | | 134 | + await Cancellation.race(() => promise, cancel); | 133 | 135 | } finally { | 134 | 136 | state.dispose(); | 135 | 137 | lockFile.dispose(); | |
|