Skip to content

Commit 5ddad87

Browse files
committed
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.
2 parents d5b6ab2 + 506a477 commit 5ddad87

4 files changed

+11
-7
lines changed

crates/project-model/src/workspace.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! metadata` or `rust-project.json`) into representation stored in the salsa
33
//! database -- `CrateGraph`.
44
5-
use std::{collections::VecDeque, fmt, fs, process::Command, sync};
5+
use std::{collections::VecDeque, fmt, fs, process::Command, str::FromStr, sync};
66

77
use anyhow::{format_err, Context};
88
use base_db::{
@@ -1228,6 +1228,10 @@ fn add_target_crate_root(
12281228

12291229
let mut env = Env::default();
12301230
inject_cargo_env(pkg, &mut env);
1231+
if let Ok(cname) = String::from_str(cargo_name) {
1232+
// 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.
1233+
env.set("CARGO_CRATE_NAME", cname.replace("-", "_"));
1234+
}
12311235

12321236
if let Some(envs) = build_data.map(|it| &it.envs) {
12331237
for (k, v) in envs {

crates/project-model/test_data/output/cargo_hello_world_project_model.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
163163
"CARGO_PKG_VERSION": "0.1.0",
164164
"CARGO_PKG_AUTHORS": "",
165-
"CARGO_CRATE_NAME": "hello_world",
165+
"CARGO_CRATE_NAME": "an_example",
166166
"CARGO_PKG_LICENSE_FILE": "",
167167
"CARGO_PKG_HOMEPAGE": "",
168168
"CARGO_PKG_DESCRIPTION": "",
@@ -232,7 +232,7 @@
232232
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
233233
"CARGO_PKG_VERSION": "0.1.0",
234234
"CARGO_PKG_AUTHORS": "",
235-
"CARGO_CRATE_NAME": "hello_world",
235+
"CARGO_CRATE_NAME": "it",
236236
"CARGO_PKG_LICENSE_FILE": "",
237237
"CARGO_PKG_HOMEPAGE": "",
238238
"CARGO_PKG_DESCRIPTION": "",

crates/project-model/test_data/output/cargo_hello_world_project_model_with_selective_overrides.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
163163
"CARGO_PKG_VERSION": "0.1.0",
164164
"CARGO_PKG_AUTHORS": "",
165-
"CARGO_CRATE_NAME": "hello_world",
165+
"CARGO_CRATE_NAME": "an_example",
166166
"CARGO_PKG_LICENSE_FILE": "",
167167
"CARGO_PKG_HOMEPAGE": "",
168168
"CARGO_PKG_DESCRIPTION": "",
@@ -232,7 +232,7 @@
232232
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
233233
"CARGO_PKG_VERSION": "0.1.0",
234234
"CARGO_PKG_AUTHORS": "",
235-
"CARGO_CRATE_NAME": "hello_world",
235+
"CARGO_CRATE_NAME": "it",
236236
"CARGO_PKG_LICENSE_FILE": "",
237237
"CARGO_PKG_HOMEPAGE": "",
238238
"CARGO_PKG_DESCRIPTION": "",

crates/project-model/test_data/output/cargo_hello_world_project_model_with_wildcard_overrides.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
160160
"CARGO_PKG_VERSION": "0.1.0",
161161
"CARGO_PKG_AUTHORS": "",
162-
"CARGO_CRATE_NAME": "hello_world",
162+
"CARGO_CRATE_NAME": "an_example",
163163
"CARGO_PKG_LICENSE_FILE": "",
164164
"CARGO_PKG_HOMEPAGE": "",
165165
"CARGO_PKG_DESCRIPTION": "",
@@ -228,7 +228,7 @@
228228
"CARGO_MANIFEST_DIR": "$ROOT$hello-world",
229229
"CARGO_PKG_VERSION": "0.1.0",
230230
"CARGO_PKG_AUTHORS": "",
231-
"CARGO_CRATE_NAME": "hello_world",
231+
"CARGO_CRATE_NAME": "it",
232232
"CARGO_PKG_LICENSE_FILE": "",
233233
"CARGO_PKG_HOMEPAGE": "",
234234
"CARGO_PKG_DESCRIPTION": "",

0 commit comments

Comments
 (0)