Skip to content

Commit e443846

Browse files
committed
fix(vscode): pick the print environment command by shell
The print environment command opened a terminal with the user's default shell and then branched only on the platform, sending `set` on Windows and `env | sort` everywhere else. That assumes every non-Windows shell is a POSIX shell and every Windows shell is cmd. Resolve `vscode.env.shell` to a shell family instead and send the command that family accepts: `set` for fish, `env | sort` for POSIX shells, `set` for cmd and `Get-ChildItem Env: | Sort-Object Name` for PowerShell. `set` in PowerShell is an alias for `Set-Variable`, which prompts for a variable name rather than printing the environment, so the previous command did not work in the default Windows shell. An unrecognised shell falls back on the platform command, which keeps the previous behaviour for any shell not listed. Signed-off-by: Adegbite Ayoade <tripleaceme@gmail.com>
1 parent a6ba67d commit e443846

3 files changed

Lines changed: 171 additions & 8 deletions

File tree

‎vscode/extension/src/commands/printEnvironment.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import { getSqlmeshEnvironment } from '../utilities/sqlmesh/sqlmesh'
33
import { isErr } from '@bus/result'
44
import { IS_WINDOWS } from '../utilities/isWindows'
5+
import { printEnvironmentCommand } from '../utilities/shellCommand'
56

