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/init: avoid target.name assignments if possible #13606

Merged
merged 1 commit into from
Mar 19, 2024

Conversation

dvdhrm
Copy link
Contributor

@dvdhrm dvdhrm commented Mar 19, 2024

Make cargo init skip target.name assignments if the default value is suitable.

Currently, all paths of cargo init will set target-names to the package-name, ensuring that a suitable target-name is set. This is required for all targets but libraries. Unfortunately, library-names have more restrictions than other targets. For example, creating a library with dashes will lead to errors:

mkdir foo-bar
cd foo-bar
touch foo-bar.rs
cargo init --lib

Fortunately, target-names for libraries are inferred from the package-name. Hence, by omitting the target-name, a valid configuration will be produced.

Instead of adjusting SourceFileInformation::target_name to use Option<String>, this commit strips the field completely, since all callers set it to the package-name.

Fixes: #11259

Make `cargo init` skip `target.name` assignments if the default value is
suitable.

Currently, all paths of `cargo init` will set target-names to the
package-name, ensuring that a suitable target-name is set. This is
required for all targets but libraries. Unfortunately, library-names
have more restrictions than other targets. For example, creating a
library with dashes will lead to errors:

    mkdir foo-bar
    cd foo-bar
    touch foo-bar.rs
    cargo init --lib

Fortunately, target-names for libraries are inferred from the
package-name. Hence, by omitting the target-name, a valid configuration
will be produced.

Instead of adjusting `SourceFileInformation::target_name` to use
`Option<String>`, this commit strips the field completely, since all
callers set it to the package-name.

Signed-off-by: David Rheinsberg <david@readahead.eu>
@rustbot
Copy link
Collaborator

rustbot commented Mar 19, 2024

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added Command-new S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 19, 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.

Make sense. Thanks!

@weihanglo
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Mar 19, 2024

📌 Commit b673c10 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 Mar 19, 2024
@bors
Copy link
Contributor

bors commented Mar 19, 2024

⌛ Testing commit b673c10 with merge ecb89a1...

@bors
Copy link
Contributor

bors commented Mar 19, 2024

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

@bors bors merged commit ecb89a1 into rust-lang:master Mar 19, 2024
23 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
@weihanglo
Copy link
Member

Note for the change:

While I understand people may use cargo new/cargo init in automation processes, the output Cargo.toml is not intended to consume in that way. So this is not considered as a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Command-new 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.

cargo init with existing lib source file with hyphen in name makes bad Cargo.toml
5 participants