Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when combining -Zbuild-std with artifact dependencies #10444

Open
phil-opp opened this issue Mar 1, 2022 · 10 comments
Open

Panic when combining -Zbuild-std with artifact dependencies #10444

phil-opp opened this issue Mar 1, 2022 · 10 comments
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage. Z-bindeps Nightly: binary artifact dependencies Z-build-std Nightly: build-std

Comments

@phil-opp
Copy link
Contributor

phil-opp commented Mar 1, 2022

Problem

The following panic happens when trying to check a project with an artifact dependency that uses build-std:

thread 'main' panicked at 'no entry found for key', src/tools/cargo/src/cargo/core/compiler/unit_dependencies.rs:169:25
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4ce3749235fc31d15ebd444b038a9877e8c700d7/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/4ce3749235fc31d15ebd444b038a9877e8c700d7/library/core/src/panicking.rs:143:14
   2: core::panicking::panic_display
             at /rustc/4ce3749235fc31d15ebd444b038a9877e8c700d7/library/core/src/panicking.rs:72:5
   3: core::panicking::panic_str
             at /rustc/4ce3749235fc31d15ebd444b038a9877e8c700d7/library/core/src/panicking.rs:56:5
   4: core::option::expect_failed
             at /rustc/4ce3749235fc31d15ebd444b038a9877e8c700d7/library/core/src/option.rs:1852:5
   5: cargo::core::compiler::unit_dependencies::build_unit_dependencies
   6: cargo::ops::cargo_compile::create_bcx
   7: cargo::ops::cargo_compile::compile_ws
   8: cargo::ops::cargo_compile::compile
   9: cargo::commands::check::exec
  10: cargo::cli::main
  11: cargo::main

Steps

  1. cargo new --lib artifact-dep-test
  2. cd artifact-dep-test
  3. cargo new --bin sub
  4. In the top-level Cargo.toml, add the following dependency:
    sub = { path = "sub", artifact = ["bin"], target = "x86_64-unknown-none" } 
  5. Run:
    cargo +nightly check -Zbindeps -Zbuild-std --target x86_64-unknown-linux-gnu
    Instead of x86_64-unknown-linux-gnu, use your host architecture. (Specifying it explicitly is currently required for build-std.)

Possible Solution(s)

No response

Notes

The issue also happens on the latest master (commit 5f611af). The backtrace in debug mode is:

thread 'main' panicked at 'no entry found for key', src/cargo/core/compiler/unit_dependencies.rs:169:25
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:116:14
   2: core::panicking::panic_display
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:72:5
   3: core::panicking::panic_str
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/panicking.rs:56:5
   4: core::option::expect_failed
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/option.rs:1817:5
   5: core::option::Option<T>::expect
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/option.rs:692:21
   6: <std::collections::hash::map::HashMap<K,V,S> as core::ops::index::Index<&Q>>::index
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/std/src/collections/hash/map.rs:1164:9
   7: cargo::core::compiler::unit_dependencies::attach_std_deps
             at ./src/cargo/core/compiler/unit_dependencies.rs:169:25
   8: cargo::core::compiler::unit_dependencies::build_unit_dependencies
             at ./src/cargo/core/compiler/unit_dependencies.rs:125:9
   9: cargo::ops::cargo_compile::create_bcx
             at ./src/cargo/ops/cargo_compile.rs:578:26
  10: cargo::ops::cargo_compile::compile_ws
             at ./src/cargo/ops/cargo_compile.rs:285:15
  11: cargo::ops::cargo_compile::compile_with_exec
             at ./src/cargo/ops/cargo_compile.rs:276:5
  12: cargo::ops::cargo_compile::compile
             at ./src/cargo/ops/cargo_compile.rs:265:5
  13: cargo::commands::check::exec
             at ./src/bin/cargo/commands/check.rs:51:5
  14: cargo::cli::execute_subcommand
             at ./src/bin/cargo/cli.rs:369:16
  15: cargo::cli::main
             at ./src/bin/cargo/cli.rs:176:5
  16: cargo::main
             at ./src/bin/cargo/main.rs:39:13
  17: core::ops::function::FnOnce::call_once
             at /rustc/9d1b2106e23b1abd32fce1f17267604a5102f57a/library/core/src/ops/function.rs:227:5

Version

cargo 1.59.0 (49d8809dc 2022-02-10)
release: 1.59.0
commit-hash: 49d8809dc2d3e6e0d5ec634fcf26d8e2aab67130
commit-date: 2022-02-10
host: x86_64-unknown-linux-gnu
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1l)
os: Pop!_OS 21.10 (impish) [64-bit]
@phil-opp phil-opp added the C-bug Category: bug label Mar 1, 2022
@ehuss ehuss added Z-bindeps Nightly: binary artifact dependencies Z-build-std Nightly: build-std labels Mar 2, 2022
@ehuss
Copy link
Contributor

ehuss commented Mar 2, 2022

Thanks for the report! I forgot to open this issue.

Handling build-std will likely take a little bit of refactoring, but shouldn't be too hard.

An outline of the problem:

  • generate_std_roots generates the root units of std to compile. However, this is only based on the --target list of targets. Instead, it needs to build the set of std to build based on all targets that will be used. I'm not sure of the best way to collect that list of targets, but it shouldn't be too hard. One option is to walk the dependency graph. Another option is to shift the building of std roots into build_unit_dependencies and lazily calculate the roots as needed. I'm not sure which approach will be better.
  • attach_std_deps will need to be smarter about how it attaches std roots into the graph. It should only attach roots that match the target kind. For example, package foo built for x86_64-unknown-none should only have the x86_64-unknown-none std roots attached to it.

cc @Byron

@ehuss
Copy link
Contributor

ehuss commented Mar 2, 2022

Oh, I should also link to #9451 which has essentially the same problem. It would be good to fix it for both scenarios at the same time. That also just needs to collect the list of additional targets that need to be built.

@Byron
Copy link
Member

Byron commented Mar 3, 2022

Thanks a lot for the outline, this should help a lot when attempting a fix.

Collecting a list of targets reliably is something that is also required for dependency and feature resolution, which I think is related to #10405 as it should mean that all targets to build for would be known as part of the crate metadata from the index.

One option is to walk the dependency graph.

My feeling is that collecting all targets there (and maybe even once) should be enough instead of having to collect them multiple times. Now, there probably is a reason that isn't the case (yet), but I thought I'd mention it here in case it's a possibility at all to avoid multiple dependency walks and repetition.

@bstrie
Copy link
Contributor

bstrie commented Mar 22, 2022

I'm kind of unclear on how build-std should interact with bindeps. If you just wanted to build-std for the bindep, and wanted to use the shipped std for everything else, how would you express that? Alternatively if you wanted to build-std the top-level project but use the shipped std for the bindep, how would you express that?

@phil-opp
Copy link
Contributor Author

@bstrie Yeah, that is something that we still need to figure out. I proposed two possible solutions:

  1. Allow to set build-std and build-std-features per package in the Cargo.toml.
  2. Add target-specific config options for build-std/build-std-features via the [target] table in .cargo/config.toml files.

Do you have other ideas?

(Maybe it makes sense to discuss this somewhere else to keep this issue thread clean?)

@bstrie
Copy link
Contributor

bstrie commented Mar 22, 2022

Great, I just wanted to confirm that this is something that people are thinking about.

(Maybe it makes sense to discuss this somewhere else to keep this issue thread clean?)

Sure, is there a tracking issue for that?

WRT this issue, my main question is whether or not this is something that would need to be resolved before the stabilization of artifact dependencies, which I hope in my eternally boundless optimism might be thinkable in the near-ish term (or at least nearer than build-std seems to be).

@devsnek
Copy link

devsnek commented Jan 29, 2023

I have a project where I have a root package which has a [build-dependencies] bindep entry for another package (fwiw, only because cargo lacks post-build scripts ☹️). this other package is no_std and x86_64-unknown-none. If I try to use a package that needs std in the sub-package's build.rs/[build-dependencies], i get errors like:

error[E0405]: cannot find trait `Drop` in this scope
   --> /home/snek/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.14/src/sync/sharded_lock.rs:606:6
    |
606 | impl Drop for Registration {
    |      ^^^^ not found in this scope
    |
help: consider importing this trait
    |
1   | use core::ops::Drop;
    |

It seems like whatever resolved the dependencies for the sub package's build deps are being inferred to need -Zbuild-std because of the x86_64-unknown-none, even though it should just be using the host target?

fee1-dead added a commit to fee1-dead-contrib/cargo that referenced this issue Apr 13, 2023
This is a simplified minimal change, which changes the build-std to
iterate over the packages to collect a list of targets std should be
built for. Does the iterating over packages part have issues with
artifact dependencies? I am not very familiar with artifact deps to
know so any help would be appreciated. But it should be fine for now
since artifact dependencies don't work with build-std yet. rust-lang#10444
fee1-dead added a commit to fee1-dead-contrib/cargo that referenced this issue Apr 13, 2023
This is a simplified minimal change, which changes the build-std to
iterate over the packages to collect a list of targets std should be
built for. Does the iterating over packages part have issues with
artifact dependencies? I am not very familiar with artifact deps to
know so any help would be appreciated. But it should be fine for now
since artifact dependencies don't work with build-std yet. rust-lang#10444
fee1-dead added a commit to fee1-dead-contrib/cargo that referenced this issue Apr 13, 2023
This is a simplified minimal change, which changes the build-std to
iterate over the packages to collect a list of targets std should be
built for. Does the iterating over packages part have issues with
artifact dependencies? I am not very familiar with artifact deps to
know so any help would be appreciated. But it should be fine for now
since artifact dependencies don't work with build-std yet. rust-lang#10444
fee1-dead added a commit to fee1-dead-contrib/cargo that referenced this issue Apr 13, 2023
This is a simplified minimal change, which changes the build-std to
iterate over the packages to collect a list of targets std should be
built for. Does the iterating over packages part have issues with
artifact dependencies? I am not very familiar with artifact deps to
know so any help would be appreciated. But it should be fine for now
since artifact dependencies don't work with build-std yet. rust-lang#10444
JonasKruckenberg added a commit to JonasKruckenberg/cargo that referenced this issue Sep 1, 2024
fixes rust-lang#10444 by using the same `CompileKind`s that artifact dependencies produces
@JonasKruckenberg
Copy link

JonasKruckenberg commented Sep 1, 2024

Had some time this morning and attempted a fix here #14480 which makes this work at least for usage in dependencies and build-dependencies (target.<cfg>.dependencies works fine too, but target.<cfg>.build-dependencies seems to still crash with a different error this one #10593, work for another PR I guess 🤷🏻‍♂️)

@epage epage added the S-triage Status: This issue is waiting on initial triage. label Nov 20, 2024
@elchukc
Copy link
Contributor

elchukc commented Dec 2, 2024

Since #14183 it now bails with error: building std is not supported on the following targets: x86_64-unknown-none

And when target = ... is set to a platform that is supported, like x86_64-unknown-linux-gnu, it panics:

thread 'main' panicked at src/tools/cargo/src/cargo/core/compiler/unit_dependencies.rs:178:34:
no entry found for key

I believe the difference is simply that some targets have their target-spec metadata filled in and are thus caught by the new error catch, while the rest do not.

@weihanglo
Copy link
Member

Since #14183 it now bails with error: building std is not supported on the following targets: x86_64-unknown-none

@elchukc see https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/help.20debugging.20a.20docs.2Ers.20issue.20with.20a.20new.20cargo.20error/near/485698051 for a relevant discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage. Z-bindeps Nightly: binary artifact dependencies Z-build-std Nightly: build-std
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants