Skip to content

Commit 58f3894

Browse files
committed
Auto merge of rust-lang#3644 - narpfel:local-crates-metadata-format-update, r=RalfJung
Fix "local crate" detection `PackageId` is an opaque identifier whose internal format is subject to change, so looking up the names of local crates by ID is more robust than parsing the ID. Resolves rust-lang#3643.
2 parents 8a7338a + 00644c1 commit 58f3894

File tree

8 files changed

+28
-12
lines changed

8 files changed

+28
-12
lines changed

src/tools/miri/cargo-miri/src/util.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::env;
23
use std::ffi::OsString;
34
use std::fs::File;
@@ -233,21 +234,18 @@ pub fn get_cargo_metadata() -> Metadata {
233234
}
234235

235236
/// Pulls all the crates in this workspace from the cargo metadata.
236-
/// Workspace members are emitted like "miri 0.1.0 (path+file:///path/to/miri)"
237237
/// Additionally, somewhere between cargo metadata and TyCtxt, '-' gets replaced with '_' so we
238238
/// make that same transformation here.
239239
pub fn local_crates(metadata: &Metadata) -> String {
240240
assert!(!metadata.workspace_members.is_empty());
241-
let mut local_crates = String::new();
242-
for member in &metadata.workspace_members {
243-
let name = member.repr.split(' ').next().unwrap();
244-
let name = name.replace('-', "_");
245-
local_crates.push_str(&name);
246-
local_crates.push(',');
247-
}
248-
local_crates.pop(); // Remove the trailing ','
249-
250-
local_crates
241+
let package_name_by_id: HashMap<_, _> =
242+
metadata.packages.iter().map(|package| (&package.id, package.name.as_str())).collect();
243+
metadata
244+
.workspace_members
245+
.iter()
246+
.map(|id| package_name_by_id[id].replace('-', "_"))
247+
.collect::<Vec<_>>()
248+
.join(",")
251249
}
252250

253251
/// Debug-print a command that is going to be run.

src/tools/miri/test-cargo-miri/Cargo.lock

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ dependencies = [
123123
"byteorder 1.5.0",
124124
]
125125

126+
[[package]]
127+
name = "test-local-crate-detection"
128+
version = "0.1.0"
129+
126130
[[package]]
127131
name = "unicode-ident"
128132
version = "1.0.12"

src/tools/miri/test-cargo-miri/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["subcrate", "issue-1567", "exported-symbol-dep"]
2+
members = ["subcrate", "issue-1567", "exported-symbol-dep", "test-local-crate-detection"]
33
exclude = ["no-std-smoke"] # it wants to be panic="abort"
44

55
[package]

src/tools/miri/test-cargo-miri/run-test.py

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def test_cargo_miri_run():
131131
cargo_miri("run") + ["--target-dir=custom-run", "--", "--target-dir=target/custom-run"],
132132
"run.args.stdout.ref", "run.custom-target-dir.stderr.ref",
133133
)
134+
test("`cargo miri run --package=test-local-crate-detection` (test local crate detection)",
135+
cargo_miri("run") + ["--package=test-local-crate-detection"],
136+
"run.local_crate.stdout.ref", "run.local_crate.stderr.ref",
137+
)
134138

135139
def test_cargo_miri_test():
136140
# rustdoc is not run on foreign targets

src/tools/miri/test-cargo-miri/run.local_crate.stderr.ref

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
subcrate,issue_1567,exported_symbol_dep,test_local_crate_detection,cargo_miri_test,cdylib,exported_symbol,issue_1691,issue_1705,issue_rust_86261,proc_macro_crate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "test-local-crate-detection"
3+
version = "0.1.0"
4+
edition = "2021"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
// Make sure we detect all crates from this workspace as "local".
3+
// The env var is set during the "build" so we can use `env!` to access it directly.
4+
println!("{}", env!("MIRI_LOCAL_CRATES"));
5+
}

0 commit comments

Comments
 (0)