Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate incorrect target detection #1376

Merged
merged 3 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/bin/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub fn logging(log_level: LevelFilter, json_output: bool) {
"binstalk_registry",
"cargo_binstall",
"cargo_toml_workspace",
"detect_targets",
"simple_git",
]);

Expand Down
2 changes: 1 addition & 1 deletion crates/binstalk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
6 changes: 6 additions & 0 deletions crates/detect-targets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
14 changes: 10 additions & 4 deletions crates/detect-targets/src/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")] {
Expand All @@ -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<String> {
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! {
Expand Down
30 changes: 23 additions & 7 deletions crates/detect-targets/src/detect/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let (prefix, postfix) = target
Expand Down Expand Up @@ -79,12 +81,25 @@ async fn get_ld_flavor(cmd: &str) -> Option<Libc> {
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.
Expand All @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions crates/detect-targets/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?
Expand Down