From 018255efe3e456aa8d712f68a714d5c6e010d03f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 13 Nov 2019 10:27:21 +0300 Subject: Minor cleanup --- xtask/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xtask') diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 16bddfb28..c46eaa407 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -19,7 +19,7 @@ use xtask::{ }; // Latest stable, feel free to send a PR if this lags behind. -const REQUIRED_RUST_VERSION: u32 = 38; +const REQUIRED_RUST_VERSION: u32 = 39; struct InstallOpt { client: Option, -- cgit v1.2.3 From 06754b78ac462d7b9fe8bd176ff7831772c0b1c8 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 13 Nov 2019 11:19:07 -0500 Subject: Use autocfg to determine rust version --- xtask/Cargo.toml | 1 + xtask/src/main.rs | 35 ++++++++++------------------------- 2 files changed, 11 insertions(+), 25 deletions(-) (limited to 'xtask') diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 023f6a859..72b39c3b4 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -6,6 +6,7 @@ authors = ["rust-analyzer developers"] publish = false [dependencies] +autocfg = "0.1" walkdir = "2.1.3" pico-args = "0.3.0" quote = "1.0.2" diff --git a/xtask/src/main.rs b/xtask/src/main.rs index c46eaa407..576ba003a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -9,17 +9,18 @@ //! `.cargo/config`. mod help; +use autocfg; use core::fmt::Write; use core::str; use pico_args::Arguments; use std::{env, path::PathBuf}; use xtask::{ codegen::{self, Mode}, - install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, run_with_output, Cmd, Result, + install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, Cmd, Result, }; // Latest stable, feel free to send a PR if this lags behind. -const REQUIRED_RUST_VERSION: u32 = 39; +const REQUIRED_RUST_VERSION: (usize, usize) = (1, 39); struct InstallOpt { client: Option, @@ -226,20 +227,14 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { } fn install_server(opts: ServerOpt) -> Result<()> { - let mut old_rust = false; - if let Ok(output) = run_with_output("cargo --version", ".") { - if let Ok(stdout) = String::from_utf8(output.stdout) { - println!("{}", stdout); - if !check_version(&stdout, REQUIRED_RUST_VERSION) { - old_rust = true; - } - } - } + let ac = autocfg::AutoCfg::with_dir("target")?; + + let old_rust = !ac.probe_rustc_version(REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1); if old_rust { eprintln!( - "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n", - REQUIRED_RUST_VERSION + "\nWARNING: at least rust {}.{}.0 is required to compile rust-analyzer\n", + REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1 ) } @@ -251,20 +246,10 @@ fn install_server(opts: ServerOpt) -> Result<()> { if res.is_err() && old_rust { eprintln!( - "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n", - REQUIRED_RUST_VERSION + "\nWARNING: at least rust {}.{}.0 is required to compile rust-analyzer\n", + REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1 ) } res } - -fn check_version(version_output: &str, min_minor_version: u32) -> bool { - // Parse second the number out of - // cargo 1.39.0-beta (1c6ec66d5 2019-09-30) - let minor: Option = version_output.split('.').nth(1).and_then(|it| it.parse().ok()); - match minor { - None => true, - Some(minor) => minor >= min_minor_version, - } -} -- cgit v1.2.3 From 5e3c1c2b5f63e57f98a7d02f75a559d225341b1c Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Wed, 13 Nov 2019 20:44:39 +0100 Subject: Allow usage of CARGO_TARGET_DIR env var --- xtask/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'xtask') diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 576ba003a..84842b428 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -227,7 +227,8 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { } fn install_server(opts: ServerOpt) -> Result<()> { - let ac = autocfg::AutoCfg::with_dir("target")?; + let target_dir = env::var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into()); + let ac = autocfg::AutoCfg::with_dir(target_dir)?; let old_rust = !ac.probe_rustc_version(REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1); -- cgit v1.2.3 From 5075c77957e127be51e1b5271567f906abeb50c6 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Wed, 13 Nov 2019 20:51:57 +0100 Subject: Use anyhow::Result in xtask, add contexts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This builds on #2231 but was actually done before that. You see, the cause for #2231 was that I got this error message: Error: Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }) } Just switching to `anyhow::Result` got me stack traces (when setting `RUST_LIB_BACKTRACE=1`) that at least showed stack backtrace: 0: std::backtrace::Backtrace::create 1: std::backtrace::Backtrace::capture 2: anyhow::error:: for anyhow::Error>::from 3: xtask::install_server 4: xtask::install 5: xtask::main 6: std::rt::lang_start::{{closure}} 7: std::panicking::try::do_call 8: __rust_maybe_catch_panic 9: std::rt::lang_start_internal 10: std::rt::lang_start 11: main With the added contexts (not at all exhaustive), the error became Error: install server Caused by: 0: build AutoCfg with target directory 1: No such file or directory (os error 2) Since anyhow is such a small thing (no new transitive dependencies!), and in general gives you `Result>` on steroids, I think this a nice small change. The only slightly annoying thing was to replace all the `Err(format!(…))?` calls (haven't even looked at whether we can make it support wrapping strings though), but the `bail!` macro is shorter anyway :) --- xtask/Cargo.toml | 1 + xtask/src/bin/pre-commit.rs | 4 ++-- xtask/src/codegen.rs | 8 +++----- xtask/src/codegen/gen_parser_tests.rs | 4 ++-- xtask/src/lib.rs | 16 +++++++--------- xtask/src/main.rs | 25 ++++++++++++++----------- 6 files changed, 29 insertions(+), 29 deletions(-) (limited to 'xtask') diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 72b39c3b4..bba4be192 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -13,3 +13,4 @@ quote = "1.0.2" proc-macro2 = "1.0.1" ron = "0.5.1" serde = { version = "1.0.0", features = ["derive"] } +anyhow = "1.0.19" diff --git a/xtask/src/bin/pre-commit.rs b/xtask/src/bin/pre-commit.rs index cc6ccb25e..44507fb74 100644 --- a/xtask/src/bin/pre-commit.rs +++ b/xtask/src/bin/pre-commit.rs @@ -19,10 +19,10 @@ fn update_staged() -> Result<()> { .current_dir(&root) .output()?; if !output.status.success() { - Err(format!( + anyhow::bail!( "`git diff --diff-filter=MAR --name-only --cached` exited with {}", output.status - ))?; + ); } for line in String::from_utf8(output.stdout)?.lines() { run(&format!("git update-index --add {}", root.join(line).to_string_lossy()), ".")?; diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 770b55a9a..53f524f42 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -52,7 +52,7 @@ fn update(path: &Path, contents: &str, mode: Mode) -> Result<()> { _ => (), } if mode == Mode::Verify { - Err(format!("`{}` is not up-to-date", path.display()))?; + anyhow::bail!("`{}` is not up-to-date", path.display()); } eprintln!("updating {}", path.display()); fs::write(path, contents)?; @@ -101,10 +101,8 @@ fn do_extract_comment_blocks(text: &str, allow_blocks_with_empty_lins: bool) -> let is_comment = line.starts_with(prefix); if is_comment { block.push(line[prefix.len()..].to_string()); - } else { - if !block.is_empty() { - res.push(mem::replace(&mut block, Vec::new())) - } + } else if !block.is_empty() { + res.push(mem::replace(&mut block, Vec::new())); } } if !block.is_empty() { diff --git a/xtask/src/codegen/gen_parser_tests.rs b/xtask/src/codegen/gen_parser_tests.rs index d0f0f683b..2977da2fa 100644 --- a/xtask/src/codegen/gen_parser_tests.rs +++ b/xtask/src/codegen/gen_parser_tests.rs @@ -102,10 +102,10 @@ fn tests_from_dir(dir: &Path) -> Result { for test in collect_tests(&text) { if test.ok { if let Some(old_test) = res.ok.insert(test.name.clone(), test) { - return Err(format!("Duplicate test: {}", old_test.name).into()); + anyhow::bail!("Duplicate test: {}", old_test.name); } } else if let Some(old_test) = res.err.insert(test.name.clone(), test) { - return Err(format!("Duplicate test: {}", old_test.name).into()); + anyhow::bail!("Duplicate test: {}", old_test.name); } } Ok(()) diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index bae4c4650..bfee2f9c8 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -2,10 +2,10 @@ pub mod codegen; +use anyhow::Context; +pub use anyhow::Result; use std::{ - env, - error::Error, - fs, + env, fs, io::{Error as IoError, ErrorKind}, path::{Path, PathBuf}, process::{Command, Output, Stdio}, @@ -13,8 +13,6 @@ use std::{ use crate::codegen::Mode; -pub type Result = std::result::Result>; - const TOOLCHAIN: &str = "stable"; pub fn project_root() -> PathBuf { @@ -69,7 +67,7 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> { .status() { Ok(status) if status.success() => (), - _ => install_rustfmt()?, + _ => install_rustfmt().context("install rustfmt")?, }; if mode == Mode::Verify { @@ -112,7 +110,7 @@ pub fn run_clippy() -> Result<()> { .status() { Ok(status) if status.success() => (), - _ => install_clippy()?, + _ => install_clippy().context("install clippy")?, }; let allowed_lints = [ @@ -162,9 +160,9 @@ where let exec = args.next().unwrap(); let mut cmd = Command::new(exec); f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); - let output = cmd.output()?; + let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?; if !output.status.success() { - Err(format!("`{}` exited with {}", cmdline, output.status))?; + anyhow::bail!("`{}` exited with {}", cmdline, output.status); } Ok(output) } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 84842b428..7eab1c949 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -9,6 +9,7 @@ //! `.cargo/config`. mod help; +use anyhow::Context; use autocfg; use core::fmt::Write; use core::str; @@ -114,21 +115,21 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> { write!(&mut invalid_flags, "{}, ", flag)?; } let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2); - Err(format!("Invalid flags: {}", invalid_flags).into()) + anyhow::bail!("Invalid flags: {}", invalid_flags) } else { - Err(e.to_string().into()) + anyhow::bail!(e.to_string()) } } fn install(opts: InstallOpt) -> Result<()> { if cfg!(target_os = "macos") { - fix_path_for_mac()? + fix_path_for_mac().context("Fix path for mac")? } if let Some(server) = opts.server { - install_server(server)?; + install_server(server).context("install server")?; } if let Some(client) = opts.client { - install_client(client)?; + install_client(client).context("install client")?; } Ok(()) } @@ -140,7 +141,7 @@ fn fix_path_for_mac() -> Result<()> { const ROOT_DIR: &str = ""; let home_dir = match env::var("HOME") { Ok(home) => home, - Err(e) => Err(format!("Failed getting HOME from environment with error: {}.", e))?, + Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e), }; [ROOT_DIR, &home_dir] @@ -154,12 +155,12 @@ fn fix_path_for_mac() -> Result<()> { if !vscode_path.is_empty() { let vars = match env::var_os("PATH") { Some(path) => path, - None => Err("Could not get PATH variable from env.")?, + None => anyhow::bail!("Could not get PATH variable from env."), }; let mut paths = env::split_paths(&vars).collect::>(); paths.append(&mut vscode_path); - let new_paths = env::join_paths(paths)?; + let new_paths = env::join_paths(paths).context("build env PATH")?; env::set_var("PATH", &new_paths); } @@ -198,7 +199,7 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { let code_binary = match code_binary { Some(it) => it, - None => Err("Can't execute `code --version`. Perhaps it is not in $PATH?")?, + None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"), }; Cmd { @@ -219,8 +220,10 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { .run_with_output()?; if !str::from_utf8(&output.stdout)?.contains("ra-lsp") { - Err("Could not install the Visual Studio Code extension. \ - Please make sure you have at least NodeJS 10.x installed and try again.")?; + anyhow::bail!( + "Could not install the Visual Studio Code extension. \ + Please make sure you have at least NodeJS 10.x installed and try again." + ); } Ok(()) -- cgit v1.2.3 From c65f42325fa9213ad79fcf12115213a923b651df Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 14 Nov 2019 11:11:32 +0300 Subject: Revert #2230 Looks like autocfg tries to do slightly more than we need (see #2231), so let's stick with minimal home-grown solution. --- xtask/Cargo.toml | 1 - xtask/src/main.rs | 36 +++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 12 deletions(-) (limited to 'xtask') diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index bba4be192..77b9ae2b4 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -6,7 +6,6 @@ authors = ["rust-analyzer developers"] publish = false [dependencies] -autocfg = "0.1" walkdir = "2.1.3" pico-args = "0.3.0" quote = "1.0.2" diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 7eab1c949..f14e6c8ae 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -10,18 +10,17 @@ mod help; use anyhow::Context; -use autocfg; use core::fmt::Write; use core::str; use pico_args::Arguments; use std::{env, path::PathBuf}; use xtask::{ codegen::{self, Mode}, - install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, Cmd, Result, + install_format_hook, run, run_clippy, run_fuzzer, run_rustfmt, run_with_output, Cmd, Result, }; // Latest stable, feel free to send a PR if this lags behind. -const REQUIRED_RUST_VERSION: (usize, usize) = (1, 39); +const REQUIRED_RUST_VERSION: u32 = 39; struct InstallOpt { client: Option, @@ -230,15 +229,20 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> { } fn install_server(opts: ServerOpt) -> Result<()> { - let target_dir = env::var_os("CARGO_TARGET_DIR").unwrap_or_else(|| "target".into()); - let ac = autocfg::AutoCfg::with_dir(target_dir)?; - - let old_rust = !ac.probe_rustc_version(REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1); + let mut old_rust = false; + if let Ok(output) = run_with_output("cargo --version", ".") { + if let Ok(stdout) = String::from_utf8(output.stdout) { + println!("{}", stdout); + if !check_version(&stdout, REQUIRED_RUST_VERSION) { + old_rust = true; + } + } + } if old_rust { eprintln!( - "\nWARNING: at least rust {}.{}.0 is required to compile rust-analyzer\n", - REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1 + "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n", + REQUIRED_RUST_VERSION, ) } @@ -250,10 +254,20 @@ fn install_server(opts: ServerOpt) -> Result<()> { if res.is_err() && old_rust { eprintln!( - "\nWARNING: at least rust {}.{}.0 is required to compile rust-analyzer\n", - REQUIRED_RUST_VERSION.0, REQUIRED_RUST_VERSION.1 + "\nWARNING: at least rust 1.{}.0 is required to compile rust-analyzer\n", + REQUIRED_RUST_VERSION, ) } res } + +fn check_version(version_output: &str, min_minor_version: u32) -> bool { + // Parse second the number out of + // cargo 1.39.0-beta (1c6ec66d5 2019-09-30) + let minor: Option = version_output.split('.').nth(1).and_then(|it| it.parse().ok()); + match minor { + None => true, + Some(minor) => minor >= min_minor_version, + } +} -- cgit v1.2.3