Repository navigation
Run scripts of any file extension in PATHEXT on Windows - #15611
Conversation
The `which()` on line 94 accounts for PATHEXT, so comparing against PATHEXT again is redundant. We need only check that the file extension is not one which Windows can run directly.
| let paths = nu_engine::env::path_str(engine_state, stack, call.head)?; | ||
|
|
||
| // On Windows, the user could have run the cmd.exe built-in "assoc" command | ||
| // Example: "assoc .nu=nuscript" and then run the cmd.exe built-in "ftype" command |
There was a problem hiding this comment.
I'd like to keep these examples because they're not commonly known, even by seasoned windows users.
There was a problem hiding this comment.
Sure thing. My intent was to update the comment to reflect that this behavior is now generic, no longer just limited to nushell scripts. But keeping an explicit example in there makes sense to me.
fdncred
left a comment
There was a problem hiding this comment.
I generally agree with what you're doing here. I just want to change the comments around these changes for clarity. This subject is a little tricky, so I want it to be as clear as possible.
|
I plan on looking at this closer tomorrow on my windows systems. |
fdncred
left a comment
There was a problem hiding this comment.
I've looked at this again. The comments are well enough now. I also tested with a nu script, a ps1 script, and a bat file. The nu script and bat file worked well when invoking in the same folder without the extension, but the ps1 file would not execute and I'm not sure why. It's also broken this way before this PR, so I'm not laying this blame on this PR.
To recap:
- t.nu should be executable with t or .\t on windows, and it works
- b.bat should be executable with b or .\b on windows, and it works
- p.ps1 should be executable with p or .\p on windows, and it does not work
We can save 3 for a later PR but it would be nice if it worked. I should be executable without the extension, right?
|
Just a sanity check from a security perspective: I assume we can consider the components of |
I looked into this. First of all, make sure that Even after adding it, I still got an error. I think the error is occurring in the This function errors if the passed in file does not exist. It doesn't do anything fancy with PATHEXT, so it is unable to rectify that the script without the file extension is actually present where it is expected to be. |
|
You're right, .PS1 wasn't in my PATHEXT. So, it seems to be working as expected. |
@sholderbach Your concern is valid. I can confirm that a script in the CWD could shadow an executable on the path. For example, I have I checked how this is handled in powershell. Powershell will not run a script in the CWD unless it is started with The shadowing behavior is currently how nushell behaves for other file extensions, such as Looking a little through git history, I think this was done to replicate the behavior of cmd.exe which, unlike powershell, will run a script or executable in the CWD regardless of whether .\ is included or not.
I think there are two ways to proceed:
I think (2) is the better way to go, but ultimately I don't think I should be the one making that decision. The one hang-up with (2) is that it could break existing functionality for users who are used to running scripts or executables from their current directory without Let me know your thoughts. I'm happy to go either way. |
|
I'd leave it as is. I believe this issue came up when I made these changes last time. It was intended to work as cmd.exe does on Windows. |
|
Thanks for the explanation @hfrentzel ! I agree with your assessment that this PR doesn't make anything existing worse. So sign off from me on that front for the PR. I agree with you that I prefer the choices that PowerShell made or are closer to POSIX. But if we get a majority for that opinion we should do that in a separate PR. I think |
|
Ok, let's run with this update. Thanks! |
… (#19636) ## Description Fixes #19584. On Windows, a script that runs through its file association (for example a `.nu`, `.py` or `.vbs` file whose extension is in `PATHEXT`) worked as `.\script.nu` but failed as `./script.nu`: ``` > ./test.nu '.' is not recognized as an internal or external command, operable program or batch file. ``` **Cause:** for these scripts `run-external` launches `cmd.exe /D /C <name>` so Windows applies the file association. The name was passed exactly as typed, and `cmd.exe` treats `/` as a switch character, so it read `./test.nu` as 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 helper `cmd_script_path` re-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`, `.cmd` and `.ps1` files don't go through this code path. ### Before / after Tested on Windows 10 Pro 19045 with `.vbs` files, which Windows associates with WScript by default (`.VBS` is in the default `PATHEXT`). The test script writes a marker file, so `RAN` means it really executed. Before (0.116.1 release; `main` at 594dd47 behaves the same): ``` .> .\hello.vbs => RAN .> ./hello.vbs => FAILED '.' is not recognized as an internal or external command, .> ./hello => FAILED '.' is not recognized as an internal or external command, .> ./sub/hello.vbs => FAILED '.' is not recognized as an internal or external command, .> ./sub/./hello.vbs => FAILED '.' is not recognized as an internal or external command, .> "./sub dir/hello.vbs" => RAN John Doe> .\hello.vbs "a b" => RAN John Doe> ./hello.vbs "a b" => FAILED '.' is not recognized as an internal or external command, R&D> .\hello.vbs => RAN x&whoami&y> .\hello.vbs => RAN ^echo cmd-builtin-ok => cmd-builtin-ok ``` After (this PR): ``` .> .\hello.vbs => RAN .> ./hello.vbs => RAN .> ./hello => RAN .> ./sub/hello.vbs => RAN .> ./sub/./hello.vbs => RAN .> "./sub dir/hello.vbs" => RAN John Doe> .\hello.vbs "a b" => RAN John Doe> ./hello.vbs "a b" => RAN R&D> .\hello.vbs => RAN x&whoami&y> .\hello.vbs => RAN ^echo cmd-builtin-ok => cmd-builtin-ok ``` ### Tests - New Windows integration tests in `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`: cwd `John Doe` / `R&D`, with `.\hello.vbs`, `./hello.vbs`, `.\hello.vbs "a b"` (18 cases) - Checked that they catch the bugs: against `main` the 18 forward-slash cases fail, and against the first version of this PR (resolved path) the special-chars cases fail. - Windows-only unit test `cmd_script_path_uses_backslashes` (7 rstest cases) for the normalization itself. - Ran locally on Windows: - `cargo fmt --all -- --check` - `cargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_used -D clippy::unchecked_time_subtraction` - `cargo 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.nu` failed with `'.' is not recognized as an internal or external command`, while `.\script.nu` worked. ## Additional notes - Fixes #19584 - Related: #6440, #15611 (running scripts through their file association)
Description
On Windows, I would like to be able to call a script directly in nushell and have that script be found in the PATH and run based on filetype associations and PATHEXT.
There have been previous discussions related to this feature, see #6440 and #15476. The latter issue is only a few weeks old, and after taking a look at it and the resultant PR I found that currently nushell is hardcoded to support only running nushell (.nu) scripts in this way.
This PR seeks to make this functionality more generic. Instead of checking that the file extension is explicitly
NU, it instead checks that it is not one ofCOM,EXE,BAT,CMD, orPS1. The first four of these are extensions that Windows can figure out how to run on its own. This is implied by the output offtypefor any of these extensions, which shows that files are just run without a calling command anyway.PS1 files are ignored because they are handled as a special in later logic.
In implementing this I initially tried to fetch the value of PATHEXT and confirm that the file extension was indeed in PATHEXT. But I determined that because
which()respects PATHEXT, this would be redundant; any executable that is found bywhichis already going to have an extension in PATHEXT. It is thus only necessary to check that it isn't one of the few extensions that should be called directly, without the use ofcmd.exe.There are some small formatting changes to
run_external.rsin the PR as a result of runningcargo fmtthat are not entirely related to the code I modified. I can back out those changes if that is desired.User-Facing Changes
Behavior for
.nuscripts will not change. Users will still need to ensure they have PATHEXT and filetype associations set correctly for them to work, but this will now also apply to scripts of other types.