Repository navigation
fix running file-associated scripts with ./ paths on Windows (#19584) - #19636
Conversation
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
The ./script.nu fix works: on Windows, ./hello.nu, ./hello and ./sub/hello.nu all run through the .nu association on the branch. Handing cmd.exe the resolved path also hands it the cwd, though, and that breaks scripts in directories that 0.117.1 handles fine.
Findings: 1
Open (2)
Notes from my clanker, checked on 175b92d. Built the branch on Windows 10 (19045) with .NU in PATHEXT and .nu associated with nu 0.117.1, and ran 28 script calls (25 .nu, 3 .vbs) through the branch, through 0.117.1 (this code matches main) and through a build with the suggested line. On Windows, clippy with CI's deny flags is clean and cargo test -p nu-command --lib -- cmd_script_path run_external passes (4 tests). On macOS, cargo fmt --check, typos and clippy with CI's flags are clean. I didn't run clippy on the suggested line.
|
I kind of worry about calling wscript failing in ci because if it does it'll post a pop up and hand the entire ci |
|
@fdncred On the WScript worry: in the last run, Windows / root passed and all the |
|
Thanks |



Description
Fixes #19584.
On Windows, a script that runs through its file association (for example a
.nu,.pyor.vbsfile whose extension is inPATHEXT) worked as.\script.nubut failed as./script.nu:Cause: for these scripts
run-externallaunchescmd.exe /D /C <name>so Windows applies the file association. The name was passed exactly as typed, andcmd.exetreats/as a switch character, so it read./test.nuas the command.followed by the switch/test.nu.Fix: for file-associated scripts, the name as typed is normalized before it's handed to
cmd.exe. The new helpercmd_script_pathre-joins the name's path components, which turns every/into\and drops redundant.components while keeping a leading., e.g../sub/./test.nu→.\sub\test.nu. Only the typed name reaches cmd.exe, never the cwd or a PATH directory, so spaces or characters like&in those directories can't affect cmd.exe's parsing.CMD internal commands (
echo,assoc,start, …) still receive the name as typed, so their behaviour is unchanged..exe,.com,.bat,.cmdand.ps1files don't go through this code path.Before / after
Tested on Windows 10 Pro 19045 with
.vbsfiles, which Windows associates with WScript by default (.VBSis in the defaultPATHEXT). The test script writes a marker file, soRANmeans it really executed.Before (0.116.1 release;
mainat 594dd47 behaves the same):After (this PR):
Tests
crates/nu-command/tests/commands/run_external.rs(mod file_association), using.vbs(default WScript association, no registry change). Each prefix (bare,^,run-external) × script is its own rstest case:can_run_file_associated_scripts_with_forward_slashes:./hello.vbs,./hello,./sub/hello.vbs,./sub/./hello.vbs,.\sub\hello.vbs(15 cases)can_run_file_associated_scripts_in_dirs_with_cmd_special_chars: cwdJohn Doe/R&D, with.\hello.vbs,./hello.vbs,.\hello.vbs "a b"(18 cases)mainthe 18 forward-slash cases fail, and against the first version of this PR (resolved path) the special-chars cases fail.cmd_script_path_uses_backslashes(7 rstest cases) for the normalization itself.cargo fmt --all -- --checkcargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_used -D clippy::unchecked_time_subtractioncargo test -p nu-command --lib run_external(10 passed)cargo test -p nu-command --test tests -- commands::run_external(124 passed, 3 ignored Linux-only)User-facing changes (Release notes)
Fixed an issue on Windows where running a file-associated script with a forward-slash path such as
./script.nufailed with'.' is not recognized as an internal or external command, while.\script.nuworked.Additional notes
./script.nufails while.\script.nuworks with file associations #19584