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

rustc is missing some --externs #12059

Closed
fenollp opened this issue Apr 29, 2023 · 4 comments
Closed

rustc is missing some --externs #12059

fenollp opened this issue Apr 29, 2023 · 4 comments
Labels
C-bug Category: bug S-triage Status: This issue is waiting on initial triage.

Comments

@fenollp
Copy link

fenollp commented Apr 29, 2023

Problem

I notice that transitive dependencies are not listed among the --extern ..=.. arguments to rustc.

Maintaining the list myself is acceptable but turn out to be a longer list of dependencies than actually needed.

Steps

  1. setup a RUSTC_WRAPPER such as:
printf '#!/bin/bash -eu\necho "$*" 1>&2\n' >wrap
chmod +x wrap
RUSTC_WRAPPER=wrap cargo install --locked --git https://github.com/fenollp/buildxargs
  1. pick one of the appearing rustc calls and run it as a docker build. Say with clap v4.2.1 (using the full tree for clap here):
# _rustc --crate-name clap --edition=2021 /home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=211 --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg feature=color --cfg feature=default --cfg feature=derive --cfg feature=error-context --cfg feature=help --cfg feature=std --cfg feature=suggestions --cfg feature=usage -C metadata=8996e440435cdc93 -C extra-filename=-8996e440435cdc93 --out-dir /home/pete/wefwefwef/buildxargs.git/_target/debug/deps -L dependency=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps --extern clap_builder=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_builder-02591a0046469edd.rmeta --extern clap_derive=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_derive-a4ff03e749cd3808.so --extern once_cell=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libonce_cell-da1c67e98ff0d3df.rmeta --cap-lints allow
docker --debug buildx bake --file=- <<EOBAKE
target "out" {
  context = "/home/pete/wefwefwef/buildxargs.git"
  contexts = {
    "deps" = "/home/pete/wefwefwef/buildxargs.git/_target/debug/deps",
    "input_src_lib_rs--clap-4.2.1" = "/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1",
    "rust" = "docker-image://docker.io/library/rust:1.68.2-slim@sha256:df4d8577fab8b65fabe9e7f792d6f4c57b637dd1c595f3f0a9398a9854e17094",
  }
  dockerfile-inline = <<DOCKERFILE
# syntax=docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14

FROM rust AS src_lib_rs-lib-clap-8996e440435cdc93
WORKDIR /home/pete/wefwefwef/buildxargs.git/_target/debug/deps
WORKDIR /home/pete/wefwefwef/buildxargs.git
RUN \
  --mount=type=bind,from=input_src_lib_rs--clap-4.2.1,target=/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1 \
  --mount=type=bind,from=deps,source=/libanstream-47e0535dab3ef0d2.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libanstream-47e0535dab3ef0d2.rmeta \
  --mount=type=bind,from=deps,source=/libanstyle-3d9b242388653423.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libanstyle-3d9b242388653423.rmeta \
  --mount=type=bind,from=deps,source=/libanstyle_parse-0d4af9095c79189b.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libanstyle_parse-0d4af9095c79189b.rmeta \
  --mount=type=bind,from=deps,source=/libbitflags-f255a966af175049.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libbitflags-f255a966af175049.rmeta \
  --mount=type=bind,from=deps,source=/libclap_builder-02591a0046469edd.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_builder-02591a0046469edd.rmeta \
  --mount=type=bind,from=deps,source=/libclap_derive-a4ff03e749cd3808.so,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_derive-a4ff03e749cd3808.so \
  --mount=type=bind,from=deps,source=/libclap_lex-7dfc2f58447e727e.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_lex-7dfc2f58447e727e.rmeta \
  --mount=type=bind,from=deps,source=/libconcolor_override-305fddcda33650f6.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libconcolor_override-305fddcda33650f6.rmeta \
  --mount=type=bind,from=deps,source=/libconcolor_query-74e38d373bc944a9.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libconcolor_query-74e38d373bc944a9.rmeta \
  --mount=type=bind,from=deps,source=/libheck-cd1cdbedec0a6dc0.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libheck-cd1cdbedec0a6dc0.rmeta \
  --mount=type=bind,from=deps,source=/libio_lifetimes-36f41602071771e6.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libio_lifetimes-36f41602071771e6.rmeta \
  --mount=type=bind,from=deps,source=/libis_terminal-4b94fef286899229.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libis_terminal-4b94fef286899229.rmeta \
  --mount=type=bind,from=deps,source=/liblibc-9de7ca31dbbda4df.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/liblibc-9de7ca31dbbda4df.rmeta \
  --mount=type=bind,from=deps,source=/liblinux_raw_sys-67b8335e06167307.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/liblinux_raw_sys-67b8335e06167307.rmeta \
  --mount=type=bind,from=deps,source=/libonce_cell-da1c67e98ff0d3df.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libonce_cell-da1c67e98ff0d3df.rmeta \
  --mount=type=bind,from=deps,source=/libproc_macro2-ef119f7eb3ef5720.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libproc_macro2-ef119f7eb3ef5720.rmeta \
  --mount=type=bind,from=deps,source=/libquote-74434efe692a445d.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libquote-74434efe692a445d.rmeta \
  --mount=type=bind,from=deps,source=/librustix-120609be99d53c6b.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/librustix-120609be99d53c6b.rmeta \
  --mount=type=bind,from=deps,source=/libstrsim-8ed1051e7e58e636.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libstrsim-8ed1051e7e58e636.rmeta \
  --mount=type=bind,from=deps,source=/libsyn-4befa7538c9a9f80.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libsyn-4befa7538c9a9f80.rmeta \
  --mount=type=bind,from=deps,source=/libunicode_ident-417636671c982ef8.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libunicode_ident-417636671c982ef8.rmeta \
  --mount=type=bind,from=deps,source=/libutf8parse-951ca9bdc6d60a50.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libutf8parse-951ca9bdc6d60a50.rmeta \
    ["rustc", "--crate-name", "clap", "--edition", "2021", "--error-format", "json", "--json", "artifacts,future-incompat", "--diagnostic-width", "211", "--crate-type", "lib", "--emit", "dep-info,metadata,link", "-C", "embed-bitcode=no", "-C", "debuginfo=2", "--cfg", "feature=\"color\"", "--cfg", "feature=\"default\"", "--cfg", "feature=\"derive\"", "--cfg", "feature=\"error-context\"", "--cfg", "feature=\"help\"", "--cfg", "feature=\"std\"", "--cfg", "feature=\"suggestions\"", "--cfg", "feature=\"usage\"", "-C", "metadata=8996e440435cdc93", "-C", "extra-filename=-8996e440435cdc93", "--out-dir", "/home/pete/wefwefwef/buildxargs.git/_target/debug/deps", "-L", "dependency=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps", "--extern", "clap_builder=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_builder-02591a0046469edd.rmeta", "--extern", "clap_derive=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_derive-a4ff03e749cd3808.so", "--extern", "once_cell=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libonce_cell-da1c67e98ff0d3df.rmeta", "--cap-lints", "allow", "/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1/src/lib.rs"]
FROM scratch AS out
COPY --from=src_lib_rs-lib-clap-8996e440435cdc93 /home/pete/wefwefwef/buildxargs.git/_target/debug/deps/*-8996e440435cdc93.* /
DOCKERFILE
  network = "none"
  output = ["/home/pete/wefwefwef/buildxargs.git/_target/debug/deps"] # https://github.com/moby/buildkit/issues/1224
  platforms = ["local"]
  target = "out"
}
group "default" { targets = ["out"] }
EOBAKE
# =>
[+] Building 0.1s (15/15) FINISHED                                                                                                                                                                                 
 => [internal] load build definition from Dockerfile                                                                                                                                                          0.0s
 => => transferring dockerfile: 5.58kB                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                             0.0s
 => => transferring context: 2B                                                                                                                                                                               0.0s
 => resolve image config for docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14                                                                            0.0s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14                                                                               0.0s
 => [context deps] load .dockerignore                                                                                                                                                                         0.0s
 => => transferring deps: 2B                                                                                                                                                                                  0.0s
 => [context rust] load metadata for docker.io/library/rust:1.68.2-slim@sha256:df4d8577fab8b65fabe9e7f792d6f4c57b637dd1c595f3f0a9398a9854e17094                                                               0.0s
 => [context input_src_lib_rs--clap-4.2.1] load .dockerignore                                                                                                                                                 0.0s
 => => transferring input_src_lib_rs--clap-4.2.1: 2B                                                                                                                                                          0.0s
 => [context rust] docker.io/library/rust:1.68.2-slim@sha256:df4d8577fab8b65fabe9e7f792d6f4c57b637dd1c595f3f0a9398a9854e17094                                                                                 0.0s
 => [context input_src_lib_rs--clap-4.2.1] load from client                                                                                                                                                   0.0s
 => => transferring input_src_lib_rs--clap-4.2.1: 7.39kB                                                                                                                                                      0.0s
 => [context deps] load from client                                                                                                                                                                           0.0s
 => => transferring deps: 4.74kB                                                                                                                                                                              0.0s
 => CACHED [src_lib_rs-lib-clap-8996e440435cdc93 1/4] WORKDIR /home/pete/wefwefwef/buildxargs.git/_target/debug/deps                                                                                          0.0s
 => CACHED [src_lib_rs-lib-clap-8996e440435cdc93 2/4] WORKDIR /home/pete/wefwefwef/buildxargs.git                                                                                                             0.0s
 => CACHED [src_lib_rs-lib-clap-8996e440435cdc93 3/4] RUN   --mount=type=bind,from=input_src_lib_rs--clap-4.2.1,target=/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1   --mount=type=  0.0s
 => CACHED [out 1/1] COPY --from=src_lib_rs-lib-clap-8996e440435cdc93 /home/pete/wefwefwef/buildxargs.git/_target/debug/deps/*-8996e440435cdc93.* /                                                           0.0s
 => exporting to client                                                                                                                                                                                       0.0s
 => => copying files 23.94kB                                                                                                                                                                                  0.0s
  1. note how there's fewer --extern ..=.. than there are --mounts. With only the --extern as --mount the build doesn't pass:
docker --debug buildx bake --file=- <<EOBAKE
target "out" {
  context = "/home/pete/wefwefwef/buildxargs.git"
  contexts = {
    "deps" = "/home/pete/wefwefwef/buildxargs.git/_target/debug/deps",
    "input_src_lib_rs--clap-4.2.1" = "/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1",
    "rust" = "docker-image://docker.io/library/rust:1.68.2-slim@sha256:df4d8577fab8b65fabe9e7f792d6f4c57b637dd1c595f3f0a9398a9854e17094",
  }
  dockerfile-inline = <<DOCKERFILE
# syntax=docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14

FROM rust AS src_lib_rs-lib-clap-8996e440435cdc93
WORKDIR /home/pete/wefwefwef/buildxargs.git/_target/debug/deps
WORKDIR /home/pete/wefwefwef/buildxargs.git
RUN \
  --mount=type=bind,from=input_src_lib_rs--clap-4.2.1,target=/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1 \
  --mount=type=bind,from=deps,source=/libclap_builder-02591a0046469edd.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_builder-02591a0046469edd.rmeta \
  --mount=type=bind,from=deps,source=/libclap_derive-a4ff03e749cd3808.so,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_derive-a4ff03e749cd3808.so \
  --mount=type=bind,from=deps,source=/libonce_cell-da1c67e98ff0d3df.rmeta,target=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libonce_cell-da1c67e98ff0d3df.rmeta \
    ["rustc", "--crate-name", "clap", "--edition", "2021", "--error-format", "json", "--json", "artifacts,future-incompat", "--diagnostic-width", "211", "--crate-type", "lib", "--emit", "dep-info,metadata,link", "-C", "embed-bitcode=no", "-C", "debuginfo=2", "--cfg", "feature=\"color\"", "--cfg", "feature=\"default\"", "--cfg", "feature=\"derive\"", "--cfg", "feature=\"error-context\"", "--cfg", "feature=\"help\"", "--cfg", "feature=\"std\"", "--cfg", "feature=\"suggestions\"", "--cfg", "feature=\"usage\"", "-C", "metadata=8996e440435cdc93", "-C", "extra-filename=-8996e440435cdc93", "--out-dir", "/home/pete/wefwefwef/buildxargs.git/_target/debug/deps", "-L", "dependency=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps", "--extern", "clap_builder=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_builder-02591a0046469edd.rmeta", "--extern", "clap_derive=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libclap_derive-a4ff03e749cd3808.so", "--extern", "once_cell=/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/libonce_cell-da1c67e98ff0d3df.rmeta", "--cap-lints", "allow", "/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1/src/lib.rs"]
FROM scratch AS out
COPY --from=src_lib_rs-lib-clap-8996e440435cdc93 /home/pete/wefwefwef/buildxargs.git/_target/debug/deps/*-8996e440435cdc93.* /
DOCKERFILE
  network = "none"
  output = ["/home/pete/wefwefwef/buildxargs.git/_target/debug/deps"] # https://github.com/moby/buildkit/issues/1224
  platforms = ["local"]
  target = "out"
}
group "default" { targets = ["out"] }
EOBAKE
# =>
#0 0.282 {"message":"can't find crate for `bitflags` which `clap_builder` depends on","code":{"code":"E0463","explanation":"A plugin/crate was declared but cannot be found.\n\nErroneous code example:\n\n```compile_fail,E0463\n#![feature(plugin)]\n#![plugin(cookie_monster)] // error: can't find crate for `cookie_monster`\nextern crate cake_is_a_lie; // error: can't find crate for `cake_is_a_lie`\n```\n\nYou need to link your code to the relevant crate in order to be able to use it\n(through Cargo or the `-L` option of rustc example). Plugins are crates as\nwell, and you link to them the same way.\n\n## Common causes\n\n- The crate is not present at all. If using Cargo, add it to `[dependencies]`\n  in Cargo.toml.\n- The crate is present, but under a different name. If using Cargo, look for\n  `package = ` under `[dependencies]` in Cargo.toml.\n\n## Common causes for missing `std` or `core`\n\n- You are cross-compiling for a target which doesn't have `std` prepackaged.\n  Consider one of the following:\n  + Adding a pre-compiled version of std with `rustup target add`\n  + Building std from source with `cargo build -Z build-std`\n  + Using `#![no_std]` at the crate root, so you won't need `std` in the first\n    place.\n- You are developing the compiler itself and haven't built libstd from source.\n  You can usually build it with `x.py build library/std`. More information\n  about x.py is available in the [rustc-dev-guide].\n\n[rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html#building-the-compiler\n"},"level":"error","spans":[{"file_name":"/home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1/src/lib.rs","byte_start":4206,"byte_end":4218,"line_start":98,"line_end":98,"column_start":9,"column_end":21,"is_primary":true,"text":[{"text":"pub use clap_builder::*;","highlight_start":9,"highlight_end":21}],"label":"can't find crate","suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[],"rendered":"error[E0463]: can't find crate for `bitflags` which `clap_builder` depends on\n  --> /home/pete/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-4.2.1/src/lib.rs:98:9\n   |\n98 | pub use clap_builder::*;\n   |         ^^^^^^^^^^^^ can't find crate\n\n"}
#0 0.282 {"artifact":"/home/pete/wefwefwef/buildxargs.git/_target/debug/deps/clap-8996e440435cdc93.d","emit":"dep-info"}
#0 0.283 {"message":"aborting due to previous error","code":null,"level":"error","spans":[],"children":[],"rendered":"error: aborting due to previous error\n\n"}
#0 0.283 {"message":"For more information about this error, try `rustc --explain E0463`.","code":null,"level":"failure-note","spans":[],"children":[],"rendered":"For more information about this error, try `rustc --explain E0463`.\n"}

Possible Solution(s)

Possible reason: since edition 2018 all externs needn't be mentioned. See: rust-lang/rust#68417 (comment)

A fix I use today is to simply maintain a list of a crate's deps and their deps and so on. This works but it does seem to bring in more deps than are actually required. Minimizing this list means cheaper builds for my wrapper project.

Notes

My script to generate mounts from externs: https://github.com/fenollp/buildxargs/blob/6aff80c1d16b93ea448104fea57e79fe4e6b0b09/tryin.sh

Context: I am writing a RUSTC_WRAPPER that uses docker buildx. I aim to minimize the docker context required to build each crate dependency. Meaning I only mount the paths to --extern deps. However I notice that cargo does not mention every crate required to build a crate: it mentions only the direct dependencies.

Is there a flag I can add to rustc or cargo to know what these missing --extern should be?

Also: by manually adding the path to dependencies one by one (selecting these mentioned in the error messages) I notice that a crate's full dependency tree could actually be pruned a bit. See here for clap v4.2.1 (lib-clap-8996e440435cdc93):

# Short tree: (only iteratively found deps)
anstream-47e0535dab3ef0d2
anstyle-3d9b242388653423
anstyle_parse-0d4af9095c79189b
bitflags-f255a966af175049
clap_lex-7dfc2f58447e727e
concolor_override-305fddcda33650f6
concolor_query-74e38d373bc944a9
io_lifetimes-36f41602071771e6
is_terminal-4b94fef286899229
libc-9de7ca31dbbda4df
linux_raw_sys-67b8335e06167307
rustix-120609be99d53c6b
strsim-8ed1051e7e58e636
utf8parse-951ca9bdc6d60a50
# Full tree
anstream-47e0535dab3ef0d2
anstyle-3d9b242388653423
anstyle_parse-0d4af9095c79189b
bitflags-f255a966af175049
clap_builder-02591a0046469edd
clap_derive-a4ff03e749cd3808
clap_lex-7dfc2f58447e727e
concolor_override-305fddcda33650f6
concolor_query-74e38d373bc944a9
heck-cd1cdbedec0a6dc0
io_lifetimes-36f41602071771e6
is_terminal-4b94fef286899229
libc-9de7ca31dbbda4df
linux_raw_sys-67b8335e06167307
once_cell-da1c67e98ff0d3df
proc_macro2-ef119f7eb3ef5720
quote-74434efe692a445d
rustix-120609be99d53c6b
strsim-8ed1051e7e58e636
syn-4befa7538c9a9f80
unicode_ident-417636671c982ef8
utf8parse-951ca9bdc6d60a50

Maybe a shorter/simpler example:

# Call to rustc to build quote says its only dep is proc_macro2,
# however this dep requires a transitive dep called unicode_ident.
# This call builds IFF that dep is added as an --extern.
# NOTE: rustc 1.68.2 (9eb3afe9e 2023-03-27) docker.io/library/rust:1.68.2-slim@sha256:df4d8577fab8b65fabe9e7f792d6f4c57b637dd1c595f3f0a9398a9854e17094
rustc --crate-name quote \
      --edition=2018 \
      "$CARGO_HOME"/registry/src/github.com-1ecc6299db9ec823/quote-1.0.26/src/lib.rs \
      --error-format=json \
      --json=diagnostic-rendered-ansi,artifacts,future-incompat \
      --diagnostic-width=211 \
      --crate-type lib \
      --emit=dep-info,metadata,link \
      -C embed-bitcode=no \
      -C debuginfo=2 \
      --cfg feature="default" \
      --cfg feature="proc-macro" \
      -C metadata=74434efe692a445d \
      -C extra-filename=-74434efe692a445d \
      --out-dir "$CARGO_TARGET_DIR/$PROFILE"/deps \
      -C linker=/usr/bin/clang \
      -L dependency="$CARGO_TARGET_DIR/$PROFILE"/deps \
      --extern proc_macro2="$CARGO_TARGET_DIR/$PROFILE"/deps/libproc_macro2-ef119f7eb3ef5720.rmeta \
      --cap-lints allow \
      -C link-arg=-fuse-ld=/usr/local/bin/mold

Version

cargo 1.68.2 (6feb7c9cf 2023-03-26)
release: 1.68.2
commit-hash: 6feb7c9cfc0c5604732dba75e4c3b2dbea38e8d8
commit-date: 2023-03-26
host: x86_64-unknown-linux-gnu
libgit2: 1.5.0 (sys:0.16.0 vendored)
libcurl: 7.86.0-DEV (sys:0.4.59+curl-7.86.0 vendored ssl:OpenSSL/1.1.1q)
os: Linux [64-bit]
@fenollp fenollp added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Apr 29, 2023
@bjorn3
Copy link
Member

bjorn3 commented May 5, 2023

It is intentional that transitive dependencies are not listed in --extern. --extern adds crates to the implicit prelude of the current crate. Not adding --extern for transitive dependencies prevents accidentally depending on crates without declaring the dependency in Cargo.toml. Rustc will look for transitive dependencies in the directory pointed to by -L.

@fenollp
Copy link
Author

fenollp commented May 7, 2023

Hi @bjorn3 thanks for take the time.

Not adding --extern for transitive dependencies prevents accidentally depending on crates without declaring the dependency in Cargo.toml.

I'm not sure I see what accidentally depending can mean here? cargo has all the info. --extern is for direct deps, alright, then how about adding a --transitive-extern <crate-name>=<file> ?

I understand rustc uses -L <dir>. I am asking for a more precise way for cargo to tell to rustc which files that call depends on. RUSTC_WRAPPER and other tools bringing hermetic builds should greatly benefit from this being explicit. Don't you think?

@bjorn3
Copy link
Member

bjorn3 commented May 7, 2023

I'm not sure I see what accidentally depending can mean here?

Say my_bin depends on liba which depends on libb, then if cargo passes --extern libb=... when compiling my_bin, it is possible for my_bin to accidentally depend on libb without declaring such a dependency. If later liba has an update which removes the dependency on libb, the build of my_bin would break. In addition my_bin may depend on a different (non-semver compatible) version of libb than liba does, in which case you get two --extern libb=... with different paths, which conflict with each other.

then how about adding a --transitive-extern <crate-name>=<file> ?

Rustc has no use for this information at all. For transitive dependencies it already records a hash in the crate metadata to prevent the wrong version being used. It only needs help with finding the transitive dependencies, hence why -L is passed by cargo.

If -Zbinary-dep-depinfo stabilizes, the .d depinfo files will contain a list of all libraries a crate depends on rather than just all source files like it does currently. You could then parse these files recursively to get from all explicit dependencies with --extern to all transitive dependencies.

Another route you can take is to parse the output of cargo metadata and construct all dependencies and commandline invocations from this. This is more work, but it is the route Bazel, Buck and similar build systems take.

@weihanglo
Copy link
Member

Since design choice is summarized pretty well, I am going to close this as “won't fix”.

cargo metadata could be a solution for this as aforementioned as well. Thank you for your report anyway.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2023
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.
Projects
None yet
Development

No branches or pull requests

3 participants