Skip to content

Agent apply build failure for a precompiled gem (pg without libpq-dev) cuts off the cause, points at an mkmf.log it has deleted, and says to install a compiler that is already there #2117

Closed

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:

  1. 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.
  2. 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.
  3. 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.
  4. The whole text is printed twice, once as Error: and once as Warning:.

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 directory

Actual

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.rb failure 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.out somewhere 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 ruby needs (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 holding mkmf.log is removed on failure while the tail still names it.

Generated by Claude Code

Activity

  1. added
    bug
    bughunt
    pm:bundler
    ux
    on Nov 17, 2026
  2. mikolalysenko commented on Nov 18, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue. Branch: agent/fix-gem-build-failure-hides-cause.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedbugbughuntpm:bundlerpriority:p1ux

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions