From 00280499095dd6375daef549c17b57f9ba39229f Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Mon, 8 Jul 2024 12:47:45 -0700 Subject: [PATCH 1/8] start trying SI proposed API --- .../envCollectionActivation/service.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index 8993007ce7c8..f8ada0eb7d0e 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -10,6 +10,7 @@ import { EnvironmentVariableScope, EnvironmentVariableMutatorOptions, ProgressLocation, + window } from 'vscode'; import { pathExists } from 'fs-extra'; import { IExtensionActivationService } from '../../activation/types'; @@ -202,7 +203,24 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ resource, shell, ); - } + // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // But first, try some dummy commands to see if I can get any sort of exit code. + const myTerm = window.createTerminal(); + window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + if (terminal === myTerm) { + const execution = shellIntegration.executeCommand('echo "Hello world"'); + window.onDidEndTerminalShellExecution(event => { + if (event.execution === execution) { + console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... + traceLog( + `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` + ); + let temp = event.exitCode; + } + }); + } + }); + //////////////////////////// const processEnv = normCaseKeys(this.processEnvVars); // PS1 in some cases is a shell variable (not an env variable) so "env" might not contain it, calculate it in that case. From efc792bf4d8748549714396863d4050c145859e6 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Tue, 9 Jul 2024 10:45:17 -0700 Subject: [PATCH 2/8] getting exit code 0 with different placement --- .../envCollectionActivation/service.ts | 59 +++++++++++++------ 1 file changed, 42 insertions(+), 17 deletions(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index f8ada0eb7d0e..0af7b45b35d0 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -182,6 +182,27 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ undefined, shell, ); + // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // But first, try some dummy commands to see if I can get any sort of exit code. + const myTerm = window.createTerminal(); + window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + if (terminal === myTerm) { + const execution = shellIntegration.executeCommand('echo "Hello world"'); + // const stream = execution.read(); + // for await(const data of stream) { + // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // } + window.onDidEndTerminalShellExecution(event => { + if (event.execution === execution) { + console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here. + traceLog( + `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` + ); + let temp = event.exitCode; + } + }); + } + }); const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env); if (!env) { @@ -203,23 +224,27 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ resource, shell, ); - // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. - // But first, try some dummy commands to see if I can get any sort of exit code. - const myTerm = window.createTerminal(); - window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - if (terminal === myTerm) { - const execution = shellIntegration.executeCommand('echo "Hello world"'); - window.onDidEndTerminalShellExecution(event => { - if (event.execution === execution) { - console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... - traceLog( - `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` - ); - let temp = event.exitCode; - } - }); - } - }); + // // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // // But first, try some dummy commands to see if I can get any sort of exit code. + // const myTerm = window.createTerminal(); + // window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + // if (terminal === myTerm) { + // const execution = shellIntegration.executeCommand('echo "Hello world"'); + // // const stream = execution.read(); + // // for await(const data of stream) { + // // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // // } + // window.onDidEndTerminalShellExecution(event => { + // if (event.execution === execution) { + // console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. + // traceLog( + // `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` + // ); + // let temp = event.exitCode; + // } + // }); + // } + // }); //////////////////////////// const processEnv = normCaseKeys(this.processEnvVars); From 9c27df587ca43535ae0563642b55a632f3f7a02c Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Tue, 9 Jul 2024 13:52:15 -0700 Subject: [PATCH 3/8] failing to get exit code --- src/client/terminals/envCollectionActivation/service.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index 0af7b45b35d0..18ec649bf7f7 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -182,6 +182,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ undefined, shell, ); + /////////////////////////////////////////////////////////////////// // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. // But first, try some dummy commands to see if I can get any sort of exit code. const myTerm = window.createTerminal(); @@ -194,7 +195,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // } window.onDidEndTerminalShellExecution(event => { if (event.execution === execution) { - console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here. + console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here. -- failing to get exit code again traceLog( `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` ); @@ -203,6 +204,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ }); } }); + ////////////////////////////////////////////////////////////// const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env); if (!env) { From 91e7f25be61322529333885ddada2ca688a424f9 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Tue, 9 Jul 2024 14:03:41 -0700 Subject: [PATCH 4/8] stop messing with parenthesis --- .../envCollectionActivation/service.ts | 75 +++++++++---------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index 18ec649bf7f7..531de0ea71f6 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -10,7 +10,7 @@ import { EnvironmentVariableScope, EnvironmentVariableMutatorOptions, ProgressLocation, - window + window, } from 'vscode'; import { pathExists } from 'fs-extra'; import { IExtensionActivationService } from '../../activation/types'; @@ -182,29 +182,27 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ undefined, shell, ); - /////////////////////////////////////////////////////////////////// + /// //////////////////////////////////////////////////////////////// // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. - // But first, try some dummy commands to see if I can get any sort of exit code. - const myTerm = window.createTerminal(); - window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - if (terminal === myTerm) { - const execution = shellIntegration.executeCommand('echo "Hello world"'); + // But first, try some dummy commands to see if I can get any sort of exit code. + const myTerm = window.createTerminal(); + window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + if (terminal === myTerm) { + const execution = shellIntegration.executeCommand('echo "Hello world"'); // const stream = execution.read(); // for await(const data of stream) { // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); // } - window.onDidEndTerminalShellExecution(event => { + window.onDidEndTerminalShellExecution((event) => { if (event.execution === execution) { - console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here. -- failing to get exit code again - traceLog( - `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` - ); - let temp = event.exitCode; + console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here, flaky. --> failing to get exit code again + traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); + const temp = event.exitCode; } - }); - } - }); - ////////////////////////////////////////////////////////////// + }); + } + }); + /// /////////////////////////////////////////////////////////// const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env); if (!env) { @@ -226,28 +224,27 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ resource, shell, ); - // // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. - // // But first, try some dummy commands to see if I can get any sort of exit code. - // const myTerm = window.createTerminal(); - // window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - // if (terminal === myTerm) { - // const execution = shellIntegration.executeCommand('echo "Hello world"'); - // // const stream = execution.read(); - // // for await(const data of stream) { - // // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); - // // } - // window.onDidEndTerminalShellExecution(event => { - // if (event.execution === execution) { - // console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. - // traceLog( - // `HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE` - // ); - // let temp = event.exitCode; - // } - // }); - // } - // }); - //////////////////////////// + // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // But first, try some dummy commands to see if I can get any sort of exit code. + const myTerm = window.createTerminal(); + window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + if (terminal === myTerm) { + const execution = shellIntegration.executeCommand('echo "Hello world"'); + // const stream = execution.read(); + // for await(const data of stream) { + // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // } + window.onDidEndTerminalShellExecution((event) => { + if (event.execution === execution) { + console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. + traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); + const temp = event.exitCode; + } + }); + } + }); + } + /// ///////////////////////// const processEnv = normCaseKeys(this.processEnvVars); // PS1 in some cases is a shell variable (not an env variable) so "env" might not contain it, calculate it in that case. From 83780b97b33267d5a854b3904540ba8f6b747952 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Tue, 9 Jul 2024 14:08:01 -0700 Subject: [PATCH 5/8] suppress error for build vsix --- src/client/terminals/envCollectionActivation/service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index 531de0ea71f6..cc3b7a347be4 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -197,7 +197,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ if (event.execution === execution) { console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here, flaky. --> failing to get exit code again traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); - const temp = event.exitCode; + // const temp = event.exitCode; } }); } @@ -238,7 +238,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ if (event.execution === execution) { console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); - const temp = event.exitCode; + // const temp = event.exitCode; } }); } From 5d6f71951b5d8fbbda2df5f507d4296ec3ca726e Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Wed, 10 Jul 2024 11:40:22 -0700 Subject: [PATCH 6/8] trying out xterm.buffer.active Co-authored-by: Daniel Imms --- package-lock.json | 11 +++ package.json | 3 +- .../envCollectionActivation/service.ts | 68 ++++++++++++------- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6419b9f3393e..5ee491c2d1dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@iarna/toml": "^2.2.5", "@vscode/extension-telemetry": "^0.8.4", + "@xterm/headless": "^5.5.0", "arch": "^2.1.0", "fs-extra": "^10.0.1", "glob": "^7.2.0", @@ -2400,6 +2401,11 @@ } } }, + "node_modules/@xterm/headless": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/@xterm/headless/-/headless-5.5.0.tgz", + "integrity": "sha512-5xXB7kdQlFBP82ViMJTwwEc3gKCLGKR/eoxQm4zge7GPBl86tCdI0IdPJjoKd8mUSFXz5V7i/25sfsEkP4j46g==" + }, "node_modules/@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", @@ -15851,6 +15857,11 @@ "dev": true, "requires": {} }, + "@xterm/headless": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/@xterm/headless/-/headless-5.5.0.tgz", + "integrity": "sha512-5xXB7kdQlFBP82ViMJTwwEc3gKCLGKR/eoxQm4zge7GPBl86tCdI0IdPJjoKd8mUSFXz5V7i/25sfsEkP4j46g==" + }, "@xtuc/ieee754": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/@xtuc/ieee754/-/ieee754-1.2.0.tgz", diff --git a/package.json b/package.json index 3de58d434ec4..e302aa14c543 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,7 @@ "description": "%walkthrough.pythonWelcome.description%", "when": "workspacePlatform != webworker", "steps": [ - { + { "id": "python.createPythonFolder", "title": "%walkthrough.step.python.createPythonFolder.title%", "description": "%walkthrough.step.python.createPythonFolder.description%", @@ -1514,6 +1514,7 @@ "dependencies": { "@iarna/toml": "^2.2.5", "@vscode/extension-telemetry": "^0.8.4", + "@xterm/headless": "^5.5.0", "arch": "^2.1.0", "fs-extra": "^10.0.1", "glob": "^7.2.0", diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index cc3b7a347be4..d8a7f16a8e1e 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -2,6 +2,7 @@ // Licensed under the MIT License. import * as path from 'path'; +import { Terminal as HeadlessTerminal } from '@xterm/headless'; import { inject, injectable } from 'inversify'; import { MarkdownString, @@ -10,6 +11,7 @@ import { EnvironmentVariableScope, EnvironmentVariableMutatorOptions, ProgressLocation, + Terminal, window, } from 'vscode'; import { pathExists } from 'fs-extra'; @@ -167,6 +169,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ this.progressService.hideProgress(); } + private _trackedTerminals: Set = new Set(); + private async _applyCollectionImpl(resource: Resource, shell = this.applicationEnvironment.shell): Promise { const workspaceFolder = this.getWorkspaceFolder(resource); const settings = this.configurationService.getSettings(resource); @@ -182,27 +186,27 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ undefined, shell, ); - /// //////////////////////////////////////////////////////////////// - // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. - // But first, try some dummy commands to see if I can get any sort of exit code. - const myTerm = window.createTerminal(); - window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - if (terminal === myTerm) { - const execution = shellIntegration.executeCommand('echo "Hello world"'); - // const stream = execution.read(); - // for await(const data of stream) { - // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); - // } - window.onDidEndTerminalShellExecution((event) => { - if (event.execution === execution) { - console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here, flaky. --> failing to get exit code again - traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); - // const temp = event.exitCode; - } - }); - } - }); - /// /////////////////////////////////////////////////////////// + // /// //////////////////////////////////////////////////////////////// + // // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // // But first, try some dummy commands to see if I can get any sort of exit code. + // const myTerm = window.createTerminal(); + // window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + // if (terminal === myTerm) { + // const execution = shellIntegration.executeCommand('echo "Hello world"'); + // // const stream = execution.read(); + // // for await(const data of stream) { + // // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // // } + // window.onDidEndTerminalShellExecution((event) => { + // if (event.execution === execution) { + // console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here, flaky. --> failing to get exit code again + // traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); + // // const temp = event.exitCode; + // } + // }); + // } + // }); + // /// /////////////////////////////////////////////////////////// const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env); if (!env) { @@ -227,20 +231,34 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. // But first, try some dummy commands to see if I can get any sort of exit code. const myTerm = window.createTerminal(); + const xterm = new HeadlessTerminal({ + allowProposedApi: true, + }); window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { if (terminal === myTerm) { + if (this._trackedTerminals.has(terminal)) { + return; + } + this._trackedTerminals.add(terminal); const execution = shellIntegration.executeCommand('echo "Hello world"'); - // const stream = execution.read(); - // for await(const data of stream) { - // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); - // } window.onDidEndTerminalShellExecution((event) => { if (event.execution === execution) { console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); // const temp = event.exitCode; + const buffer = xterm.buffer.active; + const lines: string[] = []; + for (let y = 0; y < buffer.length; y += 1) { + lines.push(buffer.getLine(y)!.translateToString(true)); + } + traceLog(`xterm-headless content\n\`\`\`\n${lines.join('\n').trim()}\n\`\`\``); } }); + const stream = execution.read(); + for await (const data of stream) { + xterm.write(data); + traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + } } }); } From ca4477c46857cd71a8dcc957720da43aadd9e290 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Wed, 10 Jul 2024 11:43:16 -0700 Subject: [PATCH 7/8] we should properly add disposable --- .../envCollectionActivation/service.ts | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index d8a7f16a8e1e..770b2f938031 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -44,7 +44,8 @@ import { IShellIntegrationService, ITerminalDeactivateService, ITerminalEnvVarCo import { ProgressService } from '../../common/application/progressService'; @injectable() -export class TerminalEnvVarCollectionService implements IExtensionActivationService, ITerminalEnvVarCollectionService { +export class TerminalEnvVarCollectionService extends Disposable + implements IExtensionActivationService, ITerminalEnvVarCollectionService { public readonly supportedWorkspaceTypes = { untrustedWorkspace: false, virtualWorkspace: false, @@ -70,6 +71,9 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ private separator: string; + private _shellIntegrationDisposableMap = this.register(new DisposableMap()); + + // problem: when extension is disposed, we want terminal env collection service to be disposed. When that is disposed, we want the listeners disposed as well constructor( @inject(IPlatformService) private readonly platform: IPlatformService, @inject(IInterpreterService) private interpreterService: IInterpreterService, @@ -87,7 +91,8 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ @inject(IEnvironmentVariablesProvider) private readonly environmentVariablesProvider: IEnvironmentVariablesProvider, ) { - this.separator = platform.osType === OSType.Windows ? ';' : ':'; + super(); + this.this.separator = platform.osType === OSType.Windows ? ';' : ':'; this.progressService = new ProgressService(this.shell); } @@ -186,9 +191,9 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ undefined, shell, ); - // /// //////////////////////////////////////////////////////////////// - // // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. - // // But first, try some dummy commands to see if I can get any sort of exit code. + /// //////////////////////////////////////////////////////////////// + // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // But first, try some dummy commands to see if I can get any sort of exit code. // const myTerm = window.createTerminal(); // window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { // if (terminal === myTerm) { @@ -206,7 +211,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // }); // } // }); - // /// /////////////////////////////////////////////////////////// + /// /////////////////////////////////////////////////////////// const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env); if (!env) { @@ -231,37 +236,35 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. // But first, try some dummy commands to see if I can get any sort of exit code. const myTerm = window.createTerminal(); - const xterm = new HeadlessTerminal({ - allowProposedApi: true, - }); - window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - if (terminal === myTerm) { - if (this._trackedTerminals.has(terminal)) { - return; - } - this._trackedTerminals.add(terminal); - const execution = shellIntegration.executeCommand('echo "Hello world"'); - window.onDidEndTerminalShellExecution((event) => { - if (event.execution === execution) { - console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. - traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); - // const temp = event.exitCode; - const buffer = xterm.buffer.active; - const lines: string[] = []; - for (let y = 0; y < buffer.length; y += 1) { - lines.push(buffer.getLine(y)!.translateToString(true)); + + map.set( + terminal, + window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + // can fire multiple times for single terminal ---> ATM when shell integration status changes, when its activated or working directory changes. + // listen to this once per terminal. Dispose once its done + + if (terminal === myTerm) { + const dispoable = map.get(terminal); + disposable.dispose(); + map.delete(terminal); + const execution = shellIntegration.executeCommand('echo "Hello world"'); + + // const stream = execution.read(); + // for await(const data of stream) { + // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // } + window.onDidEndTerminalShellExecution((event) => { + if (event.execution === execution) { + console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. + traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); + // const temp = event.exitCode; } - traceLog(`xterm-headless content\n\`\`\`\n${lines.join('\n').trim()}\n\`\`\``); - } - }); - const stream = execution.read(); - for await (const data of stream) { - xterm.write(data); - traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + }); } - } - }); + }), + ); } + /// ///////////////////////// const processEnv = normCaseKeys(this.processEnvVars); From b8ea95310579ce9b82c263652c651dee7812b480 Mon Sep 17 00:00:00 2001 From: Anthony Kim Date: Thu, 22 Aug 2024 10:00:20 -0700 Subject: [PATCH 8/8] able to get zshrc vars --- .../envCollectionActivation/service.ts | 109 +++++++++--------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/src/client/terminals/envCollectionActivation/service.ts b/src/client/terminals/envCollectionActivation/service.ts index 770b2f938031..6e542f75a026 100644 --- a/src/client/terminals/envCollectionActivation/service.ts +++ b/src/client/terminals/envCollectionActivation/service.ts @@ -13,6 +13,7 @@ import { ProgressLocation, Terminal, window, + Disposable, } from 'vscode'; import { pathExists } from 'fs-extra'; import { IExtensionActivationService } from '../../activation/types'; @@ -44,8 +45,9 @@ import { IShellIntegrationService, ITerminalDeactivateService, ITerminalEnvVarCo import { ProgressService } from '../../common/application/progressService'; @injectable() -export class TerminalEnvVarCollectionService extends Disposable - implements IExtensionActivationService, ITerminalEnvVarCollectionService { +export class TerminalEnvVarCollectionService implements IExtensionActivationService, ITerminalEnvVarCollectionService { + // extends Disposable + // implements IExtensionActivationService, ITerminalEnvVarCollectionService { public readonly supportedWorkspaceTypes = { untrustedWorkspace: false, virtualWorkspace: false, @@ -71,7 +73,7 @@ export class TerminalEnvVarCollectionService extends Disposable private separator: string; - private _shellIntegrationDisposableMap = this.register(new DisposableMap()); + // private _shellIntegrationDisposableMap = this.register(new DisposableMap()); // problem: when extension is disposed, we want terminal env collection service to be disposed. When that is disposed, we want the listeners disposed as well constructor( @@ -91,8 +93,8 @@ export class TerminalEnvVarCollectionService extends Disposable @inject(IEnvironmentVariablesProvider) private readonly environmentVariablesProvider: IEnvironmentVariablesProvider, ) { - super(); - this.this.separator = platform.osType === OSType.Windows ? ';' : ':'; + // super(); + // this.this.separator = platform.osType === OSType.Windows ? ';' : ':'; this.progressService = new ProgressService(this.shell); } @@ -194,23 +196,26 @@ export class TerminalEnvVarCollectionService extends Disposable /// //////////////////////////////////////////////////////////////// // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. // But first, try some dummy commands to see if I can get any sort of exit code. - // const myTerm = window.createTerminal(); - // window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - // if (terminal === myTerm) { - // const execution = shellIntegration.executeCommand('echo "Hello world"'); - // // const stream = execution.read(); - // // for await(const data of stream) { - // // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); - // // } - // window.onDidEndTerminalShellExecution((event) => { - // if (event.execution === execution) { - // console.log(`Command exited with code ${event.exitCode}`); // Finally getting exit code 0 if I place code here, flaky. --> failing to get exit code again - // traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); - // // const temp = event.exitCode; - // } - // }); - // } - // }); + const myTerm = window.createTerminal(); + window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + if (terminal === myTerm) { + const execution = shellIntegration.executeCommand( + 'python /Users/anthonykim/Desktop/vscode-python/python_files/printEnvVariables.py', + ); + + // const stream = execution.read(); + // for await(const data of stream) { + // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // } + window.onDidEndTerminalShellExecution((event) => { + if (event.execution === execution) { + console.log(`Command exited with code ${event.exitCode}`); + traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); + // const temp = event.exitCode; + } + }); + } + }); /// /////////////////////////////////////////////////////////// const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; traceVerbose(`Activated environment variables for ${resource?.fsPath}`, env); @@ -233,36 +238,36 @@ export class TerminalEnvVarCollectionService extends Disposable resource, shell, ); - // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. - // But first, try some dummy commands to see if I can get any sort of exit code. - const myTerm = window.createTerminal(); - - map.set( - terminal, - window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { - // can fire multiple times for single terminal ---> ATM when shell integration status changes, when its activated or working directory changes. - // listen to this once per terminal. Dispose once its done - - if (terminal === myTerm) { - const dispoable = map.get(terminal); - disposable.dispose(); - map.delete(terminal); - const execution = shellIntegration.executeCommand('echo "Hello world"'); - - // const stream = execution.read(); - // for await(const data of stream) { - // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); - // } - window.onDidEndTerminalShellExecution((event) => { - if (event.execution === execution) { - console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. - traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); - // const temp = event.exitCode; - } - }); - } - }), - ); + // // TODO: Try to get environment variable using shell integration API here -- using hidden terminal. + // // But first, try some dummy commands to see if I can get any sort of exit code. + // const myTerm = window.createTerminal(); + + // map.set( + // terminal, + // window.onDidChangeTerminalShellIntegration(async ({ terminal, shellIntegration }) => { + // // can fire multiple times for single terminal ---> ATM when shell integration status changes, when its activated or working directory changes. + // // listen to this once per terminal. Dispose once its done + + // if (terminal === myTerm) { + // const dispoable = map.get(terminal); + // disposable.dispose(); + // map.delete(terminal); + // const execution = shellIntegration.executeCommand('echo "Hello world"'); + + // // const stream = execution.read(); + // // for await(const data of stream) { + // // traceLog(`HERE ${data} HERE I AM WITH THE DATA`); + // // } + // window.onDidEndTerminalShellExecution((event) => { + // if (event.execution === execution) { + // console.log(`Command exited with code ${event.exitCode}`); // Keep getting undefined... --- placing this above gets me exit code 0. + // traceLog(`HERE ${event.exitCode} HERE I AM WITH THE EXIT CODE`); + // // const temp = event.exitCode; + // } + // }); + // } + // }), + // ); } /// /////////////////////////