Skip to content

Commit 1a1c96e

Browse files
committed
fix(go-runner): detect missing C compiler before building instrument hooks
1 parent 9ab56f8 commit 1a1c96e

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

go-runner/src/runner/mod.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,43 @@ use tempfile::TempDir;
88

99
mod overlay;
1010

11+
fn check_c_compiler(go_binary: &Path) -> anyhow::Result<()> {
12+
let output = Command::new(go_binary)
13+
.args(["env", "CC"])
14+
.output()
15+
.context("Failed to run `go env CC`")?;
16+
17+
if !output.status.success() {
18+
let stderr = String::from_utf8_lossy(&output.stderr);
19+
bail!("Failed to determine C compiler via `go env CC`: {stderr}");
20+
}
21+
22+
let cc = String::from_utf8_lossy(&output.stdout).trim().to_string();
23+
if cc.is_empty() {
24+
bail!(
25+
"No C compiler found. The CodSpeed Go runner requires a C compiler (gcc/cc) \
26+
to build the instrumentation hooks. Install `build-essential` on Ubuntu/Debian \
27+
or the equivalent for your platform."
28+
);
29+
}
30+
31+
Ok(())
32+
}
33+
1134
fn run_cmd<P: AsRef<Path>>(
1235
profile_dir: P,
1336
dir: P,
1437
cli: &Cli,
1538
) -> anyhow::Result<(TempDir, Command)> {
16-
let (_dir, overlay_file) = overlay::get_overlay_file(profile_dir.as_ref())?;
17-
1839
// Execute the `go test` command using the go binary, rather than the one in the PATH
1940
// to avoid running into infinite loops with the runner which tries to intercept `go test`.
2041
let go_binary = find_go_binary()?;
2142

43+
// Check early, before downloading instrument-hooks and generating the overlay.
44+
check_c_compiler(&go_binary)?;
45+
46+
let (_dir, overlay_file) = overlay::get_overlay_file(profile_dir.as_ref())?;
47+
2248
// Convert the CLI struct into a command:
2349
let mut cmd = Command::new(go_binary);
2450
cmd.args([
@@ -44,6 +70,11 @@ fn run_cmd<P: AsRef<Path>>(
4470
cmd.env("GOCACHE", _dir.path().join("gocache"));
4571
cmd.env("GOMODCACHE", _dir.path().join("gomodcache"));
4672

73+
// The overlay includes instrument-hooks.go which uses cgo (`import "C"`).
74+
// If CGO_ENABLED=0 (e.g. no C compiler on a bare metal runner), Go silently
75+
// excludes the file, causing "undefined: InstrumentHooks" build errors.
76+
cmd.env("CGO_ENABLED", "1");
77+
4778
Ok((_dir, cmd))
4879
}
4980

0 commit comments

Comments
 (0)