about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-08 07:25:34 +0000
committerbors <bors@rust-lang.org>2023-09-08 07:25:34 +0000
commit5ddad87b1a5ea2c71e3d28bb9f14b2382ca206fa (patch)
tree268be67264dd86bb8745c1b62574df4532036ed0
parentd5b6ab2fa593f527ad8d0c4fd1038baf91cdd3e7 (diff)
parent506a477acf98a64887fa99d4b821235be0604449 (diff)
downloadrust-5ddad87b1a5ea2c71e3d28bb9f14b2382ca206fa.tar.gz
rust-5ddad87b1a5ea2c71e3d28bb9f14b2382ca206fa.zip
Auto merge of #15574 - alibektas:15572/false_crate_name_env, r=Veykril
minor : use crate name for `CARGO_CRATE_NAME`

fixes #15572 . Until now we used the package name as a replacement of crate name. With this PR r-a first sets all the env variables it set before and on top of those it tries to set `CARGO_CRATE_NAME` to crates name, following envvar's naming convention.
-rw-r--r--crates/project-model/src/workspace.rs6
-rw-r--r--crates/project-model/test_data/output/cargo_hello_world_project_model.txt4
-rw-r--r--crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt4
-rw-r--r--crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt4
4 files changed, 11 insertions, 7 deletions
diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs
index 13463e9f72e..8cb0e65be1d 100644
--- a/crates/project-model/src/workspace.rs
+++ b/crates/project-model/src/workspace.rs
@@ -2,7 +2,7 @@
 //! metadata` or `rust-project.json`) into representation stored in the salsa
 //! database -- `CrateGraph`.
 
-use std::{collections::VecDeque, fmt, fs, process::Command, sync};
+use std::{collections::VecDeque, fmt, fs, process::Command, str::FromStr, sync};
 
 use anyhow::{format_err, Context};
 use base_db::{
@@ -1228,6 +1228,10 @@ fn add_target_crate_root(
 
     let mut env = Env::default();
     inject_cargo_env(pkg, &mut env);
+    if let Ok(cname) = String::from_str(cargo_name) {
+        // CARGO_CRATE_NAME is the name of the Cargo target with - converted to _, such as the name of the library, binary, example, integration test, or benchmark.
+        env.set("CARGO_CRATE_NAME", cname.replace("-", "_"));
+    }
 
     if let Some(envs) = build_data.map(|it| &it.envs) {
         for (k, v) in envs {
diff --git a/crates/project-model/test_data/output/cargo_hello_world_project_model.txt b/crates/project-model/test_data/output/cargo_hello_world_project_model.txt
index e595cd82729..447c7e5c869 100644
--- a/crates/project-model/test_data/output/cargo_hello_world_project_model.txt
+++ b/crates/project-model/test_data/output/cargo_hello_world_project_model.txt
@@ -162,7 +162,7 @@
                 "CARGO_MANIFEST_DIR": "$ROOT$hello-world",
                 "CARGO_PKG_VERSION": "0.1.0",
                 "CARGO_PKG_AUTHORS": "",
-                "CARGO_CRATE_NAME": "hello_world",
+                "CARGO_CRATE_NAME": "an_example",
                 "CARGO_PKG_LICENSE_FILE": "",
                 "CARGO_PKG_HOMEPAGE": "",
                 "CARGO_PKG_DESCRIPTION": "",
@@ -232,7 +232,7 @@
                 "CARGO_MANIFEST_DIR": "$ROOT$hello-world",
                 "CARGO_PKG_VERSION": "0.1.0",
                 "CARGO_PKG_AUTHORS": "",
-                "CARGO_CRATE_NAME": "hello_world",
+                "CARGO_CRATE_NAME": "it",
                 "CARGO_PKG_LICENSE_FILE": "",
                 "CARGO_PKG_HOMEPAGE": "",
                 "CARGO_PKG_DESCRIPTION": "",
diff --git a/crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt b/crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt
index e595cd82729..447c7e5c869 100644
--- a/crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt
+++ b/crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt
@@ -162,7 +162,7 @@
                 "CARGO_MANIFEST_DIR": "$ROOT$hello-world",
                 "CARGO_PKG_VERSION": "0.1.0",
                 "CARGO_PKG_AUTHORS": "",
-                "CARGO_CRATE_NAME": "hello_world",
+                "CARGO_CRATE_NAME": "an_example",
                 "CARGO_PKG_LICENSE_FILE": "",
                 "CARGO_PKG_HOMEPAGE": "",
                 "CARGO_PKG_DESCRIPTION": "",
@@ -232,7 +232,7 @@
                 "CARGO_MANIFEST_DIR": "$ROOT$hello-world",
                 "CARGO_PKG_VERSION": "0.1.0",
                 "CARGO_PKG_AUTHORS": "",
-                "CARGO_CRATE_NAME": "hello_world",
+                "CARGO_CRATE_NAME": "it",
                 "CARGO_PKG_LICENSE_FILE": "",
                 "CARGO_PKG_HOMEPAGE": "",
                 "CARGO_PKG_DESCRIPTION": "",
diff --git a/crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt b/crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt
index f10c55d0462..2e8ece82a10 100644
--- a/crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt
+++ b/crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt
@@ -159,7 +159,7 @@
                 "CARGO_MANIFEST_DIR": "$ROOT$hello-world",
                 "CARGO_PKG_VERSION": "0.1.0",
                 "CARGO_PKG_AUTHORS": "",
-                "CARGO_CRATE_NAME": "hello_world",
+                "CARGO_CRATE_NAME": "an_example",
                 "CARGO_PKG_LICENSE_FILE": "",
                 "CARGO_PKG_HOMEPAGE": "",
                 "CARGO_PKG_DESCRIPTION": "",
@@ -228,7 +228,7 @@
                 "CARGO_MANIFEST_DIR": "$ROOT$hello-world",
                 "CARGO_PKG_VERSION": "0.1.0",
                 "CARGO_PKG_AUTHORS": "",
-                "CARGO_CRATE_NAME": "hello_world",
+                "CARGO_CRATE_NAME": "it",
                 "CARGO_PKG_LICENSE_FILE": "",
                 "CARGO_PKG_HOMEPAGE": "",
                 "CARGO_PKG_DESCRIPTION": "",