Don't pass duplicate /d flag in Windows cmd use-on-cd hook - #1579
Open
emerson-d-lopes wants to merge 1 commit into
Open
Don't pass duplicate /d flag in Windows cmd use-on-cd hook#1579emerson-d-lopes wants to merge 1 commit into
emerson-d-lopes wants to merge 1 commit into
Conversation
The generated cd.cmd always prepended /d, so a user-supplied /d was passed twice and `cd /d D:\` failed with "The syntax of the command is incorrect". Only add /d when the first argument isn't already /d. The two guarded single-line ifs are deliberate: parenthesized blocks break on paths containing ")" (e.g. "C:\Program Files (x86)"), goto labels misbehave with LF line endings (the file is stored with LF and embedded via include_bytes!), and setlocal would revert the directory change on endlocal. Fixes Schniz#1556 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U5TQ1y3yBEDx73tYTHoUBj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1556
Problem
The generated
cd.cmdalways runscd /d %*. When the user supplies their own/d(e.g.cd /d D:\), the flag is passed twice and cmd fails with:Fix
Add
/donly when the first argument isn't already/d:The shape is deliberate — each alternative fails in a non-obvious way (all verified empirically on Windows 11 cmd.exe):
if/elseblocks:%*may contain)—cd /d C:\Program Files (x86)inside a( ... )block closes the block early and breaks the command. The current single-line form handles it; a block would regress it.setlocal:endlocalreverts directory changes, which would breakcdentirely.goto/labels: the file is stored with LF endings and embedded byte-exact viainclude_bytes!; cmd's label scanning is unreliable with LF-only line endings. The two guarded ifs avoid labels altogether.cditself only accepts/dbefore the path (cd C:\ /dfails with "The system cannot find the path specified"), so positions after the first never work anyway and are passed through to nativecdto produce its normal error.Testing
WinCmd e2e tests are currently skip-listed (
e2e/describe.ts), so I verified the behavior directly on Windows 11 with a 15-case matrix against realcmd.exe, comparing old script vs new script vs nativecd:cd /d Y:\(cross-drive viasubst, dir contains.node-version)fnm use --silent-if-unchangedfirescd /D Y:\(uppercase)cd ..,cd \,cd(no args)cd "C:\Program Files"/ unquotedcd C:\Program Filescd C:\Program Files (x86)andcd /d C:\Program Files (x86).node-version/.nvmrc/FNM_VERSION_FILE_STRATEGY=recursivetriggersfnmon PATH)Also verified:
cargo test— 28/28 pass, includingshell::windows_cmd::tests::use_on_cd_quotes_macro_path.fnm.exe env --use-on-cd --shell cmdgenerates the fixed script; re-ran key cases against the generated file.e2e/repros/issue-1556.test.tsper AGENTS.md (WinCmd-only; it self-skips today but will cover this once WinCmd e2e is enabled). Compiles under jest, prettier clean.🤖 Generated with Claude Code
https://claude.ai/code/session_01U5TQ1y3yBEDx73tYTHoUBj