67
export function printEnvironment() {
78
return async () => {
@@ -23,14 +24,9 @@ export function printEnvironment() {
2324
// Show the terminal
2425
terminal.show()
2526

26-
// Run the appropriate command to display environment variables
27-
if (IS_WINDOWS) {
28-
// On Windows, use 'set' command
29-
terminal.sendText('set')
30-
} else {
31-
// On Unix-like systems, use 'env' command
32-
terminal.sendText('env | sort')
33-
}
27+
// Run the command the user's shell understands, since the terminal is
28+
// opened with the default shell rather than a shell we pick
29+
terminal.sendText(printEnvironmentCommand(vscode.env.shell, IS_WINDOWS))
3430

3531
// Show a notification
3632
vscode.window.showInformationMessage(
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { detectShellFamily, printEnvironmentCommand } from './shellCommand'
3+
4+
describe('detectShellFamily', () => {
5+
it('should detect fish', () => {
6+
expect(detectShellFamily('/usr/bin/fish', false)).toBe('fish')
7+
expect(detectShellFamily('/opt/homebrew/bin/fish', false)).toBe('fish')
8+
})
9+
10+
it('should detect POSIX shells', () => {
11+
expect(detectShellFamily('/bin/bash', false)).toBe('posix')
12+
expect(detectShellFamily('/bin/zsh', false)).toBe('posix')
13+
expect(detectShellFamily('/bin/sh', false)).toBe('posix')
14+
expect(detectShellFamily('/usr/bin/dash', false)).toBe('posix')
15+
expect(detectShellFamily('/usr/bin/ksh', false)).toBe('posix')
16+
})
17+
18+
it('should detect Windows shells, ignoring the executable extension', () => {
19+
expect(detectShellFamily('C:\\Windows\\System32\\cmd.exe', true)).toBe(
20+
'cmd',
21+
)
22+
expect(
23+
detectShellFamily(
24+
'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
25+
true,
26+
),
27+
).toBe('powershell')
28+
expect(
29+
detectShellFamily('C:\\Program Files\\PowerShell\\7\\pwsh.exe', true),
30+
).toBe('powershell')
31+
})
32+
33+
it('should detect a POSIX shell installed on Windows', () => {
34+
expect(
35+
detectShellFamily('C:\\Program Files\\Git\\bin\\bash.exe', true),
36+
).toBe('posix')
37+
})
38+
39+
it('should accept a bare shell name without a path', () => {
40+
expect(detectShellFamily('fish', false)).toBe('fish')
41+
expect(detectShellFamily('pwsh', false)).toBe('powershell')
42+
})
43+
44+
it('should ignore the case of the shell name', () => {
45+
expect(detectShellFamily('/usr/bin/FISH', false)).toBe('fish')
46+
expect(detectShellFamily('C:\\WINDOWS\\SYSTEM32\\CMD.EXE', true)).toBe(
47+
'cmd',
48+
)
49+
})
50+
51+
it('should fall back on the platform for an unknown shell', () => {
52+
expect(detectShellFamily('/usr/bin/elvish', false)).toBe('posix')
53+
expect(detectShellFamily('C:\\tools\\elvish.exe', true)).toBe('cmd')
54+
})
55+
56+
it('should fall back on the platform when no shell is reported', () => {
57+
expect(detectShellFamily('', false)).toBe('posix')
58+
expect(detectShellFamily('', true)).toBe('cmd')
59+
expect(detectShellFamily(undefined, false)).toBe('posix')
60+
expect(detectShellFamily(undefined, true)).toBe('cmd')
61+
})
62+
})
63+
64+
describe('printEnvironmentCommand', () => {
65+
it('should use set in fish', () => {
66+
expect(printEnvironmentCommand('/usr/bin/fish', false)).toBe('set')
67+
})
68+
69+
it('should use env | sort in POSIX shells', () => {
70+
expect(printEnvironmentCommand('/bin/bash', false)).toBe('env | sort')
71+
expect(printEnvironmentCommand('/bin/zsh', false)).toBe('env | sort')
72+
expect(printEnvironmentCommand('/bin/sh', false)).toBe('env | sort')
73+
})
74+
75+
it('should use set in cmd', () => {
76+
expect(
77+
printEnvironmentCommand('C:\\Windows\\System32\\cmd.exe', true),
78+
).toBe('set')
79+
})
80+
81+
it('should not use set in PowerShell, where it prompts for a variable name', () => {
82+
expect(
83+
printEnvironmentCommand(
84+
'C:\\Program Files\\PowerShell\\7\\pwsh.exe',
85+
true,
86+
),
87+
).toBe('Get-ChildItem Env: | Sort-Object Name')
88+
})
89+
90+
it('should keep the previous behaviour when the shell is unknown', () => {
91+
expect(printEnvironmentCommand(undefined, false)).toBe('env | sort')
92+
expect(printEnvironmentCommand(undefined, true)).toBe('set')
93+
})
94+
})
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* The shell families we know how to print environment variables in.
3+
*/
4+
export type ShellFamily = 'fish' | 'powershell' | 'cmd' | 'posix'
5+
6+
/**
7+
* Commands that list the environment variables of the current shell, one per
8+
* shell family.
9+
*/
10+
const PRINT_ENVIRONMENT_COMMANDS: Record<ShellFamily, string> = {
11+
fish: 'set',
12+
powershell: 'Get-ChildItem Env: | Sort-Object Name',
13+
cmd: 'set',
14+
posix: 'env | sort',
15+
}
16+
17+
const SHELL_FAMILIES_BY_NAME: Record<string, ShellFamily> = {
18+
fish: 'fish',
19+
powershell: 'powershell',
20+
pwsh: 'powershell',
21+
cmd: 'cmd',
22+
bash: 'posix',
23+
zsh: 'posix',
24+
sh: 'posix',
25+
dash: 'posix',
26+
ksh: 'posix',
27+
ash: 'posix',
28+
}
29+
30+
/**
31+
* Extracts the executable name from a shell path, which may use either
32+
* separator and may carry a Windows executable extension.
33+
*/
34+
const shellName = (shellPath: string): string => {
35+
const base = shellPath.split(/[\\/]/).pop() ?? ''
36+
return base.toLowerCase().replace(/\.exe$/, '')
37+
}
38+
39+
/**
40+
* Resolves a shell path to the family whose syntax it accepts.
41+
*
42+
* Unrecognized shells fall back on the platform default, which keeps POSIX
43+
* shells we have not listed working.
44+
*
45+
* @param shellPath The path reported by `vscode.env.shell`, which is empty in
46+
* environments without a shell
47+
* @param isWindows Whether the extension host runs on Windows
48+
*/
49+
export const detectShellFamily = (
50+
shellPath: string | undefined,
51+
isWindows: boolean,
52+
): ShellFamily => {
53+
const family = shellPath
54+
? SHELL_FAMILIES_BY_NAME[shellName(shellPath)]
55+
: undefined
56+
if (family) {
57+
return family
58+
}
59+
return isWindows ? 'cmd' : 'posix'
60+
}
61+
62+
/**
63+
* Returns the command that prints all environment variables in the given
64+
* shell.
65+
*
66+
* @param shellPath The path reported by `vscode.env.shell`, which is empty in
67+
* environments without a shell
68+
* @param isWindows Whether the extension host runs on Windows
69+
*/
70+
export const printEnvironmentCommand = (
71+
shellPath: string | undefined,
72+
isWindows: boolean,
73+
): string => PRINT_ENVIRONMENT_COMMANDS[detectShellFamily(shellPath, isWindows)]

0 commit comments

Comments
 (0)