rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.5k stars 2.37k forks source link

The file path in the diagnostic message contains duplicate separators on windows #14227

Open ahaoboy opened 1 month ago

ahaoboy commented 1 month ago

Problem

When using a relative path in a workspace subproject, the file path in the diagnostic message contains duplicate separators

happened

mpv-easy-ext\./src/main.rs:2:7

expected

mpv-easy-ext/src/main.rs:2:7
 cargo clippy
    Checking mpv-easy-ext v0.1.0 (C:\wt\clippy-path\mpv-easy-ext)
warning: unused variable: `a`
 --> mpv-easy-ext\./src/main.rs:2:7
  |
2 |   let a = 1;
  |       ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `mpv-easy-ext` (bin "mpv") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.18s

Steps

git clone https://github.com/ahaoboy/cargo-clippy-path

cd cargo-clippy-path

cargo clippy

Possible Solution(s)

Use clean-path to transform the output path

Notes

No response

Version

cargo 1.81.0-nightly (154fdac39 2024-07-07)
release: 1.81.0-nightly
commit-hash: 154fdac39ae9629954e19e9986fd2cf2cdd8d964
commit-date: 2024-07-07
host: x86_64-pc-windows-msvc
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.8.0-DEV (sys:0.4.73+curl-8.8.0 vendored ssl:Schannel)
os: Windows 10.0.22635 (Windows 11 Professional) [64-bit]
ahaoboy commented 1 month ago

I don't know if it's a problem with cargo itself or if a dependent library doesn't process the path for Windows. https://github.com/rust-lang/rust-clippy/issues/13078

epage commented 1 month ago

Couple of questions

epage commented 1 month ago

Side note: I wonder if this is related to target work I did like #13849.

ahaoboy commented 1 month ago

cargo check

    Checking mpv-easy-ext v0.1.0 (C:\wt\clippy-path\mpv-easy-ext)
warning: unused variable: `a`
 --> mpv-easy-ext\./src/main.rs:2:7
  |
2 |   let a = 1;
  |       ^ help: if this is intentional, prefix it with an underscore: `_a`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `mpv-easy-ext` (bin "mpv") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s

 win…\clippy-path   main  

cargo -Vv

cargo 1.79.0 (ffa9cf99a 2024-06-03)
release: 1.79.0
commit-hash: ffa9cf99a594e59032757403d4c780b46dc2c43a
commit-date: 2024-06-03
host: x86_64-pc-windows-msvc
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:Schannel)
os: Windows 10.0.22635 (Windows 11 Professional) [64-bit]

cargo metadata


warning: please specify `--format-version` flag explicitly to avoid compatibility problems
{"packages":[{"name":"mpv-easy-ext","version":"0.1.0","id":"path+file:///C:/wt/clippy-path/mpv-easy-ext#0.1.0","license":"MIT","license_file":null,"description":"mpv-easy rust extension","source":null,"dependencies":[],"targets":[{"kind":["bin"],"crate_types":["bin"],"name":"mpv","src_path":"C:\\wt\\clippy-path\\mpv-easy-ext\\./src/main.rs","edition":"2021","doc":true,"doctest":false,"test":true}],"features":{},"manifest_path":"C:\\wt\\clippy-path\\mpv-easy-ext\\Cargo.toml","metadata":null,"publish":null,"authors":["ahaoboy"],"categories":[],"keywords":[],"readme":"README.md","repository":"https://github.com/mpv-easy/mpv-easy","homepage":null,"documentation":null,"edition":"2021","links":null,"default_run":null,"rust_version":null}],"workspace_members":["path+file:///C:/wt/clippy-path/mpv-easy-ext#0.1.0"],"workspace_default_members":["path+file:///C:/wt/clippy-path/mpv-easy-ext#0.1.0"],"resolve":{"nodes":[{"id":"path+file:///C:/wt/clippy-path/mpv-easy-ext#0.1.0","dependencies":[],"deps":[],"features":[]}],"root":null},"target_directory":"C:\\wt\\clippy-path\\target","version":1,"workspace_root":"C:\\wt\\clippy-path","metadata":null}
  • Does this reproduce on stable?

Yes

  • Does this reproduce with cargo check?

Yes

  • Does cargo metadata show the non-cleaned path?

Yes "src_path":"C:\wt\clippy-path\mpv-easy-ext\./src/main.rs"

epage commented 1 month ago

@ahaoboy Can you find a full reproduction case (manfiest, source files, etc). This can be by copy/pasting or a repo. A repo would make sure nothing was accidentally left out.

I'm assuming the problem is in cases like https://github.com/rust-lang/cargo/blob/4e19f315c965cedbfe6000f60eadd74f4ed914d3/src/cargo/util/toml/targets.rs#L318

I would assume we would have a call to https://docs.rs/cargo-util/latest/cargo_util/paths/fn.normalize_path.html in there. Might be good to go back some versions to see if this once worked and to identify the root cause to find where the best fix would be.

ahaoboy commented 1 month ago

Steps

git clone https://github.com/ahaoboy/cargo-clippy-path

cd cargo-clippy-path

cargo clippy

Ubuntu has same issue

 --> mpv-easy-ext/./src/main.rs:2:7
  |

The solution should not be difficult. Just find where the path mpv-easy-ext/./src/main.rs:2:7 is joined, and then use clean-path to process the following. cargo is too complicated, and I don’t know where the path is concatenated.

PS: Maybe we can use a clean-path-join library? The double separator does not affect the function at present, but it is impossible to jump through the link in some editors and terminals.