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

Fix: Build only the specified artifact library when multiple types are available #13842

Merged

Conversation

skogseth
Copy link
Contributor

@skogseth skogseth commented May 2, 2024

What does this PR try to resolve?

Fixes #12109.

TL;DR:

A crate bar exposes it's library as both a staticlib and a cdylib. A second crate foo specifies an artifact dependency of type staticlib on bar, meaning cargo should build bar as a staticlib and expose certain environment variables (e.g. CARGO_STATICLIB_FILE_BAR). However, due to a bug, cargo ends up building (and exposing the associated environment variables) for both the staticlib and cdylib.

The same happens if foo specifies an artifact dependency of type cdylib; both artifact types are built.

How should we test and review this PR?

The first commit introduces a test which reproduces the issue, the second commit introduces the fix. This setup was recommended by @ehuss.

Additional information

Artifact dependencies: https://rust-lang.github.io/rfcs/3028-cargo-binary-dependencies.html

TL;DR

If a crate foo requests an artifact dependency of kind <artifact_kind> from a crate bar then the following happens:

  • bar is built with crate-type <artifact_kind> and a directory is created at target/<profile>/deps/artifact/bar-<build_hash_or_something>/<artifact_kind>. The binary artifact is placed in this directory.
  • Cargo exposes certain environment variables, the most important for this PR is CARGO_<artifact_kind>_FILE_BAR, which points to the binary artifact that was specified. This environment variable is available at compile time for normal dependencies and at runtime for build-dependencies.

If multiple artifact-kinds are requested cargo will create a unit for each, and so they will all be built separately.

@rustbot
Copy link
Collaborator

rustbot commented May 2, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @weihanglo (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2024
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good! I added some minor suggestions.

Thanks for your contribution!

tests/testsuite/artifact_dep.rs Show resolved Hide resolved
tests/testsuite/artifact_dep.rs Show resolved Hide resolved
tests/testsuite/artifact_dep.rs Show resolved Hide resolved
src/cargo/core/compiler/unit_dependencies.rs Outdated Show resolved Hide resolved
@skogseth skogseth force-pushed the build-only-specified-artifact-library branch from a0e5a36 to 9a5cfbc Compare May 7, 2024 11:21
@weihanglo
Copy link
Member

Thank you for the patch!

@bors r+

@bors
Copy link
Contributor

bors commented May 7, 2024

📌 Commit 9a5cfbc has been approved by weihanglo

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 7, 2024
@bors
Copy link
Contributor

bors commented May 7, 2024

⌛ Testing commit 9a5cfbc with merge 4e59631...

@bors
Copy link
Contributor

bors commented May 7, 2024

☀️ Test successful - checks-actions
Approved by: weihanglo
Pushing 4e59631 to master...

@bors bors merged commit 4e59631 into rust-lang:master May 7, 2024
21 checks passed
@skogseth skogseth deleted the build-only-specified-artifact-library branch May 7, 2024 13:46
bors added a commit that referenced this pull request May 7, 2024
test: clean up unnecessary uses of `match_exact`

It was found during the review of <#13842 (comment)>

We should be happy using `assert_match_exact` directly.
The extra arguments to `match_exact` are not necessary for those test cases.
bors added a commit that referenced this pull request May 7, 2024
test: clean up unnecessary uses of `match_exact`

It was found during the review of <#13842 (comment)>

We should be happy using `assert_match_exact` directly.
The extra arguments to `match_exact` are not necessary for those test cases.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 8, 2024
Update cargo

10 commits in 05364cb2f61a2c2b091e061c1f42b207dfb5f81f..0ca60e940821c311c9b25a6423b59ccdbcea218f
2024-05-03 16:48:59 +0000 to 2024-05-08 01:54:25 +0000
- chore: Add cargo-lints to unstable docs (rust-lang/cargo#13881)
- test: clean up unnecessary uses of `match_exact` (rust-lang/cargo#13879)
- docs(ref): Correct heading level of `[lints]` documentation (rust-lang/cargo#13878)
- Fix: Build only the specified artifact library when multiple types are available (rust-lang/cargo#13842)
- docs: add missing CARGO_MAKEFLAGS env for plugins (rust-lang/cargo#13872)
- Add more documentation to `cargo::rustc-check-cfg` (rust-lang/cargo#13869)
- fix(toml): Remove unstable rejrected frontmatter syntax for cargo script (rust-lang/cargo#13861)
- Update UI example code in contributor guide (rust-lang/cargo#13864)
- style(test): Remove check-cfg warning (rust-lang/cargo#13865)
- Fix global_cache_tracker::max_download_size test flakiness (rust-lang/cargo#13860)

r? ghost
@rustbot rustbot added this to the 1.80.0 milestone May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

artifact-dependencies seems to build both cdylib and staticlib when only one is specified
4 participants