Description
Per japaric/xargo#186 (comment), the following no longer works:
$ cargo new --lib foo
$ cd foo
$ echo '#![no_std]' > src/lib.rs
$ cargo add heapless
$ rustc -Z unstable-options --target x86_64-unknown-linux-gnu --print target-spec-json > foo.json
$ cargo build --target foo
Compiling untagged-option v0.1.1
error: Error loading target specification: Could not find specification for target "foo"
The following conversation was taken from #4865:
Shouldn't cargo be setting cwd to the workspace root (here just the crate root, foo) which is exactly where the target file foo.json is? If so, shouldn't rustc find the file and build properly? Or has something changed in rustc so that it no longer looks for target files in the cwd?
oh I believe the issue there is that you're compiling crates from crates.io and Cargo is now switching the cwd even for those crates (as their own workspace root is themselves in Cargo's $HOME/.cargo cache
Ah yeah so to clarify I believe everything is working correctly in cargo/rustc and this is an intentional change on Cargo's behalf. (breaking in the technical/pedantic sense)
Workaround
In order to fix the cargo build error, you need to manually set RUST_TARGET_PATH
.
export RUST_TARGET_PATH=`pwd`
Remaining Discussion
I think the following question remains:
Why are users of custom target specifications required to set RUST_TARGET_PATH
every time cargo build
is invoked?
The error message does not make it clear why the build is failing. Also, it is unintuitive to set an environment variable pointing to the current directory for the build to succeed.
If the current behavior will remain in the long-term, there should probably be documentation somewhere that addresses the breaking change. Also, I think the Cargo error message should hint that RUST_TARGET_PATH
is pointing to the wrong directory.