Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unresolved extern crate on Nixos #17560

Open
StealthyKamereon opened this issue Jul 7, 2024 · 7 comments
Open

unresolved extern crate on Nixos #17560

StealthyKamereon opened this issue Jul 7, 2024 · 7 comments
Labels
C-bug Category: bug

Comments

@StealthyKamereon
Copy link

StealthyKamereon commented Jul 7, 2024

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 cognitive-engineering-lab/rustc_plugin#26 for additional information.

@StealthyKamereon StealthyKamereon added the C-bug Category: bug label Jul 7, 2024
@StealthyKamereon
Copy link
Author

Anybody willing to help me on this ?

@Multirious
Copy link

Multirious commented Dec 16, 2024

Encountered the same problem and I found out that:

  1. I guess that rust-analyzer with rust-analyzer.rustc.source = "discover" won't find files inside the nix store so I tried to explicitly set the file location.
  2. I tried /nix/store/ydmh9f2s01bdgzz9f8y6a20mbkrchh13-rust-default-1.85.0-nightly-2024-12-16/lib/rustlib/rustc-src/rust/compiler/rustc/Cargo.toml for rust-analyzer.rustc.source
  3. rust-analyzer continue to error but this time it log out that some certain command failed.
  4. I tried running that command manually and found out that it's saying the file does not exists which I figured that rust-analzyer do not read symlink
  5. I readlinked the Cargo.toml and use that value for rust-analyzer.rustc.source and the command found the file!
  6. ...except there's no Cargo.lock and rust-analyzer is trying to write the file to the nix store ...Unfortunately a deadend I guess

@Multirious
Copy link

I found out that in the part that reads rustc_source doesn't canonicalize the path in any way. Is this intended? .canonicalize is also seems like it's explicitly not implemented too.

@Multirious
Copy link

Multirious commented Dec 16, 2024

I think we also need #10792

@Multirious
Copy link

Multirious commented Dec 16, 2024

For those looking for workaround, there is cognitive-engineering-lab/rustc_plugin#26 (comment) by @StealthyKamereon

For those who can't wait a fix, I found a dirty workaround.

Make a directory in your project and create a auxiliary nix store by copying the rust package :

# Set the RUST_PKG with the shell.nix. For example, my RUST_PKG contains the following:

export RUST_PKG=/nix/store/v0769idpz76l6b3xff8ps98bcsy5ymjb-rust-default-1.80.0-nightly-2024-05-20



nix copy --to /<project-dir>/local-store $RUST_PKG --no-check-sigs

This will take care of copying all the necessary packages.

Then simply enable writing in the local store:

chmod u+w -R /<project-dir>/local-store

You then need to tell your lsp where your sysroot is. For example, I have these settings for vscode:

{

    "rust-analyzer.server.path": "/<project-dir>/local-store/nix/store/v0769idpz76l6b3xff8ps98bcsy5ymjb-rust-default-1.80.0-nightly-2024-05-20/bin/rust-analyzer",

    "rust-analyzer.cargo.sysroot": "/<project-dir>/local-store/nix/store/v0769idpz76l6b3xff8ps98bcsy5ymjb-rust-default-1.80.0-nightly-2024-05-20/",

    "rust-analyzer.rustc.source": "discover"

}

Just to make my life easier, I've made this script:

#!/usr/bin/env bash

# local_nix directory is <current dir>/.local-nix
local_nix="$(dirname "$(readlink -f "$0")")/.local-nix"

# remake if local nix directory do not exists or the current copy do not match $RUST_PKG
if [ ! -d "$local_nix" ]; then
  should_remake="true"
elif [ "$(cat "$local_nix/origin_rust_pkg")" != "$RUST_PKG" ]; then
  should_remake="true"

  # Clear current copy
  echo "Clearing $local_nix"
  rm -rf "$local_nix"
else
  should_remake="false"
fi

if [ "$should_remake" = "true" ]; then
  echo "Making $local_nix"

  # copy nix
  nix copy --to "$local_nix" $RUST_PKG --no-check-sigs

  # Make writable
  chmod u+w -R "$local_nix"

  # Save original path to file
  echo "$RUST_PKG" > "$local_nix/origin_rust_pkg"
fi

@StealthyKamereon
Copy link
Author

Yes, thanks for reposting the workaround here :)
I literally spent months try to find it so I guess I was a bit lazy and just posted on one of the three issues. Sorry for that.

For anyone coming here, see this workaround.

@Multirious
Copy link

Thanks for making the workaround anyways! I have no idea know how long I'm going to take without the works you've done. Mega time saver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants