From 16dfaf0f3e9a1a1b40f44596b9dbcdc6034c371f Mon Sep 17 00:00:00 2001 From: OmarTawfik <15987992+OmarTawfik@users.noreply.github.com> Date: Wed, 6 Dec 2023 01:53:07 -0800 Subject: [PATCH] use `rust-analyzer` binary from `rustup` --- .vscode/settings.json | 5 +++- Cargo.toml | 2 +- bin/hermit.hcl | 4 +-- crates/infra/cli/src/commands/lint/mod.rs | 2 +- .../infra/cli/src/commands/setup/cargo/mod.rs | 26 +++++++++---------- .../utils/src/codegen/common/formatting.rs | 1 + scripts/_common.sh | 17 ++++++++---- scripts/bin/rust-analyzer | 15 +++++++++++ 8 files changed, 49 insertions(+), 23 deletions(-) create mode 100755 scripts/bin/rust-analyzer diff --git a/.vscode/settings.json b/.vscode/settings.json index cd6b17cad5..f2f66de70c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,7 +9,10 @@ "rust-analyzer.check.allTargets": true, "rust-analyzer.check.command": "clippy", "rust-analyzer.check.features": "all", - "rust-analyzer.rustfmt.extraArgs": ["+nightly"], + "rust-analyzer.rustfmt.extraArgs": [ + "+nightly-2023-12-01" // Keep in sync with other "RUST_NIGHTLY_VERSION" references + ], + "rust-analyzer.server.path": "${workspaceFolder}/scripts/bin/rust-analyzer", "search.exclude": { // Packages and Dependencies "**/.hermit/": true, diff --git a/Cargo.toml b/Cargo.toml index c28cc77878..3a946497c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace.package] version = "0.11.0" -rust-version = "1.72.0" # Keep this version in sync with "$RUST_STABLE_VERSION" in "$REPO_ROOT/bin/hermit.hcl" +rust-version = "1.72.0" # Keep in sync with other "RUST_STABLE_VERSION" references edition = "2021" publish = false diff --git a/bin/hermit.hcl b/bin/hermit.hcl index 95e0859e5c..de3c2d218e 100644 --- a/bin/hermit.hcl +++ b/bin/hermit.hcl @@ -9,8 +9,8 @@ env = { // Rust: "RUST_BACKTRACE": "FULL", - "RUST_STABLE_VERSION": "1.72.0", // Keep this version in sync with "rust-version" in "$REPO_ROOT/Cargo.toml" - "RUST_NIGHTLY_VERSION": "nightly-2023-12-01", + "RUST_STABLE_VERSION": "1.72.0", // Keep in sync with other "RUST_STABLE_VERSION" references + "RUST_NIGHTLY_VERSION": "nightly-2023-12-01", // Keep in sync with other "RUST_NIGHTLY_VERSION" references "RUSTC_WRAPPER": "${HERMIT_ENV}/bin/sccache", "SCCACHE_DIR": "${HERMIT_ENV}/.hermit/sccache", diff --git a/crates/infra/cli/src/commands/lint/mod.rs b/crates/infra/cli/src/commands/lint/mod.rs index 771151c121..1d11c52395 100644 --- a/crates/infra/cli/src/commands/lint/mod.rs +++ b/crates/infra/cli/src/commands/lint/mod.rs @@ -108,7 +108,7 @@ fn run_markdown_lint() -> Result<()> { fn run_rustfmt() -> Result<()> { let mut command = Command::new("cargo-fmt") - .flag("+nightly") + .arg(format!("+{}", env!("RUST_NIGHTLY_VERSION"))) .flag("--all") .flag("--verbose"); diff --git a/crates/infra/cli/src/commands/setup/cargo/mod.rs b/crates/infra/cli/src/commands/setup/cargo/mod.rs index f3e4aca5fa..92f18d0cc5 100644 --- a/crates/infra/cli/src/commands/setup/cargo/mod.rs +++ b/crates/infra/cli/src/commands/setup/cargo/mod.rs @@ -10,19 +10,16 @@ pub fn setup_cargo() -> Result<()> { // - 'rust-std' // - 'rustc' // - // Which are enough to build, test, and run source code. - // But we need these additional optional components: - // - // - 'clippy' for linting - // - 'rust-docs' and 'rust-src' for local development - // - // So let's install these here: + // Which are enough to run infra scripts. + // But we need these additional optional components for local development: - rustup_add_component(env!("RUST_STABLE_VERSION"), "clippy")?; + rustup_add_components(env!("RUST_STABLE_VERSION"), ["clippy"])?; if !GitHub::is_running_in_ci() { - rustup_add_component(env!("RUST_STABLE_VERSION"), "rust-docs")?; - rustup_add_component(env!("RUST_STABLE_VERSION"), "rust-src")?; + rustup_add_components( + env!("RUST_STABLE_VERSION"), + ["rust-analyzer", "rust-docs", "rust-src"], + )?; } // Additionally, we also need 'rustfmt nightly', as we use experimental options. @@ -30,7 +27,7 @@ pub fn setup_cargo() -> Result<()> { rustup_install_toolchain(env!("RUST_NIGHTLY_VERSION"))?; - rustup_add_component(env!("RUST_NIGHTLY_VERSION"), "rustfmt")?; + rustup_add_components(env!("RUST_NIGHTLY_VERSION"), ["rustfmt"])?; // Make sure we have the latest dependencies: @@ -48,11 +45,14 @@ fn rustup_install_toolchain(toolchain: &str) -> Result<()> { .run() } -fn rustup_add_component(toolchain: &str, component: &str) -> Result<()> { +fn rustup_add_components( + toolchain: &str, + components: impl IntoIterator>, +) -> Result<()> { Command::new("rustup") .args(["component", "add"]) .property("--toolchain", toolchain) - .arg(component) + .args(components) .run() } diff --git a/crates/infra/utils/src/codegen/common/formatting.rs b/crates/infra/utils/src/codegen/common/formatting.rs index 5046b7604d..aeecc6a6a0 100644 --- a/crates/infra/utils/src/codegen/common/formatting.rs +++ b/crates/infra/utils/src/codegen/common/formatting.rs @@ -104,6 +104,7 @@ fn run_prettier(file_path: &Path, contents: &str) -> Result { fn run_rustfmt(contents: &str) -> Result { Command::new("rustfmt") + .arg(format!("+{}", env!("RUST_NIGHTLY_VERSION"))) .property("--emit", "stdout") .evaluate_with_input(contents) } diff --git a/scripts/_common.sh b/scripts/_common.sh index 38e8eb442d..f477304ccb 100755 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -35,8 +35,15 @@ set -euo pipefail # $REPO_ROOT/crates/infra/cli/src/commands/setup/cargo/mod.rs # -{ - rustup install --no-self-update --profile "minimal" "${RUST_STABLE_VERSION:?}" - - rustup default "${RUST_STABLE_VERSION:?}" -} +if ! output=$( + rustup install --no-self-update --profile "minimal" "${RUST_STABLE_VERSION:?}" \ + && rustup default "${RUST_STABLE_VERSION:?}" \ + 2>&1 +); then + # Only print the output if the command failed: + + echo "Running 'rustup' failed:" + echo >&2 "${output}" + + exit 1 +fi diff --git a/scripts/bin/rust-analyzer b/scripts/bin/rust-analyzer new file mode 100755 index 0000000000..f5a62424d4 --- /dev/null +++ b/scripts/bin/rust-analyzer @@ -0,0 +1,15 @@ +#!/bin/bash +set -euo pipefail + +source "$(dirname "${BASH_SOURCE[0]}")/../_common.sh" + +( + # Running "rustup default" will print: "TOOLCHAIN (default)" + # and we want to extract the "TOOLCHAIN" name only. + # This splits by spaces and gets the first part: + + rustup_default="$(rustup default)" + toolchain="${rustup_default%% *}" + + "${REPO_ROOT:?}/.hermit/rustup/toolchains/${toolchain}/bin/rust-analyzer" "$@" +)