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

cargo builds proc-macros twice based on the profile #13321

Open
kchibisov opened this issue Jan 18, 2024 · 4 comments
Open

cargo builds proc-macros twice based on the profile #13321

kchibisov opened this issue Jan 18, 2024 · 4 comments
Labels
A-proc-macro Area: compiling proc-macros A-profiles Area: profiles A-workspaces Area: workspaces C-bug Category: bug S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

Comments

@kchibisov
Copy link

kchibisov commented Jan 18, 2024

Problem

I've recently noticed a difference when building alacritty in debug and release build that I have 216 deps in debug, but 217 in release. I've started investigating and found out that some proc macros are being built twice, but with slight difference (not in features though).

In the log I found the following for --release build (--debug has just one of these lines).

     Running `CARGO=/opt/rust-bin-1.74.1/bin/cargo-bin-1.74.1 CARGO_CRATE_NAME=alacritty_config_derive CARGO_MANIFEST_DIR=/home/kchibisov/src/rust/alacritty-workspace/fork/alacritty_config_derive CARGO_PKG_AUTHORS='Christian Duerr <[email protected]>' CARGO_PKG_DESCRIPTION='Failure resistant deserialization derive' CARGO_PKG_HOMEPAGE='https://github.com/alacritty/alacritty' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=alacritty_config_derive CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION=1.70.0 CARGO_PKG_VERSION=0.2.3-dev CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=3 CARGO_PKG_VERSION_PRE=dev CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps:/opt/rust-bin-1.74.1/lib:/home/kchibisov/.rustup/toolchains/gentoo/lib' /home/kchibisov/.rustup/toolchains/gentoo/bin/rustc --crate-name alacritty_config_derive --edition=2021 alacritty_config_derive/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type proc-macro --emit=dep-info,link -C prefer-dynamic -C embed-bitcode=no -C debug-assertions=off -C metadata=f11f6d33ed698a19 -C extra-filename=-f11f6d33ed698a19 --out-dir /home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps -L dependency=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps --extern proc_macro2=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps/libproc_macro2-f3902f4e534acb8e.rlib --extern quote=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps/libquote-d1c35e16b2cf01ea.rlib --extern syn=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps/libsyn-b1050101a14e2a06.rlib --extern proc_macro -C link-arg=-fuse-ld=mold`
     Running `CARGO=/opt/rust-bin-1.74.1/bin/cargo-bin-1.74.1 CARGO_CRATE_NAME=alacritty_config_derive CARGO_MANIFEST_DIR=/home/kchibisov/src/rust/alacritty-workspace/fork/alacritty_config_derive CARGO_PKG_AUTHORS='Christian Duerr <[email protected]>' CARGO_PKG_DESCRIPTION='Failure resistant deserialization derive' CARGO_PKG_HOMEPAGE='https://github.com/alacritty/alacritty' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=alacritty_config_derive CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION=1.70.0 CARGO_PKG_VERSION=0.2.3-dev CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=3 CARGO_PKG_VERSION_PRE=dev CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps:/opt/rust-bin-1.74.1/lib:/home/kchibisov/.rustup/toolchains/gentoo/lib' /home/kchibisov/.rustup/toolchains/gentoo/bin/rustc --crate-name alacritty_config_derive --edition=2021 alacritty_config_derive/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type proc-macro --emit=dep-info,link -C prefer-dynamic -C opt-level=3 -C embed-bitcode=no -C debuginfo=1 -C metadata=e13a7f7d7b2067ba -C extra-filename=-e13a7f7d7b2067ba --out-dir /home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps -L dependency=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps --extern proc_macro2=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps/libproc_macro2-f3902f4e534acb8e.rlib --extern quote=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps/libquote-d1c35e16b2cf01ea.rlib --extern syn=/home/kchibisov/src/rust/alacritty-workspace/fork/target/release/deps/libsyn-b1050101a14e2a06.rlib --extern proc_macro -C link-arg=-fuse-ld=mold`

And the difference in each line is only the following:

embed-bitcode=no -C debug-assertions=off -C metadata=f11f6d33ed698a19 -C extra-filename=-f11f6d33ed698a19
opt-level=3 -C embed-bitcode=no -C debuginfo=1 -C metadata=e13a7f7d7b2067ba -C extra-filename=-e13a7f7d7b2067ba

which is just different optimization level, but I build debug and release at the same time when doing release, so it sounds weird? I've tried removing profile in workspace and it didn't help.

Steps

  1. https://github.com/alacritty/alacritty/
  2. cargo clean; cargo build --jobs 1 -vvv 2> debug.txt >&2
  3. cargo clean; cargo build --release --jobs 1 -vvv 2> release.txt >&2

Then you can sort the files, remove empty lines and some garbage, and diff them to discover the diff in basically one line and after you remove it the end diff is the same with the same amount of occurrences for alacritty_config_derive.

Possible Solution(s)

No response

Notes

I've seen #10645 , but I don't have different features and I'm not sure how to solve that, it seems like some .so artifacts are not used at all.

Version

cargo 1.74.1 (ecb9851af 2023-10-18)
@kchibisov kchibisov added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Jan 18, 2024
@epage
Copy link
Contributor

epage commented Jan 18, 2024

proc macros, build scripts, and their dependencies get built for your host computer while everything else gets built for the target. For most people, these are probably the same thing. In dealing with this, cargo tries to unify them as much as possible to remove redundancy but there are likely cases where it can't or isn't yet. This unification is a fairly complicated task.

I wonder if this is related to #11252. I know a previous version of it caused even more duplicate builds but don't remember if that still had some or not.

@weihanglo
Copy link
Member

weihanglo commented Jan 19, 2024

This test might be related:

// This may be surprising that `foo` doesn't get built separately. It is
// because pm might have other units (binaries, tests, etc.), and so the
// feature resolver must assume that normal deps get unified with it. This
// is related to the bigger issue where the features selected in a
// workspace depend on which packages are selected.

So this might be the case:

  • Cargo doesn't consider a proc-macro workspace member as a build dependency (build script, proc macros), so alacritty_config_derive was built with the release profile.
  • On the other hand, alacritty_config_derive is used by alacritty as a proc-macro, so it was built as a build dependency, which uses a special build profile.

@weihanglo
Copy link
Member

Try adding this. It should combine them into one rustc invocation.

[profile.release.package.alacritty_config_derive]
opt-level = 0
debug = 0

@kchibisov
Copy link
Author

I'd like to add that I've initially noticed it with sqlx and it's macros feature, where the difference is around ~70 crates for release/debug build(I don't have any profile and it's not a workspace).

Try adding this. It should combine them into one rustc invocation.

Yeah, that fixes it. But I'd assume that the macro itself will run slower.

@weihanglo weihanglo added A-workspaces Area: workspaces A-profiles Area: profiles S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted. A-proc-macro Area: compiling proc-macros and removed S-triage Status: This issue is waiting on initial triage. labels Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macro Area: compiling proc-macros A-profiles Area: profiles A-workspaces Area: workspaces C-bug Category: bug S-needs-design Status: Needs someone to work further on the design for the feature or fix. NOT YET accepted.
Projects
None yet
Development

No branches or pull requests

3 participants