diff --git a/crates/base-db/src/input.rs b/crates/base-db/src/input.rs index f5109339ad10..5cfd62f4e2e2 100644 --- a/crates/base-db/src/input.rs +++ b/crates/base-db/src/input.rs @@ -277,6 +277,7 @@ pub struct CrateData { pub root_file_id: FileId, pub edition: Edition, pub version: Option, + pub package_id: Option, /// A name used in the package's project declaration: for Cargo projects, /// its `[package].name` can be different for other project types or even /// absent (a dummy crate for the code snippet, for example). @@ -352,6 +353,7 @@ impl CrateGraph { edition: Edition, display_name: Option, version: Option, + package_id: Option, cfg_options: Arc, potential_cfg_options: Option>, mut env: Env, @@ -363,6 +365,7 @@ impl CrateGraph { root_file_id, edition, version, + package_id, display_name, cfg_options, potential_cfg_options, @@ -708,6 +711,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -719,6 +723,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -730,6 +735,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -755,6 +761,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -766,6 +773,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -788,6 +796,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -799,6 +808,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -810,6 +820,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -832,6 +843,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), @@ -843,6 +855,7 @@ mod tests { Edition2018, None, None, + None, Default::default(), Default::default(), Env::default(), diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 62c39217aef0..d1228a398ce8 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -250,6 +250,7 @@ impl Analysis { Edition::CURRENT, None, None, + None, Arc::new(cfg_options), None, Env::default(), @@ -588,19 +589,13 @@ impl Analysis { self.with_db(|db| db.crate_def_map(crate_id).files().collect()) } - /// Returns the crate with the given name - pub fn crate_with_name(&self, crate_name: &str) -> Cancellable> { + /// Returns the crate with the given package id + pub fn crate_with_id(&self, package_id: &str) -> Cancellable> { self.with_db(|db| { let graph = db.crate_graph(); let id = graph.iter().find(|id| { let crate_data = &graph[*id]; - match &crate_data.origin { - CrateOrigin::Local { name: Some(name), .. } if name.as_str() == crate_name => { - true - } - CrateOrigin::Library { name, .. } if name.as_str() == crate_name => true, - _ => false, - } + crate_data.package_id.as_deref() == Some(package_id) }); id }) diff --git a/crates/ide/src/status.rs b/crates/ide/src/status.rs index 67d6932da962..ef9afde65bb0 100644 --- a/crates/ide/src/status.rs +++ b/crates/ide/src/status.rs @@ -62,6 +62,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option) -> String { root_file_id, edition, version, + package_id, display_name, cfg_options, potential_cfg_options, @@ -81,6 +82,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option) -> String { format_to!(buf, " Root module file id: {}\n", root_file_id.index()); format_to!(buf, " Edition: {}\n", edition); format_to!(buf, " Version: {}\n", version.as_deref().unwrap_or("n/a")); + format_to!(buf, " Package Id: {}\n", package_id.as_deref().unwrap_or("n/a")); format_to!(buf, " Enabled cfgs: {:?}\n", cfg_options); format_to!(buf, " Potential cfgs: {:?}\n", potential_cfg_options); format_to!(buf, " Env: {:?}\n", env); diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs index 71b9b61e2059..500d0d54bcd1 100644 --- a/crates/project-model/src/workspace.rs +++ b/crates/project-model/src/workspace.rs @@ -944,6 +944,7 @@ fn project_json_to_crate_graph( *edition, display_name.clone(), version.clone(), + None, Arc::new(cfg_options), None, env, @@ -1222,6 +1223,7 @@ fn detached_file_to_crate_graph( Edition::CURRENT, display_name.clone(), None, + None, cfg_options.clone(), None, Env::default(), @@ -1389,6 +1391,7 @@ fn add_target_crate_root( edition, Some(CrateDisplayName::from_canonical_name(cargo_name)), Some(pkg.version.to_string()), + Some(pkg.id.to_string()), Arc::new(cfg_options), potential_cfg_options.map(Arc::new), env, @@ -1525,6 +1528,7 @@ fn sysroot_to_crate_graph( Edition::CURRENT_FIXME, Some(display_name), None, + None, cfg_options.clone(), None, Env::default(), 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 3401d7f7e47f..5873f52e50cb 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 @@ -7,6 +7,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -69,6 +72,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -139,6 +145,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -209,6 +218,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -279,6 +291,9 @@ version: Some( "0.2.98", ), + package_id: Some( + "libc 0.2.98 (registry+https://github.com/rust-lang/crates.io-index)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( 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 3401d7f7e47f..5873f52e50cb 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 @@ -7,6 +7,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -69,6 +72,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -139,6 +145,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -209,6 +218,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -279,6 +291,9 @@ version: Some( "0.2.98", ), + package_id: Some( + "libc 0.2.98 (registry+https://github.com/rust-lang/crates.io-index)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( 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 491568d4b756..2f2edd78714c 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 @@ -7,6 +7,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -68,6 +71,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -137,6 +143,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -206,6 +215,9 @@ version: Some( "0.1.0", ), + package_id: Some( + "hello-world 0.1.0 (path+file://$ROOT$hello-world)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -275,6 +287,9 @@ version: Some( "0.2.98", ), + package_id: Some( + "libc 0.2.98 (registry+https://github.com/rust-lang/crates.io-index)", + ), display_name: Some( CrateDisplayName { crate_name: CrateName( diff --git a/crates/project-model/test_data/output/rust_project_cfg_groups.txt b/crates/project-model/test_data/output/rust_project_cfg_groups.txt index 8261e5a2d907..a1a012f0d639 100644 --- a/crates/project-model/test_data/output/rust_project_cfg_groups.txt +++ b/crates/project-model/test_data/output/rust_project_cfg_groups.txt @@ -5,6 +5,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -44,6 +45,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -74,6 +76,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -104,6 +107,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -134,6 +138,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -181,6 +186,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -211,6 +217,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -306,6 +313,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -336,6 +344,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -366,6 +375,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -396,6 +406,7 @@ ), edition: Edition2018, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -472,6 +483,7 @@ ), edition: Edition2018, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( diff --git a/crates/project-model/test_data/output/rust_project_hello_world_project_model.txt b/crates/project-model/test_data/output/rust_project_hello_world_project_model.txt index c123df80a6a3..733af3657379 100644 --- a/crates/project-model/test_data/output/rust_project_hello_world_project_model.txt +++ b/crates/project-model/test_data/output/rust_project_hello_world_project_model.txt @@ -5,6 +5,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -44,6 +45,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -74,6 +76,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -104,6 +107,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -134,6 +138,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -181,6 +186,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -211,6 +217,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -306,6 +313,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -336,6 +344,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -366,6 +375,7 @@ ), edition: Edition2021, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( @@ -396,6 +406,7 @@ ), edition: Edition2018, version: None, + package_id: None, display_name: Some( CrateDisplayName { crate_name: CrateName( diff --git a/crates/rust-analyzer/src/flycheck.rs b/crates/rust-analyzer/src/flycheck.rs index 5b5cc46b4ea0..5300c659b9df 100644 --- a/crates/rust-analyzer/src/flycheck.rs +++ b/crates/rust-analyzer/src/flycheck.rs @@ -161,7 +161,7 @@ pub(crate) enum FlycheckMessage { ClearDiagnostics { id: usize, generation: usize }, /// Request clearing outdated for a specific crate - ClearCrateDiagnostics { id: usize, generation: usize, crate_name: String }, + ClearCrateDiagnostics { id: usize, generation: usize, package_id: String }, /// Request check progress notification to client Progress { @@ -186,7 +186,7 @@ impl fmt::Debug for FlycheckMessage { .field("id", id) .field("generation", generation) .finish(), - FlycheckMessage::ClearCrateDiagnostics { id, generation, crate_name } => f + FlycheckMessage::ClearCrateDiagnostics { id, generation, package_id: crate_name } => f .debug_struct("ClearDiagnostics") .field("id", id) .field("generation", generation) @@ -359,7 +359,7 @@ impl FlycheckActor { self.send(FlycheckMessage::ClearCrateDiagnostics { id: self.id, generation: self.generation, - crate_name: msg.target.name.clone(), + package_id: msg.package_id.repr.clone(), }); self.report_progress(Progress::DidCheckCrate(msg.target.name)); } diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 4e60aa502d07..a7349f0172e9 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -1009,10 +1009,10 @@ impl GlobalState { Some(format!("rust-analyzer/flycheck/{id}")), ); } - FlycheckMessage::ClearCrateDiagnostics { id, generation, crate_name } => { + FlycheckMessage::ClearCrateDiagnostics { id, generation, package_id } => { let snap = self.snapshot(); - let Ok(Some(crate_id)) = snap.analysis.crate_with_name(&crate_name) else { - tracing::info!("Flycheck: could not find crate '{crate_name}'"); + let Ok(Some(crate_id)) = snap.analysis.crate_with_id(&package_id) else { + tracing::info!("Flycheck: could not find crate '{package_id}'"); return; }; let Ok(files) = snap.analysis.files_for(crate_id) else { diff --git a/crates/test-fixture/src/lib.rs b/crates/test-fixture/src/lib.rs index 593e31c2fbb8..e5a18a3fcfc2 100644 --- a/crates/test-fixture/src/lib.rs +++ b/crates/test-fixture/src/lib.rs @@ -200,6 +200,7 @@ impl ChangeFixture { meta.edition, Some(crate_name.clone().into()), version, + None, From::from(meta.cfg.clone()), Some(From::from(meta.cfg)), meta.env, @@ -238,6 +239,7 @@ impl ChangeFixture { Edition::CURRENT, Some(CrateName::new("test").unwrap().into()), None, + None, From::from(default_cfg.clone()), Some(From::from(default_cfg)), default_env, @@ -280,6 +282,7 @@ impl ChangeFixture { Edition::CURRENT, Some(CrateDisplayName::from_canonical_name("core")), None, + None, Default::default(), Default::default(), Env::from_iter([( @@ -327,6 +330,7 @@ impl ChangeFixture { Edition::CURRENT, Some(CrateDisplayName::from_canonical_name("proc_macros")), None, + None, Default::default(), Default::default(), Env::from_iter([(