Skip to content
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

Expose shell's environment - bash #237602

Merged
merged 71 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from 70 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
714bb39
start terminal shell env proposed
anthonykim1 Sep 17, 2024
3c412ff
fix typo
anthonykim1 Sep 17, 2024
f214756
progress on shellEnvDetectionCapability, mainThreadTerminalSI
anthonykim1 Sep 19, 2024
3923373
update IShellEnvDetectionCapability interface
anthonykim1 Sep 19, 2024
0465588
touch up on $shellEnvChange
anthonykim1 Sep 19, 2024
e1d611a
adjust IShellEnvDetectionCapability
anthonykim1 Sep 19, 2024
98131e7
properly listen to envChangeEvent
anthonykim1 Sep 19, 2024
e37f119
Serialize env map, expose on exthost
anthonykim1 Nov 4, 2024
a0602bc
Merge remote-tracking branch 'upstream/main' into shellEnvAPI
anthonykim1 Nov 4, 2024
94c6574
start adding to zsh script
anthonykim1 Nov 4, 2024
e4ab9e8
receive environment variable in extension host, properly escape "
anthonykim1 Nov 19, 2024
e2b4ca1
clean up
anthonykim1 Nov 19, 2024
1cf51a5
Add TODO: properly escape double quotes, figure out why JSON parse fa…
anthonykim1 Nov 21, 2024
f0b1578
Fix nonce check, ignore PS1 for now in bash
Tyriar Dec 2, 2024
1b2eb1f
Add some simple PS1 string tests to deserializeMessage
Tyriar Dec 2, 2024
0956b86
New approach of sending env entries separately
Tyriar Dec 2, 2024
bb735f8
be able to get EnvSingleVar
anthonykim1 Dec 3, 2024
a1503da
few comments
anthonykim1 Jan 4, 2025
61653d5
add function signature for start, set, end environment var
anthonykim1 Jan 5, 2025
db981ae
implement EnvStart, EnvEntry, EnvEnd for single env entry
anthonykim1 Jan 6, 2025
a25a9a9
deserialize env value for EnvEntry
anthonykim1 Jan 6, 2025
7fe7c26
Remove unncessary comments
anthonykim1 Jan 6, 2025
338514c
only leave pwsh in this PR and exclude other shells
anthonykim1 Jan 7, 2025
9b97e1b
keep exlcuding other shell env - only pwsh should remain
anthonykim1 Jan 7, 2025
fc9f3f5
Update src/vs/workbench/api/common/extHostTerminalShellIntegration.ts
anthonykim1 Jan 8, 2025
bb93fdd
Update src/vscode-dts/vscode.proposed.terminalShellEnv.d.ts
anthonykim1 Jan 8, 2025
0ca8a75
Update src/vs/workbench/contrib/terminal/common/scripts/shellIntegrat…
anthonykim1 Jan 8, 2025
935f14a
Update src/vs/workbench/contrib/terminal/common/scripts/shellIntegrat…
anthonykim1 Jan 8, 2025
2f7b969
Update src/vs/workbench/contrib/terminal/common/scripts/shellIntegrat…
anthonykim1 Jan 8, 2025
cb997d5
Update src/vs/workbench/api/common/extHostTerminalShellIntegration.ts
anthonykim1 Jan 8, 2025
b6b673f
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
anthonykim1 Jan 8, 2025
0ab7322
Update src/vs/workbench/api/common/extHost.protocol.ts
anthonykim1 Jan 8, 2025
fe93145
Update src/vs/workbench/api/browser/mainThreadTerminalShellIntegratio…
anthonykim1 Jan 8, 2025
badfb37
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
anthonykim1 Jan 8, 2025
3f156f9
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
anthonykim1 Jan 8, 2025
4e8c2cb
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
anthonykim1 Jan 8, 2025
333c8d5
Update src/vs/platform/terminal/common/capabilities/capabilities.ts
anthonykim1 Jan 8, 2025
c68c58a
Update src/vs/workbench/api/browser/mainThreadTerminalShellIntegratio…
anthonykim1 Jan 8, 2025
944ce9a
Update src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts
anthonykim1 Jan 8, 2025
b2afe32
Update src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts
anthonykim1 Jan 8, 2025
dbbe2df
Update src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts
anthonykim1 Jan 8, 2025
0b248e2
add comment for ShellEnvDetection
anthonykim1 Jan 8, 2025
d246ba7
change envs in shellEnvDetectionCapability to env
anthonykim1 Jan 8, 2025
6b081da
Mention escaping character for EnvJSON similar to commandLine
anthonykim1 Jan 8, 2025
d555813
Do not fire env event if env has not changed
anthonykim1 Jan 8, 2025
3591fa4
add link to CommandLine
anthonykim1 Jan 9, 2025
143a4cc
follow main branch format so I avoid merge conflict
anthonykim1 Jan 9, 2025
6509fa1
Merge branch 'main' into shellEnvPwshOnly
anthonykim1 Jan 9, 2025
03fe326
remove resolved TODO
anthonykim1 Jan 9, 2025
d501cee
Update src/vs/workbench/api/browser/mainThreadTerminalShellIntegratio…
anthonykim1 Jan 9, 2025
e94cf4d
Update src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts
anthonykim1 Jan 9, 2025
0b831a5
use vscode object equals
anthonykim1 Jan 9, 2025
5f9f2db
Merge branch 'main' into shellEnvPwshOnly
Tyriar Jan 9, 2025
f853a1a
add zsh update_env for shellIntegration-zsh.sh
anthonykim1 Jan 9, 2025
a438864
add EnvStart, EnvEntry, EnvEnd
anthonykim1 Jan 9, 2025
df71492
why doesnt if [ "$__vsc_stable" = "0" ]; work
anthonykim1 Jan 9, 2025
2a9d2d7
add test to check shellPath
anthonykim1 Jan 9, 2025
2aee3f0
Merge branch 'main' into shellEnvBashOnly
anthonykim1 Jan 9, 2025
fac9a74
stop messing with formatting
anthonykim1 Jan 9, 2025
e372a9d
try to be more detail in testing
anthonykim1 Jan 10, 2025
29b59e5
clean up
anthonykim1 Jan 10, 2025
e3ffe10
dont change the format
anthonykim1 Jan 10, 2025
6bc4f46
properly use stable/insider flag so update_env on insiders
anthonykim1 Jan 10, 2025
5560bdc
modify test after feedback
anthonykim1 Jan 10, 2025
9c42014
Merge branch 'main' into shellEnvBashOnly
anthonykim1 Jan 10, 2025
9032f16
rename to *EnvironmentSingleVar and make it transactional via _pendin…
anthonykim1 Jan 10, 2025
f4c32eb
add docs for *EnvSingle and update bash script to conform to *EnvSingle
anthonykim1 Jan 10, 2025
fbde338
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
anthonykim1 Jan 10, 2025
06c4ce8
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
anthonykim1 Jan 10, 2025
532ef41
remove _env.clear from start and make _env non-readonly
anthonykim1 Jan 10, 2025
9b6a10e
Update src/vs/platform/terminal/common/capabilities/shellEnvDetection…
Tyriar Jan 10, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ import { assertNoRpc } from '../utils';
await closeTerminalAsync(terminal);
});

if (platform() === 'darwin' || platform() === 'linux') {
test('Test if env is set', async () => {
const { shellIntegration } = await createTerminalAndWaitForShellIntegration();
const env = shellIntegration.env;
ok(env);
ok(env.PATH);
ok(env.PATH.length > 0, 'env.PATH should have a length greater than 0');
});
}

test('execution events should fire in order when a command runs', async () => {
const { terminal, shellIntegration } = await createTerminalAndWaitForShellIntegration();
const events: string[] = [];
Expand Down
3 changes: 3 additions & 0 deletions src/vs/platform/terminal/common/capabilities/capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export interface IShellEnvDetectionCapability {
readonly onDidChangeEnv: Event<Map<string, string>>;
get env(): Map<string, string>;
setEnvironment(envs: { [key: string]: string | undefined } | undefined, isTrusted: boolean): void;
startEnvironmentSingleVar(isTrusted: boolean): void;
setEnvironmentSingleVar(key: string, value: string | undefined, isTrusted: boolean): void;
endEnvironmentSingleVar(isTrusted: boolean): void;
}

export const enum CommandInvalidationReason {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { equals } from '../../../../base/common/objects.js';
export class ShellEnvDetectionCapability extends Disposable implements IShellEnvDetectionCapability {
readonly type = TerminalCapability.ShellEnvDetection;

private readonly _env: Map<string, string> = new Map();
private _env: Map<string, string> = new Map();
private _pendingEnv: Map<string, string> | undefined;
get env(): Map<string, string> { return this._env; }
Tyriar marked this conversation as resolved.
Show resolved Hide resolved

private readonly _onDidChangeEnv = this._register(new Emitter<Map<string, string>>());
Expand All @@ -36,4 +37,30 @@ export class ShellEnvDetectionCapability extends Disposable implements IShellEnv
// Convert to event and fire event
this._onDidChangeEnv.fire(this._env);
}

startEnvironmentSingleVar(isTrusted: boolean): void {
if (!isTrusted) {
return;
}
this._pendingEnv = new Map();
}
setEnvironmentSingleVar(key: string, value: string | undefined, isTrusted: boolean): void {
if (!isTrusted) {
return;
}
if (key !== undefined && value !== undefined) {
this._pendingEnv?.set(key, value);
}
}
endEnvironmentSingleVar(isTrusted: boolean): void {
if (!isTrusted) {
return;
}
if (!this._pendingEnv) {
return;
}
this._env = this._pendingEnv;
this._pendingEnv = undefined;
this._onDidChangeEnv.fire(this._env);
}
}
46 changes: 46 additions & 0 deletions src/vs/platform/terminal/common/xterm/shellIntegrationAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@ const enum VSCodeOscPt {
*/
EnvJson = 'EnvJson',

/**
* The start of the collecting user's environment variables individually.
* Clears any environment residuals in previous sessions.
*
* Format: `OSC 633 ; EnvSingleStart ; <Nonce>`
*
* WARNING: This sequence is unfinalized, DO NOT use this in your shell integration script.
*/
EnvSingleStart = 'EnvSingleStart',

/**
* Sets an entry of single environment variable to transactional pending map of environment variables.
*
* Format: `OSC 633 ; EnvSingleEntry ; <EnvironmentKey> ; <EnvironmentValue> ; <Nonce>`
*
* WARNING: This sequence is unfinalized, DO NOT use this in your shell integration script.
*/
EnvSingleEntry = 'EnvSingleEntry',

/**
* The end of the collecting user's environment variables individually.
* Clears any pending environment variables and fires an event that contains user's environment.
*
* Format: `OSC 633 ; EnvSingleEnd ; <Nonce>`
*
* WARNING: This sequence is unfinalized, DO NOT use this in your shell integration script.
*/
EnvSingleEnd = 'EnvSingleEnd'
}

/**
Expand Down Expand Up @@ -446,6 +474,24 @@ export class ShellIntegrationAddon extends Disposable implements IShellIntegrati
}
return true;
}
case VSCodeOscPt.EnvSingleStart: {
this._createOrGetShellEnvDetection().startEnvironmentSingleVar(args[0] === this._nonce);
return true;
}
case VSCodeOscPt.EnvSingleEntry: {
const arg0 = args[0];
const arg1 = args[1];
const arg2 = args[2];
if (arg0 !== undefined && arg1 !== undefined) {
const env = deserializeMessage(arg1);
this._createOrGetShellEnvDetection().setEnvironmentSingleVar(arg0, env, arg2 === this._nonce);
}
return true;
}
case VSCodeOscPt.EnvSingleEnd: {
this._createOrGetShellEnvDetection().endEnvironmentSingleVar(args[0] === this._nonce);
return true;
}
case VSCodeOscPt.RightPromptStart: {
this._createOrGetCommandDetection(this._terminal).handleRightPromptStart();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ __vsc_update_cwd() {
builtin printf '\e]633;P;Cwd=%s\a' "$(__vsc_escape_value "$__vsc_cwd")"
}

__vsc_update_env() {
builtin printf '\e]633;EnvSingleStart;%s;\a' $__vsc_nonce
for var in $(compgen -v); do
if printenv "$var" >/dev/null 2>&1; then
value=$(builtin printf '%s' "${!var}")
builtin printf '\e]633;EnvSingleEntry;%s;%s;%s\a' "$var" "$(__vsc_escape_value "$value")" $__vsc_nonce
fi
done
builtin printf '\e]633;EnvSingleEnd;%s;\a' $__vsc_nonce
}

__vsc_command_output_start() {
if [[ -z "${__vsc_first_prompt-}" ]]; then
builtin return
Expand All @@ -240,6 +251,10 @@ __vsc_command_complete() {
builtin printf '\e]633;D;%s\a' "$__vsc_status"
fi
__vsc_update_cwd

if [ "$__vsc_stable" = "0" ]; then
__vsc_update_env
fi
}
__vsc_update_prompt() {
# in command execution
Expand Down Expand Up @@ -269,6 +284,10 @@ __vsc_precmd() {
fi
__vsc_first_prompt=1
__vsc_update_prompt

if [ "$__vsc_stable" = "0" ]; then
__vsc_update_env
fi
}

__vsc_preexec() {
Expand Down
Loading