diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index 4a1b2ff9a44..0c33396857d 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -5,6 +5,7 @@ use tools::{ generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt, Overwrite, Result, run_fuzzer, }; +use std::{path::{PathBuf}, env}; fn main() -> Result<()> { let matches = App::new("tasks") @@ -17,7 +18,12 @@ fn main() -> Result<()> { .subcommand(SubCommand::with_name("fuzz-tests")) .get_matches(); match matches.subcommand_name().expect("Subcommand must be specified") { - "install-code" => install_code_extension()?, + "install-code" => { + if cfg!(target_os = "macos") { + fix_path_for_mac()?; + } + install_code_extension()?; + } "gen-tests" => gen_tests(Overwrite)?, "gen-syntax" => generate(Overwrite)?, "format" => run_rustfmt(Overwrite)?, @@ -63,3 +69,36 @@ fn verify_installed_extensions() -> Result<()> { } Ok(()) } + +fn fix_path_for_mac() -> Result<()> { + let mut vscode_path: Vec = { + const COMMON_APP_PATH: &str = + r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin"; + const ROOT_DIR: &str = ""; + let home_dir = match env::var("HOME") { + Ok(home) => home, + Err(e) => bail!("Failed getting HOME from environment with error: {}.", e), + }; + + [ROOT_DIR, &home_dir] + .iter() + .map(|dir| String::from(dir.clone()) + COMMON_APP_PATH) + .map(PathBuf::from) + .filter(|path| path.exists()) + .collect() + }; + + if !vscode_path.is_empty() { + let vars = match env::var_os("PATH") { + Some(path) => path, + None => bail!("Could not get PATH variable from env."), + }; + + let mut paths = env::split_paths(&vars).collect::>(); + paths.append(&mut vscode_path); + let new_paths = env::join_paths(paths)?; + env::set_var("PATH", &new_paths); + } + + Ok(()) +}