Request: Better compiler hint for when using edition2018 style import without declaring edition = "2018" in Cargo.toml

Recently, I forgot declaring edition = "2018" in Cargo.toml and try to write use crate_name in the code. The error message was:

error[E0433]: failed to resolve: maybe a missing crate `crate_name`?
 --> src/main.rs:1:5
   |
 1 | use crate_name;
   |     ^^^^^^^^^^ maybe a missing crate `crate_name`?

This error message confused me for hours. I only find out how to fix it by remembering "edition".

Request

The compiler should add the following hints:

hint: Try setting edition to 2018
hint: Try adding `extern crate crate_name` for 2015 edition
4 Likes

When working with one crate, you should know its Rust edition. If your crate is in edition 2015, simply adding the edition field does not make it magically work. The cargo new command sets edition 2018 by default.

Actually, I was working on a monorepo of multiple crates.

Is following compiler hints by the letter supposed to fix everything?

cargo new has its own problems, such as creating a .git directory inside a git repository.

You can pass --vcs none.

1 Like

For convenient, I think that cargo new should avoid creating a .git directory when a .git directory already exists in one of the ancestor directories. But this should be in a separate proposal.

I just tried and both cargo init and cargo new when within a git tree correctly did not create another .git

3 Likes

Try making pull request to rustc. It's relatively easy to implement, since the error is attached to a node which can tell you which edition it is, so it's a matter of adding extra info to the error based on that.

2 Likes

There are some open issues regarding this, such as https://github.com/rust-lang/rust/issues/61914 or https://github.com/rust-lang/rust/issues/63323. Improvements are occasionally being added, such as https://github.com/rust-lang/rust/pull/71783.

@kornel you mentioned this when updating the diagnostic. Have you changed your opinion about 2015-specific diagnostics? I personally think it would be good to help in situations like this.

Yes, I was wrong about not needing 2015 version of the error.

1 Like

There are 3rd party extensions for better integration with workspaces, such as cargo ws create. (There are others, that's just the one I know.)

I think discussing the pros and cons of cargo new is off topic here. Not everyone uses cargo new, which is fine. Adding a hint to use to the 2018 edition is a good idea IMHO.

7 Likes

It already does, which I find a little annoying because my home directory is a git repository, and I still want Cargo to create git repositories under it. :slight_smile:

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.