Skip to content

CARGO_CRATE_NAME is set incorrectly for integration tests and examples #15572

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

Closed
ModProg opened this issue Sep 7, 2023 · 1 comment · Fixed by #15574
Closed

CARGO_CRATE_NAME is set incorrectly for integration tests and examples #15572

ModProg opened this issue Sep 7, 2023 · 1 comment · Fixed by #15574
Labels
C-bug Category: bug

Comments

@ModProg
Copy link
Contributor

ModProg commented Sep 7, 2023

rust-analyzer version: rust-analyzer 1 (2df30e1 2023-09-03)

rustc version: rustc 1.72.0 (5680fa18f 2023-08-23)

rust-analyzer sets CARGO_CRATE_NAME incorrectly for examples and integration tests. Possibly related to #8149.

# Cargo.toml
[package]
name = "crate"
version = "0.1.0"
edition = "2021"

[dependencies]
crate-name.path = "crate-name"
// crate-name/src/lib.rs
#[proc_macro]
pub fn crate_name(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
    panic!("{}", std::env::var("CARGO_CRATE_NAME").unwrap(),)
}

Put crate_name::crate_name!{} in src/main.rs, tests/test.rs and example/example.rs.

cargo check prints:

#   Compiling crate v0.1.0 (/crate)
error: proc macro panicked
 --> tests/test.rs:1:1
  |
1 | crate_name::crate_name!();
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: test

error: proc macro panicked
 --> src/main.rs:2:5
  |
2 |     crate_name::crate_name!();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: crate

error: proc macro panicked
 --> examples/example.rs:2:5
  |
2 |     crate_name::crate_name!();
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: example

error: could not compile `crate` (bin "crate") due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `crate` (test "test") due to previous error
error: could not compile `crate` (bin "crate" test) due to previous error
error: could not compile `crate` (example "example") due to previous error

While rust-analyzer reports crate for all of them, meaning that it did not run crate_name with the correct name.

@alibektas
Copy link
Member

It seems that we use PKG_NAME as a replacement for the crate name.

// FIXME: This isn't really correct (a package can have many crates with different names), but
// it's better than leaving the variable unset.
env.set("CARGO_CRATE_NAME", CrateName::normalize_dashes(&package.name).to_string());

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

Successfully merging a pull request may close this issue.

2 participants