Repository navigation
fix(vscode): pick the print environment command by shell - #6091
tripleaceme wants to merge 3 commits into
Conversation
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>
|
@cmgoffena13 — closes #5608. This is the option 1 I asked about on the issue (detect the shell); the description explains why I would argue for option 2 now that it is built, and the PR is shaped so switching is a deletion of one file and one import. The part worth your eye is not fish: |
cmgoffena13
left a comment
There was a problem hiding this comment.
Small adjustments, comments added. Reminder that any new files need a // SPDX-License-Identifier: Apache-2.0 header to align to our contributing standards.
| * shell family. | ||
| */ | ||
| const PRINT_ENVIRONMENT_COMMANDS: Record<ShellFamily, string> = { | ||
| fish: 'set', |
There was a problem hiding this comment.
I think this needs to be set -x so it only lists exported variables
There was a problem hiding this comment.
Done in d239db3. Agreed — a bare set in fish also lists shell-local variables, so it was printing more than the other families do. set -x is the exported-only listing, which is what env gives on the POSIX side.
| */ | ||
| const PRINT_ENVIRONMENT_COMMANDS: Record<ShellFamily, string> = { | ||
| fish: 'set', | ||
| powershell: 'Get-ChildItem Env: | Sort-Object Name', |
There was a problem hiding this comment.
This will truncate values, we should have it be: Get-ChildItem Env: | Sort-Object Name | ForEach-Object { '{0}={1}' -f $_.Name, $_.Value }
There was a problem hiding this comment.
Done in d239db3, using your exact command. The default table view truncating at console width is the kind of thing that only shows up on a machine with long values, so good catch — and formatting each one as NAME=value also lines the output up with what env | sort prints.
| @@ -0,0 +1,94 @@ | |||
| import { describe, it, expect } from 'vitest' | |||
There was a problem hiding this comment.
Need to add in another test to that if we have pwsh and isWindows false, it still returns the powershell command correctly
There was a problem hiding this comment.
Added in d239db3 as should use the PowerShell command for pwsh off Windows, covering both /usr/local/bin/pwsh and a bare pwsh.
I checked it isn't a test that passes no matter what. The regression it guards is PowerShell being treated as Windows-only, so I gated the lookup behind !(family === 'powershell' && !isWindows) and confirmed the new test fails, then restored it and confirmed all 14 pass. Worth noting the command-layer coverage was the gap — detectShellFamily('pwsh', false) was already asserted, but nothing checked that the command came back right off Windows.
fish's bare `set` also lists shell-local variables, so it is now `set -x`, which matches what the other families print. PowerShell's default table view truncates values at the console width, so each variable is now formatted explicitly as `NAME=value`. Adds the pwsh-off-Windows case for printEnvironmentCommand, and the SPDX headers both new files were missing. Signed-off-by: Adegbite Ayoade <tripleaceme@gmail.com>
|
All three comments addressed in d239db3, replied inline. The SPDX headers were a miss on my part — both new files now carry
One caveat on the two command changes: neither fish nor PowerShell is installed on this machine, so I applied both on the reasoning rather than by running them. |
Description
Closes #5608.
sqlmesh: Print Environmentopens a terminal and sends a command to list the environment. It chose that command by platform, treating "not Windows" as "POSIX shell withenv":The command is now chosen from the shell that is actually in use. The decision lives in a pure function so it can be tested without a VS Code mock, and
printEnvironment.tsis one call into it.setsetGet-ChildItem Env: | Sort-Object NameUnrecognized shells fall back on the platform default, so nothing that works today stops working.
Two things worth knowing, both covered on the issue
The reporter's premise is not quite right. fish has real pipes and
envandsortare ordinary binaries there, soenv | sortdoes print the environment correctly in fish today. This is an idiomatic-output improvement, not a crash fix —setadditionally lists fish's own shell variables in fish's own formatting, which is what a fish user expects.The genuine breakage is on Windows, and the issue does not mention it. In PowerShell,
setis an alias forSet-Variable, which with no arguments does not print the environment — it promptsSupply values for the following parameters: Name[0]:and waits. PowerShell is VS Code's default terminal on Windows, so this command has been unusable for most Windows users. Fixed here because it is the same decision, but it does widen the change beyond the issue's text; happy to split it out if you would rather.I should be clear about confidence on that: it follows from
Set-Variable's documented behaviour rather than from a run. I have neither Windows nor fish available, so neither end of this is verified in a live shell, and the PowerShell replacement command is unverified too.Why
vscode.env.shellIt is stable API since 1.38 and this extension targets
^1.96.0. Its documentation says it is the detected default shell overridden byterminal.integrated.defaultProfile, so reading that setting directly would be strictly worse — it holds a profile name, not a path, and does not resolve the auto-detect case.process.env.SHELLis the extension host's login shell, which is not necessarily what VS Code launches, and is absent on Windows. Decisively, this command callscreateTerminalwith noshellPath, so the terminal it opens is exactly the onevscode.env.shelldescribes.Test Plan
13 tests in
src/utilities/shellCommand.test.ts, covering fish (including a Homebrew path), the POSIX family, cmd and PowerShell with and without.exe, a POSIX shell on Windows (Git Bash — POSIX wins over platform), a bare name with no path, mixed case, an unknown shell, and no shell reported at all (vscode.env.shellis''where none exists).Rather than reverting once, I checked the tests against three separate mutants, because reverting to the original behaviour leaves some tests green for the right reason:
isWindows ? 'set' : 'env | sort'setcmdmapped to a wrong valueUnion across the three: every one of the 13 tests fails under at least one mutant, so none is passing vacuously.
Nothing exercises
printEnvironment.tsitself, since there is no VS Code mock in this repo and I did not add one; the tests pin the decision function only.Checklist
make styleand fixed any issuesmake fast-test)git commit -s) per the DCO