Skip to content

Url::from_file_path() incorrect handling of backslash on linux #901

@nayeemrmn

Description

@nayeemrmn

Also affects Url::from_directory_path().

Backslashes in paths get left as-is. They should be percent-encoded to %5C. Currently that backslash would change to a forward-slash if the output were to be stringified and re-parsed with Url::parse(). See the following roundtrip:

fn main() {
  let path = std::path::PathBuf::from("/a/b/c\\d");
  let url = url::Url::from_file_path(&path).unwrap();
  println!("{}", url.as_str()); // file:///a/b/c\d
  let url2 = url::Url::parse(url.as_str()).unwrap();
  println!("{}", url2.as_str()); // file:///a/b/c/d
  let path2 = url2.to_file_path().unwrap();
  println!("{}", path2.display()); // /a/b/c/d
  //                         Expected /a/b/c\d
}

Expected:

fn main() {
  let path = std::path::PathBuf::from("/a/b/c\\d");
  let url = url::Url::from_file_path(&path).unwrap();
  println!("{}", url.as_str()); // file:///a/b/c%5Cd
  let url2 = url::Url::parse(url.as_str()).unwrap();
  println!("{}", url2.as_str()); // file:///a/b/c%5Cd
  let path2 = url2.to_file_path().unwrap();
  println!("{}", path2.display()); // /a/b/c\d
}

On Windows they get swapped to forward-slashes, which is correct for Windows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions