Skip to content

redundant escape of " in command run-external #16264

Open

Description

@gravitional

Describe the bug

when I run

C:/Windows/System32/cmd.exe /E:ON /C r#'"C:/Program Files (x86)/Intel/oneAPI/setvars.bat" intel64 vs2022 && set'#

it failed with

'\"C:/Program Files (x86)/Intel/oneAPI/setvars.bat\"' is not recognized as an internal or external command,
operable program or batch file.

Here you can see, nu escape the " with \, even when the raw string sigil(r#') is used.
And the stupid cmd can only read "C:/Program Files (x86)/Intel/oneAPI/setvars.bat".
So can we avoid this escape somehow??

How to reproduce

follow the describe

Expected behavior

  1. just follow the describe

Configuration

key value
version 0.106.0
major 0
minor 106
patch 0
branch
commit_hash 1b01625
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.86.0 (05f9846f8 2025-03-31)
rust_channel 1.86.0-x86_64-pc-windows-msvc
cargo_version cargo 1.86.0 (adf9b6ad1 2025-02-28)
build_time 2025-07-23 15:13:30 +00:00
build_rust_channel release
allocator standard
features default, plugin, rustls-tls, sqlite, trash-support
installed_plugins
experimental_options example=false, reorder-cell-paths=false

Activity

  1. added
    status:needs-triage
    on Jul 26, 2025
  2. fdncred commented on Jul 26, 2025

    @fdncred
    Contributor

    Have you tried using backticks instead of raw strings? Another thing you can try is using string interpolation or using env vars like I did below.

    This is something I wrong a while back to do something similar.

    let vswhere = $'($env."ProgramFiles(x86)")\Microsoft Visual Studio\Installer\vswhere.exe'
    let installPath = ^$vswhere -latest -property installationPath
    let vsdevcmd = $'($installPath)\Common7\Tools\vsdevcmd.bat'
    let op = run-external $env.comspec "/c" $vsdevcmd "-no_logo" "-startdir=none" "-arch=x64" "-host_arch=x64" "&&" "set"
    $op | decode utf8 | lines | parse "{key}={value}" | where key != PWD and key != FILE_PWD and key != CURRENT_FILE | transpose -ird | load-env
  3. added
    A:external-commands
    A:quoting-expansion
    on Jul 26, 2025
  4. gravitional commented on Jul 26, 2025

    @gravitional
    Author

    Have you tried using backticks instead of raw strings? Another thing you can try is using string interpolation or using env vars like I did below.

    This is something I wrong a while back to do something similar.

    let vswhere = $'($env."ProgramFiles(x86)")\Microsoft Visual Studio\Installer\vswhere.exe'
    let installPath = ^$vswhere -latest -property installationPath
    let vsdevcmd = $'($installPath)\Common7\Tools\vsdevcmd.bat'
    let op = run-external $env.comspec "/c" $vsdevcmd "-no_logo" "-startdir=none" "-arch=x64" "-host_arch=x64" "&&" "set"
    $op | decode utf8 | lines | parse "{key}={value}" | where key != PWD and key != FILE_PWD and key != CURRENT_FILE | transpose -ird | load-env

    Backticks give the same result.
    But I will try your codes, thanks a lot !

  5. gravitional commented on Jul 26, 2025

    @gravitional
    Author

    Furthermore, if one use powershell, in the way below,
    it just works as one would expected,

    cmd.exe /E:ON /C "`"C:/Program Files (x86)/Intel/oneAPI/setvars.bat`" intel64 vs2022 >nul && set"
    # or
    cmd.exe /E:ON /C '"C:/Program Files (x86)/Intel/oneAPI/setvars.bat" intel64 vs2022 >nul && set'

    It is intuitive.

  6. motgenror commented on Jan 17, 2026

    @motgenror

    Thoughts on this:

    Problem

    Currently in Nushell there doesn't seem to be a way of running arbitrary .exe files without forcing the MSVCRT quoting rules on the arguments.

    Root Cause

    When Nushell (or any other Rust program in general) wants to run another program, it uses std::process::Command. Command has a concept of multiple arguments. Linux kernel, when you run a program, has a concept of multiple arguments. Windows does not - there is just one argument to running a program (called a "command line"), and it's up to the target program to decide how it wants to parse that.

    There is one convention in Windows world for how to pack multiple args into a single string - the MSVCRT one. This is what Command is encoding on Windows by default and that is where those uncalled for \ come from. Nushell trusts Rust to take care of args, and Rust devs decided that MSVCRT is the sensible default on Windows, even if there does not exist a rule saying that all Windows binaries have to follow MSVCRT, and cmd.exe doesn't.

    Solutions

    Universal escape hatch

    Add a --rawargs (bikeshed) flag to run-external that would disable std::process::Command's MSVCRT behavior - Rust already supports this escape hatch via raw_arg.

    This will allow correct usage of any Windows binary that does not conform to MSVCRT and will solve the problem universally, beyond just cmd.exe. The user would have to understand the target program's argument encoding convention on their own and construct the string with that in mind.

    The downside is that people would still trip over direct calls to cmd.exe not knowing the right way to call is run-external --rawargs.

    Example:

    run-external --rawargs cmd.exe /c r#'call "C:\Program Files\foo\bar.bat" & set'#
    

    Cmdexe-specific escape hatch

    Add an internal command that runs cmd.exe /c for you with MSVCRT encoding turned off, let's call it cmdexe (bikeshed).

    Example:

    cmdexe r#'call "C:\Program Files\foo\bar.bat" & set'#
    

    The downsides are still that people wouldn't know to run cmdexe instead of cmd.exe, and that it doesn't cover other Windows programs that don't conform to MSVCRT, so it's less useful than the universal solution.

    Detect cmd.exe being run

    That's what PowerShell is doing: detecting that it's going to spawn a cmd.exe and disables MSVCRT encoding for its args.

    Examples:

    cmd /c 'echo "Hello, world"'
    cmd.exe /c 'echo "Hello, world"'
    C:\Windows\system32\cmd.exe /c 'echo "Hello, world"'
    X:\somewhere\else\cmd.exe /c 'echo "Hello, world"'
    

    The downside is false positives - if you disable MSVCRT encoding and the target program is not a Windows cmd.exe and just happens to have the same file name, you might get very nasty bugs and even security vulnerabilities. But, this might be mitigated by verifying the target binary's certificate, i.e. what company signed it. If it's Microsoft, chances
    are good it's a Windows cmd.exe.

    Proclaim non-MSVCRT binaries support as a non-goal

    You can just say Nushell doesn't want to support non-MSVCRT external command arguments by design and point to workarounds such as placing your cmd.exe code into a dedicated .bat file and running that file from Nushell, since Rust already handles that case.

    Security concerns

    Isn't an internal cmdexe command an "eval" kind of thing?

    Only in the sense that you're able to run an external program that executes input code.

    You can already do that in Nushell with any other program:

    Powershell:

    ~> run-external powershell.exe '-NoProfile' '-Command' r#'Write-Host 'hi'; Get-Date'#
    hi
    
    Friday, January 16, 2026 23:49:30
    

    Bash:

    ~> run-external c:\msys64\usr\bin\bash.exe '-lc' r#'echo hi ; date'#
    hi
    Fri, Jan 16, 2026 23:51:02
    

    Nushell:

    ~> run-external nu.exe '-c' r#'print 'hi' ; date now'#
    hi
    Fri, 16 Jan 2026 23:51:59 +0100 (now)
    

    Cmd.exe:

    Not reliably possible because of Nu's limitation, but still:

    ~> run-external cmd.exe '/d' '/c' 'echo hi & date'
    hi
    The current date is: 2026-01-16
    Enter the new date: (yy-mm-dd)
    

    So, if you interpret "eval" as being able to run dynamically-constructed code, then this is already a feature of Nushell no matter what, even of Nushell scripts, with all of the dangers of user injected code that it brings. The injection issue is A) unsolvable in principle as long you allow running external commands, and B) orthogonal to the issue of not currently being able to run cmd.exe correctly.

  7. fdncred commented on Jan 17, 2026

    @fdncred
    Contributor

    Nice write up @motgenror. I think I'm personally leaning towards --raw-args. I've asked the core-team to chime in. They may or may not reply.

    I could also live with cmdexe named winrun and being a windows-only command, but I don't really like os-only commands that much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:external-commandsA:quoting-expansionstatus:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions