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: prevent dashes in lib.name #12783

Merged
merged 3 commits into from Mar 16, 2024
Merged

cargo: prevent dashes in lib.name #12783

merged 3 commits into from Mar 16, 2024

Conversation

dvdhrm
Copy link
Contributor

@dvdhrm dvdhrm commented Oct 6, 2023

The TOML parser of Cargo currently refuses lib.name entries that contain dashes. Unfortunately, it uses the package-name as default if no explicit lib.name entry is specified. This package-name, however, can contain dashes.

Cargo documentation states that the package name is converted first, yet this was never implemented by the code-base.

Fix this inconsistency and convert the package name to a suitable crate-name first.

This fixes #12780. It is an alternative to #12640.

@rustbot
Copy link
Collaborator

rustbot commented Oct 6, 2023

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) soon.

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 Oct 6, 2023
@dvdhrm
Copy link
Contributor Author

dvdhrm commented Oct 6, 2023

(update: fixed rustfmt complaints)

@epage epage added the T-cargo Team: Cargo label Oct 6, 2023
@epage
Copy link
Contributor

epage commented Oct 6, 2023

@rfcbot fcp merge

When auto-filling lib.name, cargo has a discrepancy

  • documentation: says lib.name will use _
  • implementation: any - -> _ conversion happens later in the process, missing some areas like cargo metadata and environment variables set for artifact dependencies

We talked in the cargo team meeting about leaning towards making the code match the documentation. This FCP is a strawpoll to confirm that.

In stable rust, this only affects the lib name in cargo metadata. I've been looking for any other way this may manifest, including our environment variables and haven't seen any so far.

@rfcbot
Copy link
Collaborator

rfcbot commented Oct 6, 2023

Team member @epage has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels Oct 6, 2023
@ehuss
Copy link
Contributor

ehuss commented Oct 7, 2023

Hm, I didn't think about the consequence of this impacting the artifact-dependency environment variables, which appears to be a breaking change. For example, I did a quick code search and found https://github.com/lf-/clipper/blob/dd1caf13191e0fa58afbc3680fb83782c20e63d2/fixtures/dlopen-openssl-fixture/src/main.rs#L9 which would break with this change.

I'm not sure what to do about that. Perhaps one option is to set both environment variables for backwards compatibility?

@dvdhrm
Copy link
Contributor Author

dvdhrm commented Oct 8, 2023

The RFC says the env-var is named after the "crate", so this is actually a fix. But that is more of a theoretical argument, it is still a breaking change.

@epage
Copy link
Contributor

epage commented Oct 9, 2023

. Perhaps one option is to set both environment variables for backwards compatibility?

If that works, it seems reasonable.

@rustbot rustbot added the A-manifest Area: Cargo.toml issues label Oct 13, 2023
@dvdhrm
Copy link
Contributor Author

dvdhrm commented Oct 13, 2023

(update: we now remember whether target-names were inferred, which in turn allows us to keep setting the previously used artifact env-vars)

@dvdhrm
Copy link
Contributor Author

dvdhrm commented Oct 13, 2023

The bindeps and multidep features of Cargo are unstable, but the new revision of this PR tries to avoid breakage in the env-vars of these features. Unfortunately, dash->underscore conversion is lossy, so we cannot reliably tell whether compatibility-mode is required. Therefore, I extended the Cargo-internal manifest state to remember whether a target-name was inferred, or whether it is explicit. Now we know whether a target-name used to be different in the past, and we can use this information to keep backwards-compatibility.

Note that I think the stripped env-var of bindeps is quite confusing, since changing the dep-name locally will affect which env-var becomes the stripped one. I think I did my best to retain this behavior, so this should be fine.

Add a boolean state to `Target` that tells us whether the name of the
target was inferred by Cargo, or whether it was directly specified in
the Manifest.

This value will be required in the future, to allow changing the
inferred names of targets, but retaining enough information to keep
backwards compatibility.
We are about to change the default value for target-names of libraries.
They used to match the package-name. In the future, they will use the
package-name with dashes converted to underscores. This will affect the
artifact env-variables, since they expose target-names. Hence, set the
old env-vars, too, to avoid breakage.

Note that we do not retain the name of a target before it was converted,
and the conversion is lossy, so we cannot reconstruct it. However, we
can rely on the fact that the conversion only happens for default values
(since user-supplied values never allowed dashes). Furthermore, we now
remember whether a target-name was inferred, so we can exactly
reconstruct whether a library-target could have contained dashes in
older releases, or not.
The TOML parser of Cargo currently refuses `lib.name` entries that
contain dashes. Unfortunately, it uses the package-name as default if no
explicit `lib.name` entry is specified. This package-name, however, can
contain dashes.

Cargo documentation states that the package name is converted first, yet
this was never implemented by the code-base.

Fix this inconsistency and convert the package name to a suitable
crate-name first.
@dvdhrm
Copy link
Contributor Author

dvdhrm commented Oct 17, 2023

(update: rebase to master to fix CI failures)

@dvdhrm
Copy link
Contributor Author

dvdhrm commented Dec 21, 2023

