Skip to content

Commit

Permalink
Determine rust channel by parsing rustc output if env vars do not exi…
Browse files Browse the repository at this point in the history
…st (#163)

* Determine rust channel by parsing rustc output

The RUSTUP_TOOLCHAIN environment variable might not always be present.
This is the case for e.g. NixOS where rust is routinely not installed via
rustup, thus not setting this env var, causing build failures.
Instead, build.rs will now run `rustc -V` and check if the output contains the
word "nightly".

* Check env vars first, fall back to rustc in $PATH

* Try to check RUSTUP_TOOLCHAIN first
  • Loading branch information
itsCryne authored Jul 19, 2024
1 parent 3d717b6 commit 4ee0b78
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions azalea/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
use std::env;
use std::process::Command;

fn main() {
let rust_toolchain = env::var("RUSTUP_TOOLCHAIN").unwrap();
if rust_toolchain.starts_with("stable") {
panic!("Azalea currently requires nightly Rust. You can use `rustup override set nightly` to set the toolchain for this directory.");
match env::var("RUSTUP_TOOLCHAIN") {
Ok(rust_toolchain) if !rust_toolchain.starts_with("nightly") => { // stable & beta
panic!("Azalea currently requires nightly Rust. You can use `rustup override set nightly` to set the toolchain for this directory.");
}
Ok(_) => return, // nightly

Check warning on line 9 in azalea/build.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> azalea/build.rs:9:18 | 9 | Ok(_) => return, // nightly | ^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: replace `return` with a unit value | 9 | Ok(_) => (), // nightly | ~~
Err(_) => { // probably not installed via rustup, run rustc and parse its output
let rustc_command = env::var("RUSTC")
.or_else(|_| env::var("CARGO_BUILD_RUSTC"))
.unwrap_or(String::from("rustc"));
let rustc_version_output = Command::new(rustc_command).arg("-V").output().unwrap();
if !rustc_version_output.status.success()
|| !String::from_utf8(rustc_version_output.stdout)
.unwrap()
.contains("nightly")
{
panic!("Azalea currently requires nightly Rust. It seems that you did not install Rust via rustup. Please check the documentation for your installation method, to find out how to use nightly Rust.");
}
}
}
}

0 comments on commit 4ee0b78

Please sign in to comment.