NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.99k stars 14.01k forks source link

Clippy not working for cross targets #278508

Open abbec opened 9 months ago

abbec commented 9 months ago

Describe the bug

Since #176694, clippy does not work because it uses the non-cross sysroot.

Steps To Reproduce

  1. use pkgsCross.mingwW64.buildPackages.cargo to build a crate
  2. run pkgsCross.mingwW64.buildPackages.clippy on the same crate

Expected behavior

Both 1) and 2) should find the rust std crate.

Additional context

Clippy fails with

error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-pc-windows-gnu` target may not be installed
  = help: consider downloading the target with `rustup target add x86_64-pc-windows-gnu`

error: cannot find macro `format` in this scope
 --> src/main.rs:6:5
  |
6 |     format!("{} Brrrrrrrrrr!", emoji)
  |     ^^^^^^

error: cannot find macro `println` in this scope
 --> src/main.rs:2:5
  |
2 |     println!("{}", get_output_message("🦼"));
  |     ^^^^^^^

error[E0412]: cannot find type `String` in this scope
 --> src/main.rs:5:39
  |
5 | fn get_output_message(emoji: &str) -> String {
  |                                       ^^^^^^ not found in this scope

error: requires `sized` lang_item

Some errors have detailed explanations: E0412, E0463.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `rusty-rust` (bin "rusty-rust") due to 5 previous errors

Caused by:
  process didn't exit successfully: `CARGO=/nix/store/4myq3g6xjg7x51hbxdbllyn273ggby8q-cargo-1.73.0/bin/.cargo-wrapped CARGO_BIN_NAME=rusty-rust CARGO_CRATE_NAME=rusty_rust CARGO_MANIFEST_DIR=/home/abbe/code/gbk/nedryglot/examples/hello/clients/rusty-rust CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=rusty-rust CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/abbe/code/gbk/nedryglot/examples/hello/clients/rusty-rust/target/debug/deps:/nix/store/hywr2z0bbf7vrhii1bwp09xcinmzy3n4-rustc-1.73.0/lib' /nix/store/jl8mpd22pkjdag1cca4mr4wjfxhd9s5y-clippy-1.73.0/bin/clippy-driver rustc --crate-name rusty_rust --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=214 --crate-type bin --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C metadata=f848e6f1f731cec0 -C extra-filename=-f848e6f1f731cec0 --out-dir /home/abbe/code/gbk/nedryglot/examples/hello/clients/rusty-rust/target/x86_64-pc-windows-gnu/debug/deps --target x86_64-pc-windows-gnu -C linker=x86_64-w64-mingw32-cc -C incremental=/home/abbe/code/gbk/nedryglot/examples/hello/clients/rusty-rust/target/x86_64-pc-windows-gnu/debug/incremental -L dependency=/home/abbe/code/gbk/nedryglot/examples/hello/clients/rusty-rust/target/x86_64-pc-windows-gnu/debug/deps -L dependency=/home/abbe/code/gbk/nedryglot/examples/hello/clients/rusty-rust/target/debug/deps -D warnings` (exit status: 1)

Note the path used in LD_LIBRARY_PATH which is also in the output from clippy-driver --print=sysroot:

$ clippy-driver --print=sysroot
/nix/store/hywr2z0bbf7vrhii1bwp09xcinmzy3n4-rustc-1.73.0

This should be

$ clippy-driver --print=sysroot
/nix/store/91vd42yp6pg9bba7c7mra0zg1c5b5h0s-x86_64-w64-mingw32-rustc-1.73.0

Notify maintainers

@mehmooda @alyssais @SuperSandro2000 @Mindavi @yu-re-ka @basvandijk

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.8-200.fc39.x86_64, Fedora Linux, 39 (Workstation Edition), nobuild`
 - multi-user?: `yes`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.13.2`
 - channels(abbe): `"home-manager-23.11.tar.gz, nixgl, toolbelt"`
 - channels(root): `"nixpkgs-23.11"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixpkgs`

Add a :+1: reaction to issues you find important.

alyssais commented 9 months ago

Do you have a Nix expression that can reproduce this?

abbec commented 9 months ago

Sure,

$ nix-shell -p pkgsCross.mingwW64.buildPackages.cargo pkgsCross.mingwW64.buildPackages.clippy --run "cargo init && cargo clippy --target x86_64-pc-windows-gnu"
     Created binary (application) package
    Checking rust-repro v0.1.0 (/var/tmp/rust-repro)
error[E0463]: can't find crate for `std`
  |
  = note: the `x86_64-pc-windows-gnu` target may not be installed
  = help: consider downloading the target with `rustup target add x86_64-pc-windows-gnu`

error: cannot find macro `println` in this scope
 --> src/main.rs:2:5
  |
2 |     println!("Hello, world!");
  |     ^^^^^^^

error: requires `sized` lang_item

For more information about this error, try `rustc --explain E0463`.
error: could not compile `rust-repro` (bin "rust-repro") due to 3 previous errors
yu-re-ka commented 9 months ago

Note the path used in LD_LIBRARY_PATH which is also in the output from clippy-driver --print=sysroot:

do not be fooled by the name. With the cross compilation optimization rustc the $target- prefix can not be relied upon. You'd have to actually check the contents of that store path to be sure it does not contain the cross std library.

abbec commented 9 months ago

Note the path used in LD_LIBRARY_PATH which is also in the output from clippy-driver --print=sysroot:

do not be fooled by the name. With the cross compilation optimization rustc the $target- prefix can not be relied upon. You'd have to actually check the contents of that store path to be sure it does not contain the cross std library.

It does not though.

yu-re-ka commented 9 months ago

Fair. so we need a wrapper for clippy-driver that inserts the correct sysroot

abbec commented 9 months ago

Fair. so we need a wrapper for clippy-driver that inserts the correct sysroot

Did a hacky workaround that did just that, which seems to do the trick.