diff --git a/integrations/rust-project/src/cli/develop.rs b/integrations/rust-project/src/cli/develop.rs index 4e4453142c5aa..a03724b37c6e2 100644 --- a/integrations/rust-project/src/cli/develop.rs +++ b/integrations/rust-project/src/cli/develop.rs @@ -62,6 +62,7 @@ impl Develop { stdout, prefer_rustup_managed_toolchain, sysroot, + sysroot_src, pretty, mode, check_cycles, @@ -80,7 +81,21 @@ impl Develop { let sysroot = if prefer_rustup_managed_toolchain { SysrootConfig::Rustup } else if let Some(sysroot) = sysroot { - SysrootConfig::Sysroot(sysroot) + // sysroot_src priority: + // 1. --sysroot-src flag + // 2. RUST_SRC_PATH env var + // 3. --sysroot flag + let sysroot_src = sysroot_src + .or_else(|| { + std::env::var("RUST_SRC_PATH") + .ok() + .map(|s| PathBuf::from(&s)) + }) + .unwrap_or(sysroot.clone()); + SysrootConfig::Sysroot { + sysroot, + sysroot_src, + } } else { SysrootConfig::BuckConfig }; @@ -126,13 +141,20 @@ impl Develop { let sysroot = match sysroot_mode { crate::SysrootMode::BuckConfig => SysrootConfig::BuckConfig, crate::SysrootMode::Rustc => SysrootConfig::Rustup, - crate::SysrootMode::FullPath(path) => SysrootConfig::Sysroot(path), + crate::SysrootMode::FullPath(path) => SysrootConfig::Sysroot { + sysroot: path.clone(), + sysroot_src: path, + }, crate::SysrootMode::Command(cmd_args) => { let cmd = cmd_args[0].clone(); let args = cmd_args[1..].to_vec(); let output = std::process::Command::new(cmd).args(args).output().unwrap(); let path = String::from_utf8(output.stdout).unwrap(); - SysrootConfig::Sysroot(PathBuf::from(path.trim())) + let path = PathBuf::from(path.trim()); + SysrootConfig::Sysroot { + sysroot: path.clone(), + sysroot_src: path, + } } }; @@ -279,9 +301,12 @@ impl Develop { info!(kind = "progress", "finding std source code"); let sysroot = match &sysroot { - SysrootConfig::Sysroot(path) => Sysroot { - sysroot: safe_canonicalize(&expand_tilde(path)?), - sysroot_src: None, + SysrootConfig::Sysroot { + sysroot, + sysroot_src, + } => Sysroot { + sysroot: safe_canonicalize(&expand_tilde(sysroot)?), + sysroot_src: Some(safe_canonicalize(&expand_tilde(&sysroot_src)?)), sysroot_project: None, }, SysrootConfig::BuckConfig => { diff --git a/integrations/rust-project/src/main.rs b/integrations/rust-project/src/main.rs index cad36529a3337..6a7ed1e62125a 100644 --- a/integrations/rust-project/src/main.rs +++ b/integrations/rust-project/src/main.rs @@ -89,11 +89,18 @@ enum Command { #[clap(long, conflicts_with = "sysroot")] prefer_rustup_managed_toolchain: bool, - /// The directory containing the Rust source code, including std. + /// The directory containing the Rust standard library and the rust-analyzer proc macro + /// server. /// Default value is determined based on platform. #[clap(short = 's', long)] sysroot: Option, + /// The directory containing the Rust standard library source code. + /// Defaults to the value of the `RUST_SRC_PATH` environment variable, if set, or the + /// `--sysroot` flag otherwise. + #[clap(long)] + sysroot_src: Option, + /// Pretty-print generated `rust-project.json` file. #[clap(short, long)] pretty: bool, diff --git a/integrations/rust-project/src/sysroot.rs b/integrations/rust-project/src/sysroot.rs index 359567c027470..a8c740ca754f3 100644 --- a/integrations/rust-project/src/sysroot.rs +++ b/integrations/rust-project/src/sysroot.rs @@ -25,7 +25,10 @@ use crate::target::Target; #[derive(Debug)] pub(crate) enum SysrootConfig { - Sysroot(PathBuf), + Sysroot { + sysroot: PathBuf, + sysroot_src: PathBuf, + }, BuckConfig, Rustup, }