cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Warn when uploading a new crate with a hyphen in its name

Open alice-i-cecile opened this issue 3 years ago • 3 comments

Problem

Hyphens in crate names suck for all sorts of reasons, see hyphens considered harmful. Unfortunately, cargo will happily allow you to upload a crate with a hyphen without warning.

Changing the name of crates once published appears to be effectively impossible as well.

Proposed Solution

When users are uploading a new crate, cancel the upload and warn the user if there is a hyphen in the name.

Allow users to bypass this by providing an explicit flag.

Notes

Related to #2775.

alice-i-cecile avatar Jul 10 '22 20:07 alice-i-cecile

Very much agree with this. I spent hours trying to figure out whether I should name crates with hyphens or underscore and there not being an official consensus on this is pretty weird (basically everything else has a convention). Since hyphens get converted to underscores under the hood anyways, this would be a good way to guide people towards more consistent naming of crates.

konstantinkopka avatar Jul 11 '22 06:07 konstantinkopka

Hyphens in crate names suck for all sorts of reasons, see hyphens considered harmful. Unfortunately, cargo will happily allow you to upload a crate with a hyphen without warning.

Do you mean crate names or package names? The linked RFC only speaks to crate names and not package names. I don't think I've seen a consensus that underscrores should be preferred to hyphens in package names and making cargo opinionated without more context or consensus seems premature.

btw cargo add is now in cargo and it accepts either and normalizes them though there is still some room for improvement (e.g. https://github.com/rust-lang/cargo/issues/10680)

Changing the name of crates once published appears to be effectively impossible as well.

I've been bit with this with clap. A clap developer prematurely took the clap-man names but then another one made several clap_* crates. Now I feel I can't use the clap-man name because I'd like _ and - to be consistent within clap (I would have preferred - but that boat has sailed). I ended up having to go with the clap_mangen name to get out of this.

epage avatar Jul 11 '22 18:07 epage

Package names, my apologies. I've run into two serious headaches with these:

  • macros not working without special casing
  • divergences between the package name and how it's used; users will put foo-bar in their Cargo.toml, but use foo_bar in their library.

I also think there's a "confusability" argument to be made for progressively disallowing hyphens.

alice-i-cecile avatar Jul 11 '22 18:07 alice-i-cecile

I'm going to close, as I don't think we'll be adding a warning in the short term. There are far too many packages using hyphens, and there isn't an agreed-upon standard. People are free to pick which particular method they want, and some people strongly prefer one or the other.

I think if any change is made to steer people towards one form or the other will require an RFC. This would impact a large number of users, and I suspect it just isn't worth the investment.

If there are specific places where the friction could be reduced, I think it would be good to examine those without jumping to a solution first. For example cargo add will handle the translation for the user.

I'm not entire sure I understand the two points raised. Macros shouldn't have any involvement with package names. And for the second point, I'm not really clear because crate names (in Rust) can only have underscores.

I understand that the difference from a package name and the library crate name can cause some confusion. However, I think there are several other issues already covering that (such as #2775 and #6827). If there are specific situations where some use-case is difficult (such as whatever is alluded to with macros), I suggest opening a new issue with those specifics if there isn't already an issue for them.

ehuss avatar Aug 13 '22 19:08 ehuss