Skip to content

Commit

Permalink
use rust-analyzer binary from rustup
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Dec 6, 2023
1 parent fa8d257 commit 16dfaf0
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions bin/hermit.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down
2 changes: 1 addition & 1 deletion crates/infra/cli/src/commands/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
26 changes: 13 additions & 13 deletions crates/infra/cli/src/commands/setup/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@ 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.
// So let's install the '$RUST_NIGHTLY_VERSION' toolchain along with the 'rustfmt' component.

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:

Expand All @@ -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<Item = impl Into<String>>,
) -> Result<()> {
Command::new("rustup")
.args(["component", "add"])
.property("--toolchain", toolchain)
.arg(component)
.args(components)
.run()
}

Expand Down
1 change: 1 addition & 0 deletions crates/infra/utils/src/codegen/common/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn run_prettier(file_path: &Path, contents: &str) -> Result<String> {

fn run_rustfmt(contents: &str) -> Result<String> {
Command::new("rustfmt")
.arg(format!("+{}", env!("RUST_NIGHTLY_VERSION")))
.property("--emit", "stdout")
.evaluate_with_input(contents)
}
17 changes: 12 additions & 5 deletions scripts/_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions scripts/bin/rust-analyzer
Original file line number Diff line number Diff line change
@@ -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" "$@"
)

0 comments on commit 16dfaf0

Please sign in to comment.