rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.96k stars 12.68k forks source link

Can't find `thiserror_impl` crate with `-Z sanitizer` #132109

Open ColonelThirtyTwo opened 2 hours ago

ColonelThirtyTwo commented 2 hours ago

I tried this code:

// (empty lib.rs)

Cargo.toml:

[package]
name = "nightly-cant-find-crate-test"
version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "1.0.65"

Compiling with env RUSTFLAGS="-Z sanitizer=address" cargo +nightly build

I expected to see this happen: Library compiles

Instead, this happened: Rust fails to find the crate thiserror_impl, even though it's a transitive dependency:

> env RUSTFLAGS="-Z sanitizer=address" cargo +nightly build
   Compiling proc-macro2 v1.0.89
   Compiling unicode-ident v1.0.13
   Compiling thiserror v1.0.65
   Compiling quote v1.0.37
   Compiling syn v2.0.85
   Compiling thiserror-impl v1.0.65
error[E0463]: can't find crate for `thiserror_impl`
   --> /home/col/.cargo/registry/src/index.crates.io-6f17d22bba15001f/thiserror-1.0.65/src/lib.rs:278:9
    |
278 | pub use thiserror_impl::*;
    |         ^^^^^^^^^^^^^^ can't find crate

For more information about this error, try `rustc --explain E0463`.

The crate builds without errors if I omit the sanitizer option, or replace it with something else like -Zub-checks. It also obviously builds with stable Rust.

Meta

rustc --version --verbose:

> rustup run nightly rustc --version
rustc 1.84.0-nightly (4f2f477fd 2024-10-23)
> cargo --version
cargo 1.82.0 (8f40fc59f 2024-08-21)
taiki-e commented 2 hours ago

You have to pass --target flag.

See also documentation of -Z sanitizer:

Build scripts and procedural macros

Use of sanitizers together with build scripts and procedural macros is technically possible, but in almost all cases it would be best avoided. This is especially true for procedural macros which would require an instrumented version of rustc.

In more practical terms when using cargo always remember to pass --target flag, so that rustflags will not be applied to build scripts and procedural macros.

ColonelThirtyTwo commented 2 hours ago

Setting the target worked, thank you. A warning message would be helpful though.