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 --bin creates a lib crate when crate source file is present #9333

Closed
BartMassey opened this issue Apr 6, 2021 · 6 comments · Fixed by #9522
Closed

cargo init --bin creates a lib crate when crate source file is present #9333

BartMassey opened this issue Apr 6, 2021 · 6 comments · Fixed by #9522
Labels
C-bug Category: bug Command-init E-easy Experience: Easy

Comments

@BartMassey
Copy link
Contributor

BartMassey commented Apr 6, 2021

Problem

cargo init has a nice feature, in that if the file foo.rs exists in the directory foo that is being initialized, no src directory will be created and instead name = "foo" and path = "foo.rs" will be placed in Cargo.toml.

Unfortunately, cargo init --bin still produces a lib crate in this case.

Steps

  1. mkdir foo
  2. cd foo
  3. touch foo.rs
  4. cargo init --bin

The resulting Cargo.toml is

[package]
name = "foo"
version = "0.1.0"
authors = ["Me <me@example.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[lib]
name = "foo"
path = "foo.rs"

I would strongly prefer getting [[bin]] instead of [lib] here.

Notes

Output of cargo version: cargo 1.51.0 (43b129a20 2021-03-16)

@BartMassey BartMassey added the C-bug Category: bug label Apr 6, 2021
@BartMassey
Copy link
Contributor Author

BartMassey commented Apr 6, 2021

Apparently cargo init is deciding whether to create a binary or library crate depending on whether the source file (foo.rs above) contains a definition of main() — replace step 3 above with

echo 'fn main() {}' >foo.rs

and get a binary crate.

This is a reasonable heuristic, but I think the --bin and --lib flags should override it.

@ehuss ehuss added Command-init E-easy Experience: Easy labels May 20, 2021
@Aelnor
Copy link
Contributor

Aelnor commented May 24, 2021

What if we have a file with fn main() and the user asks for a --lib here?
Do we fail the whole process?

I'm ready to fix this one if we have an answer

@ehuss
Copy link
Contributor

ehuss commented May 24, 2021

I think an error would be fine, with hopefully a clear message that describes the conflict and maybe a suggestion on how to resolve it.

@BartMassey
Copy link
Contributor Author

A warning would be best, I think. It's conceivable that a particularly obdurate user intends to have a function named main in their library: the warning should be sufficient for the normal case.

@ehuss
Copy link
Contributor

ehuss commented May 25, 2021

A warning would fine, too! I don't feel strongly one way or the other.

@BartMassey
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug Command-init E-easy Experience: Easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants