Skip to content

cargo/init: avoid target.name assignments if possible - #13606

Merged
bors merged 1 commit into
rust-lang:masterfrom
dvdhrm:pr/initlib
Mar 19, 2024
Merged

bors merged 1 commit into
rust-lang:masterfrom
dvdhrm:pr/initlib

Conversation

@dvdhrm

@dvdhrm dvdhrm commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

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 <[email protected]>
@rustbot

rustbot commented Mar 19, 2024

Copy link
Copy Markdown
Collaborator

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

@weihanglo weihanglo left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

@bors r+

@bors

bors commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

📌 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

bors commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

⌛ Testing commit b673c10 with merge ecb89a1...

@bors

bors commented Mar 19, 2024

Copy link
Copy Markdown
Contributor

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

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