Rust-analyzer no_std unresolved crate alloc

I am working on a bare-metal project. When I add extern crate alloc, rust-analyzer reports an error (unresolved extern crate). When I goto-defintion on alloc, that works and the error disappears until I restart rust-analyzer. cargo check runs without any complaints (cargo check --target x86_64-unknown-none -Zbuild-std=core,alloc -Zbuild-std-features=compiler-builtins-mem). I am unsure if this is a rust-analyzer bug or if I am just configuring it wrong.

Cargo.toml

[package]
name = "rust-analyzer-demo"
version = "0.1.0"
edition = "2024"

[[bin]]
name = "rust-analyzer-demo"
path = "src/main.rs"
test = false
doctest = false
bench = false

[dependencies]

main.rs

#![no_main]
#![no_std]

extern crate alloc;

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo<'_>) -> ! {
    loop {}
}

use core::alloc::GlobalAlloc;

struct Ga;

#[global_allocator]
static GA: Ga = Ga;

unsafe impl GlobalAlloc for Ga {
    unsafe fn alloc(&self, _layout: core::alloc::Layout) -> *mut u8 {
        todo!()
    }

    unsafe fn dealloc(&self, _ptr: *mut u8, _layout: core::alloc::Layout) {
        todo!()
    }
}

.helix/languages.toml

[language-server.rust-analyzer.config.cargo]
extraArgs = [
  "-Zbuild-std=core,alloc",
  "-Zbuild-std-features=compiler-builtins-mem",
]
target = "x86_64-unknown-none"

[language-server.rust-analyzer.config.check]
extraArgs = [
  "-Zbuild-std=core,alloc",
  "-Zbuild-std-features=compiler-builtins-mem",
]
targets = [
  "x86_64-unknown-none",
]

Can you show log messages according to Troubleshooting - rust-analyzer? (That page seems specialized for VS Code)

I have managed to get an LSP log file out of helix as follows:

RA_LOG=info hx src/main.rs --log helix-log -v
sed -n 's/^.*rust-analyzer err <- "\(.*\)"/"\1"/p' helix-log   | jq -Rr 'fromjson | sub("\n$"; "")' > ra-log-info

I have uploaded the compressed log here

I am unable to draw a conclusion from the logs :frowning:

I'd guess this is a bug and you can report it to the rust-analyzer developers and see what they know about it.

PS. I made a truncated version of the log by removing lines toward the end of the file not containing exact word match "alloc", in case it's useful. Its uncompressed size is half of the original :slight_smile:

1 Like

Thank you for your help. In preparation for opening an issue, I updated to a more recent nightly, which made the error disappear. I can't find any issue on the rust-analyzer repository related to it, but it seems to be gone now.

1 Like