diff --git a/Cargo.lock b/Cargo.lock index 6fb46ca3..91b743ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,6 +833,8 @@ dependencies = [ "cfg-if", "guess_host_triple", "tokio", + "tracing", + "tracing-subscriber", "windows-dll", "windows-sys", ] diff --git a/crates/bin/src/logging.rs b/crates/bin/src/logging.rs index 267fb8d6..320f266a 100644 --- a/crates/bin/src/logging.rs +++ b/crates/bin/src/logging.rs @@ -202,6 +202,7 @@ pub fn logging(log_level: LevelFilter, json_output: bool) { "binstalk_registry", "cargo_binstall", "cargo_toml_workspace", + "detect_targets", "simple_git", ]); diff --git a/crates/binstalk/Cargo.toml b/crates/binstalk/Cargo.toml index affb6196..8f40bf4f 100644 --- a/crates/binstalk/Cargo.toml +++ b/crates/binstalk/Cargo.toml @@ -18,7 +18,7 @@ binstalk-types = { version = "0.5.0", path = "../binstalk-types" } cargo-toml-workspace = { version = "1.0.0", path = "../cargo-toml-workspace" } command-group = { version = "2.1.0", features = ["with-tokio"] } compact_str = { version = "0.7.0", features = ["serde"] } -detect-targets = { version = "0.1.11", path = "../detect-targets" } +detect-targets = { version = "0.1.11", path = "../detect-targets", features = ["tracing"] } either = "1.8.1" itertools = "0.11.0" jobslot = { version = "0.2.11", features = ["tokio"] } diff --git a/crates/detect-targets/Cargo.toml b/crates/detect-targets/Cargo.toml index 9e3613ba..c3528b64 100644 --- a/crates/detect-targets/Cargo.toml +++ b/crates/detect-targets/Cargo.toml @@ -11,9 +11,15 @@ license = "Apache-2.0 OR MIT" [dependencies] tokio = { version = "1.28.2", features = ["rt", "process", "sync"], default-features = false } +tracing = { version = "0.1.37", optional = true } +tracing-subscriber = { version = "0.3.17", features = ["fmt"], default-features = false, optional = true } cfg-if = "1.0.0" guess_host_triple = "0.1.3" +[features] +tracing = ["dep:tracing"] +cli-logging = ["tracing", "dep:tracing-subscriber"] + [target.'cfg(target_os = "windows")'.dependencies] windows-sys = { version = "0.48.0", features = ["Win32_System_Threading", "Win32_System_SystemInformation", "Win32_Foundation"] } windows-dll = { version = "0.4.1", features = ["windows"], default-features = false } diff --git a/crates/detect-targets/src/detect.rs b/crates/detect-targets/src/detect.rs index d9fb9df6..15d8a125 100644 --- a/crates/detect-targets/src/detect.rs +++ b/crates/detect-targets/src/detect.rs @@ -7,6 +7,8 @@ use std::{ use cfg_if::cfg_if; use tokio::process::Command; +#[cfg(feature = "tracing")] +use tracing::debug; cfg_if! { if #[cfg(target_os = "linux")] { @@ -32,10 +34,14 @@ cfg_if! { /// Check [this issue](https://github.com/ryankurte/cargo-binstall/issues/155) /// for more information. pub async fn detect_targets() -> Vec { - let target = get_target_from_rustc().await.unwrap_or_else(|| { - guess_host_triple::guess_host_triple() - .unwrap_or(crate::TARGET) - .to_string() + let target = get_target_from_rustc().await; + #[cfg(feature = "tracing")] + debug!("get_target_from_rustc()={target:?}"); + let target = target.unwrap_or_else(|| { + let target = guess_host_triple::guess_host_triple(); + #[cfg(feature = "tracing")] + debug!("guess_host_triple::guess_host_triple()={target:?}"); + target.unwrap_or(crate::TARGET).to_string() }); cfg_if! { diff --git a/crates/detect-targets/src/detect/linux.rs b/crates/detect-targets/src/detect/linux.rs index a945c881..bba7fe60 100644 --- a/crates/detect-targets/src/detect/linux.rs +++ b/crates/detect-targets/src/detect/linux.rs @@ -4,6 +4,8 @@ use std::{ }; use tokio::{process::Command, task}; +#[cfg(feature = "tracing")] +use tracing::debug; pub(super) async fn detect_targets(target: String) -> Vec { let (prefix, postfix) = target @@ -79,12 +81,25 @@ async fn get_ld_flavor(cmd: &str) -> Option { status, stdout, stderr, - } = Command::new(cmd) + } = match Command::new(cmd) .arg("--version") .stdin(Stdio::null()) .output() .await - .ok()?; + { + Ok(output) => output, + Err(_err) => { + #[cfg(feature = "tracing")] + debug!("Running `{cmd} --version`: err={_err:?}"); + return None; + } + }; + + let stdout = String::from_utf8_lossy(&stdout); + let stderr = String::from_utf8_lossy(&stderr); + + #[cfg(feature = "tracing")] + debug!("`{cmd} --version`: status={status}, stdout='{stdout}', stderr='{stderr}'"); const ALPINE_GCOMPAT: &str = r#"This is the gcompat ELF interpreter stub. You are not meant to run this directly. @@ -93,16 +108,14 @@ You are not meant to run this directly. if status.success() { // Executing glibc ldd or /lib/ld-linux-{cpu_arch}.so.1 will always // succeeds. - String::from_utf8_lossy(&stdout) - .contains("GLIBC") - .then_some(Libc::Gnu) + stdout.contains("GLIBC").then_some(Libc::Gnu) } else if status.code() == Some(1) { // On Alpine, executing both the gcompat glibc and the ldd and // /lib/ld-musl-{cpu_arch}.so.1 will fail with exit status 1. - if str::from_utf8(&stdout).as_deref() == Ok(ALPINE_GCOMPAT) { + if stdout == ALPINE_GCOMPAT { // Alpine's gcompat package will output ALPINE_GCOMPAT to stdout Some(Libc::Gnu) - } else if String::from_utf8_lossy(&stderr).contains("musl libc") { + } else if stderr.contains("musl libc") { // Alpine/s ldd and musl dynlib will output to stderr Some(Libc::Musl) } else { @@ -120,6 +133,9 @@ You are not meant to run this directly. .await .ok()?; + #[cfg(feature = "tracing")] + debug!("`{cmd} --version`: status={status}"); + status.success().then_some(Libc::Gnu) } else { None diff --git a/crates/detect-targets/src/main.rs b/crates/detect-targets/src/main.rs index fbaa187f..2623c077 100644 --- a/crates/detect-targets/src/main.rs +++ b/crates/detect-targets/src/main.rs @@ -4,6 +4,12 @@ use detect_targets::detect_targets; use tokio::runtime; fn main() -> io::Result<()> { + #[cfg(feature = "cli-logging")] + tracing_subscriber::fmt::fmt() + .with_max_level(tracing::Level::TRACE) + .with_writer(std::io::stderr) + .init(); + let targets = runtime::Builder::new_current_thread() .enable_all() .build()?