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, pushd};
10 // Latest stable, feel free to send a PR if this lags behind.
11 const REQUIRED_RUST_VERSION: u32 = 51;
14 pub(crate) fn run(self) -> Result<()> {
15 if cfg!(target_os = "macos") {
16 fix_path_for_mac().context("Fix path for mac")?
18 if let Some(server) = self.server() {
19 install_server(server).context("install server")?;
21 if let Some(client) = self.client() {
22 install_client(client).context("install client")?;
29 pub(crate) struct ClientOpt {
30 pub(crate) code_bin: Option<String>,
33 const VS_CODES: &[&str] = &["code", "code-exploration", "code-insiders", "codium", "code-oss"];
35 pub(crate) struct ServerOpt {
36 pub(crate) malloc: Malloc,
39 pub(crate) enum Malloc {
45 fn fix_path_for_mac() -> Result<()> {
46 let mut vscode_path: Vec<PathBuf> = {
47 const COMMON_APP_PATH: &str =
48 r"/Applications/Visual Studio Code.app/Contents/Resources/app/bin";
49 const ROOT_DIR: &str = "";
50 let home_dir = match env::var("HOME") {
52 Err(e) => bail!("Failed getting HOME from environment with error: {}.", e),
57 .map(|dir| String::from(*dir) + COMMON_APP_PATH)
59 .filter(|path| path.exists())
63 if !vscode_path.is_empty() {
64 let vars = match env::var_os("PATH") {
66 None => bail!("Could not get PATH variable from env."),
69 let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
70 paths.append(&mut vscode_path);
71 let new_paths = env::join_paths(paths).context("build env PATH")?;
72 env::set_var("PATH", &new_paths);
78 fn install_client(client_opt: ClientOpt) -> Result<()> {
79 let _dir = pushd("./editors/code");
83 cmd!("npm --version").run().context("`npm` is required to build the VS Code plugin")?;
84 cmd!("npm ci").run()?;
86 cmd!("npm run package --scripts-prepend-node-path").run()?;
88 cmd!("cmd.exe /c npm --version")
90 .context("`npm` is required to build the VS Code plugin")?;
91 cmd!("cmd.exe /c npm ci").run()?;
93 cmd!("cmd.exe /c npm run package").run()?;
96 // Find the appropriate VS Code binary.
97 let lifetime_extender;
98 let candidates: &[&str] = match client_opt.code_bin.as_deref() {
100 lifetime_extender = [it];
101 &lifetime_extender[..]
105 let code = candidates
110 cmd!("{bin} --version").read().is_ok()
112 cmd!("cmd.exe /c {bin}.cmd --version").read().is_ok()
116 format_err!("Can't execute `{} --version`. Perhaps it is not in $PATH?", candidates[0])
120 let installed_extensions = if cfg!(unix) {
121 cmd!("{code} --install-extension rust-analyzer.vsix --force").run()?;
122 cmd!("{code} --list-extensions").read()?
124 cmd!("cmd.exe /c {code}.cmd --install-extension rust-analyzer.vsix --force").run()?;
125 cmd!("cmd.exe /c {code}.cmd --list-extensions").read()?
128 if !installed_extensions.contains("rust-analyzer") {
130 "Could not install the Visual Studio Code extension. \
131 Please make sure you have at least NodeJS 12.x together with the latest version of VS Code installed and try again. \
132 Note that installing via xtask install does not work for VS Code Remote, instead you’ll need to install the .vsix manually."
139 fn install_server(opts: ServerOpt) -> Result<()> {
140 let mut old_rust = false;
141 if let Ok(stdout) = cmd!("cargo --version").read() {
142 if !check_version(&stdout, REQUIRED_RUST_VERSION) {
149 "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
150 REQUIRED_RUST_VERSION,
153 let features = match opts.malloc {
154 Malloc::System => &[][..],
155 Malloc::Mimalloc => &["--features", "mimalloc"],
156 Malloc::Jemalloc => &["--features", "jemalloc"],
159 let cmd = cmd!("cargo install --path crates/rust-analyzer --locked --force --features force-always-assert {features...}");
162 if res.is_err() && old_rust {
164 "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n",
165 REQUIRED_RUST_VERSION,
173 fn check_version(version_output: &str, min_minor_version: u32) -> bool {
174 // Parse second the number out of
175 // cargo 1.39.0-beta (1c6ec66d5 2019-09-30)
176 let minor: Option<u32> = version_output.split('.').nth(1).and_then(|it| it.parse().ok());
179 Some(minor) => minor >= min_minor_version,