1 //! Installs rust-analyzer language server and/or editor plugin.
3 use std::{env, path::PathBuf, str};
5 use anyhow::{bail, format_err, Context, Result};
6 use xshell::{cmd, Shell};
11 pub(crate) fn run(self, sh: &Shell) -> Result<()> {
12 if cfg!(target_os = "macos") {
13 fix_path_for_mac(sh).context("Fix path for mac")?;
15 if let Some(server) = self.server() {
16 install_server(sh, server).context("install server")?;
18 if let Some(client) = self.client() {
19 install_client(sh, client).context("install client")?;
26 pub(crate) struct ClientOpt {
27 pub(crate) code_bin: Option<String>,
30 const VS_CODES: &[&str] = &["code", "code-exploration", "code-insiders", "codium", "code-oss"];
32 pub(crate) struct ServerOpt {
33 pub(crate) malloc: Malloc,
36 pub(crate) enum Malloc {
42 fn fix_path_for_mac(sh: &Shell) -> Result<()> {
43 let mut vscode_path: Vec<PathBuf> = {
44 const COMMON_APP_PATH: &str =
45 r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
46 const ROOT_DIR: &str = "";
47 let home_dir = sh.var("HOME").map_err(|err| {
48 format_err!("Failed getting HOME from environment with error: {}.", err)
53 .map(|dir| dir.to_string() + COMMON_APP_PATH)
55 .filter(|path| path.exists())
59 if !vscode_path.is_empty() {
60 let vars = sh.var_os("PATH").context("Could not get PATH variable from env.")?;
62 let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
63 paths.append(&mut vscode_path);
64 let new_paths = env::join_paths(paths).context("build env PATH")?;
65 sh.set_var("PATH", &new_paths);
71 fn install_client(sh: &Shell, client_opt: ClientOpt) -> Result<()> {
72 let _dir = sh.push_dir("./editors/code");
76 cmd!(sh, "npm --version").run().context("`npm` is required to build the VS Code plugin")?;
77 cmd!(sh, "npm ci").run()?;
79 cmd!(sh, "npm run package --scripts-prepend-node-path").run()?;
81 cmd!(sh, "cmd.exe /c npm --version")
83 .context("`npm` is required to build the VS Code plugin")?;
84 cmd!(sh, "cmd.exe /c npm ci").run()?;
86 cmd!(sh, "cmd.exe /c npm run package").run()?;
89 // Find the appropriate VS Code binary.
90 let lifetime_extender;
91 let candidates: &[&str] = match client_opt.code_bin.as_deref() {
93 lifetime_extender = [it];
94 &lifetime_extender[..]
103 cmd!(sh, "{bin} --version").read().is_ok()
105 cmd!(sh, "cmd.exe /c {bin}.cmd --version").read().is_ok()
109 format_err!("Can't execute `{} --version`. Perhaps it is not in $PATH?", candidates[0])
113 let installed_extensions = if cfg!(unix) {
114 cmd!(sh, "{code} --install-extension rust-analyzer.vsix --force").run()?;
115 cmd!(sh, "{code} --list-extensions").read()?
117 cmd!(sh, "cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?;
118 cmd!(sh, "cmd.exe /c {code}.cmd --list-extensions").read()?
121 if !installed_extensions.contains("rust-analyzer") {
123 "Could not install the Visual Studio Code extension. \
124 Please make sure you have at least NodeJS 12.x together with the latest version of VS Code installed and try again. \
125 Note that installing via xtask install does not work for VS Code Remote, instead you’ll need to install the .vsix manually."
132 fn install_server(sh: &Shell, opts: ServerOpt) -> Result<()> {
133 let features = match opts.malloc {
134 Malloc::System => &[][..],
135 Malloc::Mimalloc => &["--features", "mimalloc"],
136 Malloc::Jemalloc => &["--features", "jemalloc"],
139 let cmd = cmd!(sh, "cargo install --path crates/rust-analyzer --locked --force --features force-always-assert {features...}");