(Any update on this?)

@ehuss
Copy link
Contributor

ehuss commented Mar 1, 2024

@Eh2406, @Muscraft, @joshtriplett do you think you can check the FCP above? Do you have any questions or concerns? Anything that can help you understand this change?

@Eh2406
Copy link
Contributor

Eh2406 commented Mar 5, 2024

Sorry for the delay in ✔️

@rfcbot rfcbot added the final-comment-period FCP — a period for last comments before action is taken label Mar 5, 2024
@rfcbot
Copy link
Collaborator

rfcbot commented Mar 5, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Mar 16, 2024
@bors
Copy link
Collaborator

bors commented Mar 16, 2024

⌛ Testing commit 3ca04e2 with merge 6972630...

@bors
Copy link
Collaborator

bors commented Mar 16, 2024

☀️ Test successful - checks-actions
Approved by: ehuss
Pushing 6972630 to master...

@bors bors merged commit 6972630 into rust-lang:master Mar 16, 2024
20 checks passed
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 20, 2024
Update cargo

5 commits in 2fe739fcf16c5bf8c2064ab9d357f4a0e6c8539b..d438c80c45c24be676ef5867edc79d0a14910efe
2024-03-15 21:39:18 +0000 to 2024-03-19 16:11:22 +0000
- refactor(toml): Expose surce/spans for VirtualManifests (rust-lang/cargo#13603)
- cargo/init: avoid target.name assignments if possible (rust-lang/cargo#13606)
- chore: Fix minor grammar nit in command-line help (rust-lang/cargo#13602)
- Bump to 0.80.0; update changelog (rust-lang/cargo#13604)
- cargo: prevent dashes in lib.name (rust-lang/cargo#12783)

r? ghost
@rustbot rustbot added this to the 1.79.0 milestone Mar 20, 2024
tamird added a commit to aya-rs/rustc-llvm-proxy that referenced this pull request Mar 21, 2024
bors added a commit that referenced this pull request Mar 25, 2024
Fix doc collision for lib/bin with a dash in the inferred name.

This fixes an issue where `cargo doc` would report a collision if the project has a `-` in the name, and both a lib and bin. As a consequence of the change in #12783, the target name for the library no longer has a `-`. The code that checks for the lib/bin doc collision was only checking if the target names were equal, which is no longer the case after #12783. The solution here is to use the `crate_name` and not the target name, which has the appropriate `_` translation done. This is the more correct method to use, even before #12783, because rustdoc uses crate names, not target names (and thus even documenting bins have their filenames converted to underscores).

Fixes #13628
tronical added a commit to slint-ui/slint that referenced this pull request Mar 27, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
tronical added a commit to slint-ui/slint that referenced this pull request Mar 27, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
tronical added a commit to slint-ui/slint that referenced this pull request Mar 27, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
adpaco-aws added a commit to model-checking/kani that referenced this pull request Mar 27, 2024
Upgrades the Rust toolchain to `nightly-2024-03-21`. The relevant
changes in Rust are:
* rust-lang/rust#122480
* rust-lang/rust#122748
* rust-lang/cargo#12783

I wasn't confident that our regression testing could detect differences
in the file paths being generated with the new logic, so I added code
similar to the following just to check they were equivalent:
```rust
             let base_filename = tcx.output_filenames(()).output_path(OutputType::Object);
+            let binding = tcx.output_filenames(()).path(OutputType::Object);
+            let base_filename2 = binding.as_path();
+            assert_eq!(base_filename, base_filename2);
```
Note that this was done for each instance where we used `output_path`,
and completed regression testing with the previous toolchain version. I
also checked manually the instance where we generate a `.dot` graph for
debug purposes following the instructions
[here](https://model-checking.github.io/kani/cheat-sheets.html?highlight=dot#debug)
(a `libmain.dot` file was generated for the `main.rs` in one of my
projects).

---------

Co-authored-by: Celina G. Val <celinval@amazon.com>
tronical added a commit to slint-ui/slint that referenced this pull request Mar 28, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
tronical added a commit to slint-ui/slint that referenced this pull request Mar 28, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
tronical added a commit to slint-ui/slint that referenced this pull request Apr 3, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
ogoffart pushed a commit to slint-ui/slint that referenced this pull request Apr 5, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
tronical added a commit to slint-ui/slint that referenced this pull request Apr 16, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
tronical added a commit to slint-ui/slint that referenced this pull request Apr 16, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
sky5454 pushed a commit to sky5454/slint that referenced this pull request Apr 17, 2024
Cargo disallows dashes in library target names (lib.name in Cargo.toml), but when not set it uses
the package name. That meant that dashes snuck in and rust-lang/cargo#12783 fixes that.

The targets Corrosion creates correspond to the library name, so after that change it's slint_cpp.

To make this work with stable cargo, explicitly switch lib.name to slint_cpp.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-manifest Area: Cargo.toml issues disposition-merge FCP with intent to merge finished-final-comment-period FCP complete S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-cargo Team: Cargo to-announce
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Inconsistent definitions for lib.name regarding hyphens
8 participants