Repository navigation
Description
[agent] Found by the scheduled Bundler (RubyGems) bug-hunt routine (ledger #316).
Summary
When agent apply rebuilds a precompiled gem from its source gem and the gem's extconf.rb fails because a development library is missing (here pg 1.6.2 on a machine without libpq-dev), the native_extension_build_failed error doesn't let the user find out why:
- The error keeps the last 20 lines of the build's stderr. For any mkmf failure those lines are mkmf's list of configuration options (
--with-pg-dir,--without-pg-lib, ...). The line that names the cause,Can't find the 'libpq-fe.h header, and the gem's own advice (sudo apt install libpq-dev) come before that list and are cut off. - The tail ends with "To see why this extension failed to compile, please check the mkmf.log which can be found here:
<gem home>/.socket-patch-build/pg-1.6.2-x86_64-linux/home/extensions/.../mkmf.log". socket-patch deletes.socket-patch-build/before it prints the error, so that file doesn't exist. - The remedy says "Install a compiler toolchain and the Ruby headers and apply again." Both are installed. What is missing is the gem's own build dependency.
- The whole text is printed twice, once as
Error:and once asWarning:.
The installed gem is left untouched (checked: every file under vendor/bundle has the same sha256 before and after), so this is friction and not harm. It matters because a precompiled gem is the case where the user has never needed the gem's build dependencies: bundle install worked without libpq-dev, and the first time they hear of a build is this message.
Repro
A machine with a compiler and Ruby headers, without libpq-dev (/usr/include/postgresql/libpq-fe.h absent). No patch API is needed; the agent record is written by hand.
mkdir pgproj && cd pgproj && git init -q .
printf 'source "https://rubygems.org"\n\ngem "pg", "1.6.2"\n' > Gemfile
bundle config set --local path vendor/bundle
bundle install # installs pg-1.6.2-x86_64-linux (precompiled)
G=$(ls -d vendor/bundle/ruby/*/gems/pg-*)
cat > mkrec.py <<'PY'
import sys, json, hashlib, os
purl, key, before, after = sys.argv[1:5]
os.makedirs(".socket/blobs", exist_ok=True)
def gh(b): return hashlib.sha256(b"blob %d\0" % len(b) + b).hexdigest()
b, a = open(before, "rb").read(), open(after, "rb").read()
for x in (b, a): open(".socket/blobs/" + gh(x), "wb").write(x)
json.dump({"patches": {purl: {"uuid": "77777777-7777-4777-8777-777777777772",
"exportedAt": "2026-01-01T00:00:00Z",
"files": {key: {"beforeHash": gh(b), "afterHash": gh(a)}},
"vulnerabilities": {"GHSA-aaaa-bbbb-cccc": {"cves": ["CVE-2026-0001"], "summary": "s", "severity": "HIGH", "description": "d"}},
"description": "synthetic test patch", "license": "MIT", "tier": "free"}}},
open(".socket/manifest.json", "w"), indent=2)
PY
cp $G/ext/pg.c before.c
(cat before.c; printf '\nint socket_patched_marker(void) { return 1; }\n') > after.c
python3 mkrec.py pkg:gem/pg@1.6.2 ext/pg.c before.c after.c
socket-patch apply; echo "exit=$?" # exit 1
ls vendor/bundle/ruby/*/.socket-patch-build # No such file or directoryActual
Error: Failed to patch pkg:gem/pg@1.6.2: native_extension_build_failed: the native extension did not build from the patched sources (ext/pg.c); nothing of the installed gem was changed. Install a compiler toolchain and the Ruby headers and apply again.
--ruby=/opt/rbenv/versions/3.3.6/bin/$(RUBY_BASE_NAME)
--with-pg
--without-pg
--enable-gvl-unlock
--disable-gvl-unlock
--with-cross-build
--without-cross-build
--with-pg-config
--without-pg-config
--with-pg_config
--without-pg_config
--with-pg-dir
--without-pg-dir
--with-pg-include=${pg-dir}/include
--without-pg-include
--with-pg-lib=${pg-dir}/lib
--without-pg-lib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/…/pgproj/vendor/bundle/ruby/3.3.0/.socket-patch-build/pg-1.6.2-x86_64-linux/home/extensions/x86_64-linux/3.3.0/pg-1.6.2/mkmf.log
extconf failed, exit code 1
Warning: the native extension did not build from the patched sources (ext/pg.c); nothing of the installed gem was changed. Install a compiler toolchain and the Ruby headers and apply again.
[the same 20 lines again]
Summary: 0 of 1 targeted patch applied, 0 already patched, 1 failed, 0 not found on disk
For comparison, gem install pg -v 1.6.2 --platform ruby on the same machine prints, a few lines above that option list:
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*****************************************************************************
Please install libpq or postgresql client package like so:
sudo apt install libpq-dev
The same shape shows with any mkmf failure. With no gcc on PATH, a C patch to precompiled ffi 1.17.1 ends in a Ruby backtrace through mkmf.rb and the same pointer to an mkmf.log under the deleted .socket-patch-build/.
Expected
docs/ecosystems.md, "Ruby gems: native extensions": "When the build fails, the installed gem is left as it was and the package fails with native_extension_build_failed, showing the tail of the build output." CLI_CONTRACT.md on agent gem refusals: "the message says what is needed".
The message should let the user fix the build without reading source:
- Show the part of the output that carries the cause. For an
extconf.rbfailure that is the text before mkmf's "Provided configuration options:" block; cutting at that marker (as is already done for "Gem files will remain installed") and then taking the tail would keep it. - Don't point at a file that has been deleted. Keep the failed stage's
mkmf.log/gem_make.outsomewhere and name that path, or drop the line. - Say in the remedy that a precompiled gem is rebuilt from source and needs what
gem install <gem> --platform rubyneeds (a compiler, the Ruby headers and the gem's own libraries). - Print it once.
The docs passage could also say that a precompiled gem's rebuild needs the gem's development libraries, which the user's bundle install never did.
Versions
| socket-patch | Ruby | Bundler | Result |
|---|---|---|---|
main 3b14afc |
3.3.6 | 4.0.18 | reproduces (three runs on two days, identical output) |
The rebuild from the source gem is new in #2098 (610ba8d), so 5.1.0 isn't affected in this form: it refuses a precompiled gem before any build. Linux only; on macOS the missing piece would more often be a Homebrew library, with the same truncated output.
Suspect code
crates/socket-patch-core/src/patch/gem_ext.rs:1434(stderr_tail): takes the last 20 non-empty lines; mkmf's option dump is longer than that for most gems.crates/socket-patch-core/src/patch/gem_ext.rs:232(BUILD_STAGE_DIR): the stage holdingmkmf.logis removed on failure while the tail still names it.
Generated by Claude Code
Activity
bundler: 2026-11-17 run 2 (native rebuild rest, Bundler 1.17.3, #2117)
mikolalysenko commentedon Nov 18, 2026
[agent] Claiming this issue. Branch: agent/fix-gem-build-failure-hides-cause.
Generated by Claude Code
Test that a failed gem build shows its cause
Show the cause of a failed gem extension build
Name the source gem and keep a prior advisory