rust-lang / rust-lang/rust-analyzer

`unresolved extern crate` on Nixos

Open
#17,560 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: rust-analyzer 1.80.0-nightly (d84b903 2024-05-19)

rustc version: rustc 1.80.0-nightly (d84b90375 2024-05-19)

editor or extension: NeoVim configured with lspconfig

relevant settings:

rust_analyzer = {
  cargo = {
    features = "all",
  },
  rustc = { source = "discover" },
},

repository link (if public, optional): rustc_plugin example

steps to reproduce:
Clone the example from https://github.com/cognitive-engineering-lab/rustc_plugin/ and change the crate:

git clone https://github.com/cognitive-engineering-lab/rustc_plugin/
cd example/print-all-items
sed -i 's;rustc_plugin = { path = "../.." };rustc_plugin = "=0.10.0-nightly-2024-05-20"' Cargo.toml

Create a shell.nix with the following content:

{ nixpkgs ? import <nixpkgs> { }}:

let
  rustOverlay = builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz";
  pinnedPkgs = nixpkgs.fetchFromGitHub {
    owner  = "NixOS";
    repo   = "nixpkgs";
    rev    = "b3fcfcfabd01b947a1e4f36622bbffa3985bdac6";
    sha256 = "0va68ddkrqb6j349rsbkjhdq7m243kg9njcqhnx9p2sbrcl2g5l8";
  };
  pkgs = import pinnedPkgs {
    overlays = [ (import rustOverlay) ];
  };
  rust_pkg = pkgs.rust-bin.nightly."2024-05-20".default.override {
    extensions = ["rust-src" "rustc" "rustc-dev" "llvm-tools" "rust-analyzer" "cargo"];
  };
in
  pkgs.mkShell {
    buildInputs = with pkgs; [
      (
        rust_pkg
      )
      openssl
      pkg-config
      eza
      fd
    ];

    RUST_BACKTRACE = 1;
    RUST_SRC_PATH = "${rust_pkg}/lib/rustlib/src/rust/library";
  }

Enter the shell:

nix-shell

Launch RA:

rust-analyzer diagnostics .

You will have the following errors:

processing crate: print_all_items_driver, module: /print-all-items/src/bin/print-all-items-driver.rs
processing crate: cargo_print_all_items, module: /print-all-items/src/bin/cargo-print-all-items.rs
processing crate: print_all_items, module: /print-all-items/src/lib.rs
Diagnostic { code: RustcHardError("unresolved-extern-crate"), message: "unresolved extern crate", range: FileRange { file_id: FileId(11), range: 98..124 }, severity: Error, unused: false, experimental: false, fixes: None, main_node: Some(InFileWrapper { file_id: FileId(11), value: SyntaxNodePtr { kind: EXTERN_CRATE, range: 98..124 } }) }
Diagnostic { code: RustcHardError("unresolved-extern-crate"), message: "unresolved extern crate", range: FileRange { file_id: FileId(11), range: 125..154 }, severity: Error, unused: false, experimental: false, fixes: None, main_node: Some(InFileWrapper { file_id: FileId(11), value: SyntaxNodePtr { kind: EXTERN_CRATE, range: 125..154 } }) }
Diagnostic { code: RustcHardError("unresolved-extern-crate"), message: "unresolved extern crate", range: FileRange { file_id: FileId(11), range: 155..181 }, severity: Error, unused: false, experimental: false, fixes: None, main_node: Some(InFileWrapper { file_id: FileId(11), value: SyntaxNodePtr { kind: EXTERN_CRATE, range: 155..181 } }) }
Diagnostic { code: RustcHardError("unresolved-extern-crate"), message: "unresolved extern crate", range: FileRange { file_id: FileId(11), range: 182..209 }, severity: Error, unused: false, experimental: false, fixes: None, main_node: Some(InFileWrapper { file_id: FileId(11), value: SyntaxNodePtr { kind: EXTERN_CRATE, range: 182..209 } }) }

diagnostic scan complete

Error: diagnostic error detected

Stack backtrace:
   0: <anyhow::Error>::msg::<&str>
   1: std::sys_common::backtrace::__rust_begin_short_backtrace::<<stdx::thread::Builder>::spawn<<rust_analyzer::cli::flags::Diagnostics>::run::{closure#0}, core::result::Result<(), anyhow::Error>>::{closure#0}, core::result::Result<(), anyhow::Error>>
   2: <<std::thread::Builder>::spawn_unchecked_<<stdx::thread::Builder>::spawn<<rust_analyzer::cli::flags::Diagnostics>::run::{closure#0}, core::result::Result<(), anyhow::Error>>::{closure#0}, core::result::Result<(), anyhow::Error>>::{closure#2} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
   3: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/d84b9037541f45dc2c52a41d723265af211c0497/library/alloc/src/boxed.rs:2022:9
   4: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/d84b9037541f45dc2c52a41d723265af211c0497/library/alloc/src/boxed.rs:2022:9
   5: std::sys::pal::unix::thread::Thread::new::thread_start
             at /rustc/d84b9037541f45dc2c52a41d723265af211c0497/library/std/src/sys/pal/unix/thread.rs:108:17
   6: start_thread
   7: __GI___clone3

See https://github.com/cognitive-engineering-lab/rustc_plugin/issues/26 for additional information.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report in the rustc_plugin example, using shell.nix and the print-all-items Cargo.toml and src/bin files. Start with the rust-analyzer diagnostics entry point and compare its handling of the nightly toolchain and extern crates on NixOS. Done means the example no longer reports false unresolved-extern-crate diagnostics and the diagnostics scan completes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.