Proc macro server error: LoadLibraryExW failed for clap Parser macro

I'm trying to get the example implementation from the "clap" crate working, as below:

use clap::Parser;

/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long)]
    name: String,

    /// Number of times to greet
    #[arg(short, long, default_value_t = 1)]
    count: u8,
}

fn main() {
    let args = Args::parse();

    for _ in 0..args.count {
        println!("Hello {}!", args.name);
    }
}

However, it fails on the derive macro for Args, with the following error:

proc macro server error: Cannot create expander for C:\<path to clap_derive-d7e84929d03230d8.dll>: LoadLibraryExW failed

I am running the above on Windows 11 in Visual Studio Code, with:

  • clap v4.6.1
  • rust-analyzer v0.3.2929
  • cargo v1.96.0
  • rustup v1.29.0

I've tried the following solutions:

  • cargo update
  • cargo clean
  • Switching rust-analyzer to pre-release
  • Changing the toolchain from stable-x86_64-pc-windows-gnu to stable-x86_64-pc-windows-msvc (and vice versa)
  • In settings.json, changing rust-analyzer.rustc.source to discover

None of the above works.

could it be related to "LoadLibraryExW failed" when loading proc macros with too many -L search paths · Issue #110889 · rust-lang/rust ?

(It might not be what you want to hear, but coding rust in a devcontainer running on WSL is a really good experience and may be worth a thought. I can give tips if you're interested as this is my preferred approach)

First, you didn't specify what has failed. Due to the attempts described I guess it's rust-analyzer (and not building).

This looks like some malformed build artifact. Can you try uninstalling and reinstalling Rust, perhaps including MSVC build tools?

If nothing works, we'll need more details. The only way to get them is building the proc macro server from source, and unfortunately it's pretty hairy.

You will need to change this line:

Into:

        let lib = unsafe { load_library(path) }.map_err(|e| format!("{e:?}")).map_err(invalid_data_err)?;

After making this change, you need to build the proc-macro server. Run cargo build proc-macro-srv-cli --release --features sysroot-abi in the repo root. It will only work with nightly Rust, or if you set RUSTC_BOOTSTRAP=1, and the version of rustc must match exactly the version you use to build your project.

Then, inside your project, change the setting rust-analyzer.procMacro.server to point to the newly built executable. If everything is alright, the error message should now contain more details about why LoadLibraryExW has failed.

PS C:\<path to rust-analyzer>> cargo build proc-macro-srv-cli --release --features sysroot-abi
error: unexpected argument 'proc-macro-srv-cli' found

Usage: cargo.exe build [OPTIONS]

For more information, try '--help'.

I've set RUSTC_BOOTSTRAP=1 and the rustc versions match

Ah sorry, it needs to be

cargo build -p proc-macro-srv-cli --release --features sysroot-abi
PS C:\<path to rust-analyzer>> cargo build -p proc-macro-srv-cli --release --features sysroot-abi

error: the package 'proc-macro-srv-cli' does not contain this feature: sysroot-abi

Where did you download rust-analyzer sources from? It definitely does contain this feature.

I cloned it from the source repository