0

Could there be any future problem in naming a dependency .rs for example,

[dependencies]
gccjit.rs = { git = "https://github.com/swgillespie/gccjit.rs.git" }

In the above code, I use .rs for something which is not a Rust source code file. Is that not ideal or is it okay because it would be easier to default to the same name as the repository?

If an object is named .rs it could maybe be automatically recognized as Rust source code but in this case it is not.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424

1 Answers1

7

Yes, there is a problem. Do not do this.

How you could have determined this for yourself

Try to use the code that you've proposed, you'll see:

$ cargo build
error: failed to parse manifest at `.../Cargo.toml`

Caused by:
  could not parse input as TOML

Caused by:
  expected an equals, found a period at line 9

You could have also attempted to create a package with the same name. You would have then seen:

$ cargo new 'gccjit.rs.git'
error: Invalid character `.` in crate name: `gccjit.rs.git`
use --name to override crate name

Who controls crate names

You don't get to control the name of an imported crate that way; the crate determines it and it's already picked one:

[package]
name = "gccjit"

If you want to rename an existing package on import, you have to use the package key to match the real name:

some_name = { package = "gccjit", git = "https://github.com/swgillespie/gccjit.rs.git" }

See How to idiomatically alias a crate in Rust 2018? for more.

To use a period in the name, you can seemingly use a string key (although I think this is a bug):

"gcc.jit" = { package = "gccjit", git = "https://github.com/swgillespie/gccjit.rs.git" }

However, a package name has to be a valid Rust identifier, which periods are not. If you do this, there's no way to use the package.

It's redundant anyway

More opinion based, such a name is completely pointless. You don't need to say "rs" or "rust" in the name because of course it's Rust code. If it wasn't, you couldn't use it as a dependency in the first place.

We don't call our packages "computer-source-code-awesome-tool" for the same reason — that much is implied.

Package names are different from source control

Cargo and Rust don't care what the name of your source control repository is. It's separate from the package name. While it's better to have them be somewhat similar, there's no real reason they have to be whatsoever related.

Package names are different from library names

This is a feature with a very small number of uses, but the name of your package (a.k.a. name of the crate on Crates.io) can be different from the name of your library (what is imported into the code).

Piston is the biggest "offender" of this that I know of:

[package]
name = "piston2d-graphics"
version = "0.30.0"

[lib]
name = "graphics"

Please do not use this as it's simply maddening to attempt to debug.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • I still don't understand why we're even given the possibility to rename it if renaming it only means breaking it. Why? – Niklas Rosencrantz Feb 13 '19 at 03:22
  • 2
    @NiklasRosencrantz *renaming it only means breaking it* — How did you get to that conclusion? I showed an example of properly renaming a crate (`some_name = { package="...`) before showing an example that misuses the feature (`"gcc.jit" = { package = "...`). – Shepmaster Feb 13 '19 at 03:28