Skip to content

Commit c704eb2

Browse files
authored
feat: add platform debugging and cross-compile detection (#17)
- Add 'gitclaw platform' command to show detected OS/arch - Add check_target_mismatch() to detect cross-compiled binaries - Warn on startup if binary was compiled for different OS than runtime - Prevents confusing issues like Darwin binary selecting wrong packages on Linux Example warning: 'This gitclaw binary was compiled for macOS but is running on Linux.'
1 parent 232c040 commit c704eb2

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

src/cli.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub enum Commands {
4242
#[arg(value_enum)]
4343
shell: Shell,
4444
},
45+
/// Show platform information
46+
Platform {},
4547
/// Update gitclaw itself
4648
SelfUpdate {
4749
/// Only check for updates, don't install

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ async fn main() {
3333

3434
let cli = Cli::parse();
3535

36+
// Check for platform mismatch (e.g., Darwin binary on Linux)
37+
if let Some(warning) = platform::check_target_mismatch() {
38+
eprintln!("{}", warning);
39+
}
40+
3641
// Load configuration and merge with CLI args
3742
let config = match Config::load() {
3843
Ok(cfg) => cfg,
@@ -85,6 +90,21 @@ async fn run(cli: Cli, config: Config) -> anyhow::Result<()> {
8590
let name = cmd.get_name().to_string();
8691
generate(shell, &mut cmd, name, &mut std::io::stdout());
8792
}
93+
Commands::Platform {} => {
94+
let (os, arch) = gitclaw::platform::current_platform()?;
95+
println!("Detected platform: {} {}", os, arch);
96+
#[cfg(target_os = "macos")]
97+
println!("Compiled for: macOS");
98+
#[cfg(target_os = "linux")]
99+
println!("Compiled for: Linux");
100+
#[cfg(target_os = "windows")]
101+
println!("Compiled for: Windows");
102+
println!("OS aliases: {:?}", os.aliases());
103+
println!("Arch aliases: {:?}", arch.aliases());
104+
if let Some(warning) = gitclaw::platform::check_target_mismatch() {
105+
println!("\n{}", warning);
106+
}
107+
}
88108
Commands::SelfUpdate { check } => {
89109
if check {
90110
self_update::check_for_update(&config).await?

src/platform.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,49 @@ pub fn current_platform() -> Result<(OS, Arch), PlatformError> {
9191
Ok((detect_os()?, detect_arch()?))
9292
}
9393

94+
/// Check if the binary's compile target matches runtime platform
95+
/// This catches cross-compiled binaries run under emulation
96+
pub fn check_target_mismatch() -> Option<String> {
97+
// Use conditional compilation to detect the compiled target
98+
let compiled_for_macos = cfg!(target_os = "macos");
99+
let compiled_for_linux = cfg!(target_os = "linux");
100+
let compiled_for_windows = cfg!(target_os = "windows");
101+
102+
let runtime_os = std::env::consts::OS;
103+
104+
// If compiled for macOS but running on Linux (e.g., under Rosetta or emulation)
105+
if compiled_for_macos && runtime_os == "linux" {
106+
return Some(
107+
"Warning: This gitclaw binary was compiled for macOS but is running on Linux.\n\
108+
This will cause incorrect package selection.\n\
109+
Please install the Linux version or build from source: cargo install --path ."
110+
.to_string(),
111+
);
112+
}
113+
114+
// If compiled for Linux but running on macOS
115+
if compiled_for_linux && runtime_os == "macos" {
116+
return Some(
117+
"Warning: This gitclaw binary was compiled for Linux but is running on macOS.\n\
118+
This will cause incorrect package selection.\n\
119+
Please install the macOS version or build from source: cargo install --path ."
120+
.to_string(),
121+
);
122+
}
123+
124+
// If compiled for Windows but running on Unix
125+
if compiled_for_windows && (runtime_os == "linux" || runtime_os == "macos") {
126+
return Some(
127+
"Warning: This gitclaw binary was compiled for Windows but is running on a Unix system.\n\
128+
This will cause incorrect package selection.\n\
129+
Please install the correct version for your OS."
130+
.to_string(),
131+
);
132+
}
133+
134+
None
135+
}
136+
94137
pub fn score_asset(name: &str, os: OS, arch: Arch) -> i32 {
95138
let lower = name.to_lowercase();
96139
let mut score = 0;

0 commit comments

Comments
 (0)