Skip to content

In Windows, Trailing Backslash In Quoted Path Argument Leads to Wrong Command-line Parsing #72653

@git-blame

Description

@git-blame

If a rust windows program has a command-line argument like this: .\myapp -p "C:\Program Files\" -v, it will be parsed as follows: ['.\myapp', '-p', 'C:\\Program Files\\" -v'] instead of ['.\myapp', '-p', 'C:\\Program Files\\', '-v'].

Looking over rust code, this is due to rust adhering to Microsoft standard:

/// Implements the Windows command-line argument parsing algorithm.
///
/// Microsoft's documentation for the Windows CLI argument format can be found at
/// <https://docs.microsoft.com/en-us/previous-versions//17w5ykft(v=vs.85)>.

And the link states:

A double quotation mark preceded by a backslash (") is interpreted as a literal double quotation mark character (").

So everything is behaving to spec. The problem is that if a user is running my app in powershell, tab-completion will automatically add in the trailing backslash in quoted paths. So I have to explain that this doesn't work.

Some observations:

  • Tab-completion in command prompt (cmd) does not add trailing backslash
  • Microsoft commands like dir, ls don't have a problem with trailing slash (in powershell or cmd). For example dir "C:\Program Files\" /B
  • I tried using Windows function CommandLineToArgvW through the winapi crate but it also seems to have the same problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-windowsOperating system: Windows

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions