Can I Configure Rust Analyzer & VSCode to Use a Different Target For Different Crates in My Workspce?

I have a workspace where one crate needs to build built for native, and another crate needs to be build for WASM, but it appears that I can only set one target for the whole workspace in Rust-analzyer.

I can add a .cargo/config.toml file with a custom target, and that works in the CLI when I run cargo build, but rust-analzyer still reports an error that the wasm32 intrinsics don't exist.

Does anybody know if this is supported at all or if I should open a feature request / bug report?

This is because, unfortunately, .cargo/config.toml is not a configuration file for a package. It's a configuration file that sets the behavior of running cargo commands. So, my_wasm/.cargo/config.toml applies when you run cargo commands while in the my_wasm/ directory, and not otherwise — regardless of what packages those cargo commands are operating on.

So, it will generally not get you what you want to use .cargo/config.toml for a single package in a workspace, and, in general, .cargo/config.toml is not a fully reliable way of setting any build settings (e.g. cargo --manifest-path=/path/to/some-project/Cargo.toml from an unrelated working directory will ignore it entirely, I believe).

Cargo’s unstable feature per-package-target will let you declare that the specific package should be compiled for wasm regardless of the rest of the workspace.

Outside of unstable features, the only option is not put both packages in one workspace. You can use rust-analyzer.linkedProjects to have rust-analyzer check both packages/workspaces simultaneously, and in that case, the .cargo/config.toml for each will be used.

2 Likes