Stream: t-compiler/rust-analyzer

Topic: rustc fails to resolve standard library items


Jonas Schievink [he/him] (Dec 09 2020 at 20:21, on Zulip):

It looks like r-a is failing to resolve libcore/std items on the rustc code base. I'm getting this output:

[ERROR project_model::workspace] cyclic deps: cargo_test_support(CrateId(156)) -> cargo(CrateId(144))
[ERROR project_model::workspace] cyclic deps: rustc_std_workspace_core(CrateId(1133)) -> core(CrateId(222))
[ERROR project_model::workspace] cyclic deps: wasi(CrateId(1649)) -> rustc_std_workspace_alloc(CrateId(1132))
Jonas Schievink [he/him] (Dec 09 2020 at 20:21, on Zulip):

These cyclic deps errors have shown up quite a bit in the past – is our project model broken somehow, or why do they appear?

Joshua Nelson (Dec 09 2020 at 20:21, on Zulip):

sounds like something to do with bootstrapping?

Joshua Nelson (Dec 09 2020 at 20:22, on Zulip):

although I guess libstd doesn't depend on rustc in the same sense as rust-analyzer thinks of it

Jonas Schievink [he/him] (Dec 09 2020 at 20:23, on Zulip):

Seems like r-a usually ends up using the standard library of whichever toolchain is installed, not libstd from the Rust checkout

Jonas Schievink [he/him] (Dec 09 2020 at 20:24, on Zulip):

Looks like @matklad has recently refactored the code dealing with cyclic deps

Joshua Nelson (Dec 09 2020 at 20:24, on Zulip):

ah yeah, rustc depending on master libstd strikes again :upside_down:

matklad (Dec 10 2020 at 13:13, on Zulip):

@Jonas Schievink yeah, cyclic deps are a long-standing bug.

The problem is that cargo doesn't quite forbid cyclic dependencies between packages. I think what cargo allows is for src/lib.rs with --test to depend on src/lib.rs without --test, and that doesn't quite pass our logic of optimistically enabling test.

matklad (Dec 10 2020 at 13:13, on Zulip):

That is, to solve this properly we need to dupe packages. Let me .entry().or_insert() this issue....

matklad (Dec 10 2020 at 13:15, on Zulip):

https://github.com/rust-analyzer/rust-analyzer/issues/2414#issuecomment-561213070

matklad (Dec 10 2020 at 13:18, on Zulip):

Although I am not sure why my changes cause it to fail if it worked before...

Jonas Schievink [he/him] (Dec 10 2020 at 13:20, on Zulip):

I see, that makes sense. Yeah, I'm not sure why it suddenly broke.

Jonas Schievink [he/him] (Dec 11 2020 at 21:25, on Zulip):

My current workaround:

diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs
index 7f4a7e56b..47aaad197 100644
--- a/crates/project_model/src/workspace.rs
+++ b/crates/project_model/src/workspace.rs
@@ -358,8 +358,8 @@ fn cargo_to_crate_graph(

     // Now add a dep edge from all targets of upstream to the lib
     // target of downstream.
-    for pkg in cargo.packages() {
-        for dep in cargo[pkg].dependencies.iter() {
+    for pkg in cargo.packages().collect::<Vec<_>>().into_iter().rev() {
+        for dep in cargo[pkg].dependencies.iter().rev() {
             let name = CrateName::new(&dep.name).unwrap();
             if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
                 for &from in pkg_crates.get(&pkg).into_iter().flatten() {
Jonas Schievink [he/him] (Dec 11 2020 at 21:31, on Zulip):

Welp, this line breaks r-a in libcore of a Rust checkout https://github.com/rust-lang/rust/blob/2225ee1b62ff089917434aefd9b2bf509cfa087f/library/core/src/lib.rs#L51

Jonas Schievink [he/him] (Dec 11 2020 at 21:31, on Zulip):

At least there I know why it happens and who did it (me)

Jonas Schievink [he/him] (Dec 11 2020 at 21:33, on Zulip):

I guess we don't set the --cfg test for sysroot crates, otherwise I would have broken everything

Last update: Jul 26 2021 at 14:30UTC