Skip to content

Fix gem build failure hiding its cause (#2117) - #2128

Merged
Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-build-failure-hides-cause
Nov 18, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 4 commits into
mainfrom
agent/fix-gem-build-failure-hides-cause

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

LLM Description written by Claude Code:claude-fable-5-2

Fixes #2117
Refs #2106 (only the remedy wording of a failed in-place build changes; argon2 still cannot be rebuilt in place, see Follow-ups)

Summary

When agent apply rebuilds a gem's native extension and the build fails, the error now shows why. Nothing about when a build runs or what it writes changes: this PR only changes the text of a failure that already left the installed gem untouched.

Before, for a precompiled pg 1.6.2 on a machine without libpq-dev: 17 lines of mkmf configuration options, a pointer to an mkmf.log that had been deleted, "Install a compiler toolchain and the Ruby headers", all printed twice.

After (same machine, same record):

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. pg-1.6.2-x86_64-linux was installed precompiled, so nothing of it was built on this machine before: the rebuild needs what `gem install pg -v 1.6.2 --platform ruby` needs, which is a compiler toolchain, the Ruby headers and the libraries the gem builds against. The lines below say what failed; when they name something as missing, install it and apply again.
[...]
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
[...]
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
extconf failed, exit code 1

Summary: 0 of 1 targeted patch applied, 0 already patched, 1 failed, 0 not found on disk

Root cause

stderr_tail (crates/socket-patch-core/src/patch/gem_ext.rs) kept the last 20 non-empty lines of the build's stderr. RubyGems ends the output of a failed extconf.rb with mkmf's "Provided configuration options" list (27 lines for pg) and a pointer to the build's mkmf.log, so the tail was that list, and the lines naming the cause were cut off. The log is in the stage directory, which is removed before the error is printed. The two callers put a fixed "Install a compiler toolchain and the Ruby headers" in front of that tail. The CLI then printed a failed gem refusal as the package's Error: line and again as a Warning: with the same text, because the refusal is also carried as a coded warning for the JSON envelope.

What changed

  • stderr_tail drops mkmf's option list (the heading and the tab-indented lines under it) and RubyGems' mkmf.log pointer with its path line, then takes the last 30 lines instead of 20. Tab-indented lines elsewhere (a Ruby backtrace, make output) are kept.
  • The remedy of a precompiled gem's failed rebuild says the gem was never built on this machine and names the source gem's install command and what it needs. The remedy of a failed in-place build no longer assumes the compiler is what is missing.
  • apply on the human path no longer prints a Warning: that repeats a failed package's Error: line (restates_a_failure in commands/apply.rs). This holds for both refusal codes, native_extension_build_failed and native_extensions_unsupported. --json output is unchanged: the package's error and the warnings[] entry are both still there.
  • A failed in-place build no longer prints "bundle install --redownload will revert these patches" for a gem it put back unpatched. It still does when an earlier run left a file of the record patched.
  • docs/ecosystems.md says that a precompiled gem's rebuild is the first time the gem's own build libraries are needed. CLI_CONTRACT.md describes the tail and the single print.

Evidence

Check Result
The issue's repro (pg 1.6.2, Ruby 3.3.6, Bundler 4.0.18, no libpq-dev) on origin/main 89c482d reproduces: option list, deleted mkmf.log path, printed twice
Same repro on this branch output above, printed once, exit 1, every file under vendor/ has the same sha256 as before the run
cargo test -p socket-patch-core --lib 7797 passed (before the last commit; gem_ext tests rerun after it: 62 passed)
cargo test -p socket-patch-cli --lib 950 passed
e2e_gem_native_ext_build -- --include-ignored (real Ruby 3.3.6 and gcc) 34 passed
in_process_gem_apply, in_process_gem_multi_platform, e2e_redirect_gem_build, e2e_vendor_gem_build, e2e_redirect_gem_stale_install, all with --include-ignored 2, 7, 47, 18, 41 passed (run before the last commit, which touches none of their code paths except the remedy text)
cargo clippy --locked --workspace --all-features -- -D warnings (CI's command) clean

Tests for #2117, all red on origin/main (the first commit adds them alone):

  • gem_ext::tests::stderr_tail_keeps_the_cause_of_a_failed_extconf and stderr_tail_keeps_indented_lines_outside_the_option_list: the filter.
  • e2e_gem_native_ext_build::a_failed_extconf_shows_its_cause_once: the stand-in Ruby fails a build the way a missing library does, for a precompiled gem and for a source-built one. It asserts the cause is shown, the option list and the log pointer are not, the remedy text, one print and no Warning: on the human path, the coded warnings[] entry under --json, and an unchanged vendor/ tree.
  • e2e_gem_native_ext_build::a_failed_build_keeps_the_advisory_of_a_file_patched_before and apply::tests::only_the_coded_copy_of_a_failed_result_restates_it: the two edges the review raised.

Review

/code-review high ran on the diff and gave five findings. Three are fixed in the last commit (the gem's name and version taken from the source gem id, remedy wording that held only when there was build output, the advisory dropped when an earlier run left a file patched). Two are not:

  • Surface the tail of mkmf.log instead of dropping the pointer. Not done here. The issue allows either, and reading a file whose path the gem's build printed into an error message is a design choice a maintainer should make. mkmf's own "Check the mkmf.log file for more details" line stays in the output, so a failure whose only detail is in that log (a link error during a have_library check) still does not show it. Listed under Follow-ups.
  • Mark the warning as envelope-only where it is pushed, instead of matching it against the failed result's error. Not done: it needs a new field on RunWarning or a second list through ApplyOutcome, for the same behavior. If the two strings ever drift apart, the e2e test above fails.

Follow-ups

https://claude.ai/code/session_011f9aeP49dwo2SCWb55mzyj


Generated by Claude Code

Refs #2117.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeP49dwo2SCWb55mzyj
Two unit tests for the build-output filter and one end-to-end test
with the stand-in Ruby, which now can fail a build the way a gem's
extconf.rb does when a library is missing. All three fail on main:
the error ends in mkmf's list of configuration options, points at
an mkmf.log in the removed stage, names a compiler as the remedy
and is printed twice.

Refs #2117.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeP49dwo2SCWb55mzyj
When agent apply rebuilds a gem's native extension and the build
fails, the error carried the last 20 lines of the build's stderr.
For a failed extconf.rb those are mkmf's list of configuration
options, so the lines naming the cause (a missing libpq-fe.h for a
precompiled pg) were cut off. The tail also pointed at an mkmf.log
in the stage, which is removed before the error is printed.

The diagnostic now drops mkmf's option list and its mkmf.log
pointer before taking the tail, which grows to 30 lines. The remedy
of a precompiled gem says that the rebuild needs what
`gem install <name> --platform ruby` needs, and the in-place one
no longer assumes the compiler is what is missing.

On the human path a failed gem refusal was printed twice, as the
package's Error line and as a Warning with the same text. The
warning is now kept for the JSON envelope only. A failed in-place
build also no longer advises that `bundle install --redownload`
reverts a patch it did not leave applied.

Fixes #2117. Refs #2106.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeP49dwo2SCWb55mzyj
From the review of this branch:

- The remedy of a precompiled gem's failed rebuild names the
  source gem by its name and version instead of a name derived
  from the installed directory.
- Both remedies no longer promise build output that names what is
  missing: a build that timed out or could not start has none.
- A failed in-place build keeps the `bundle install --redownload`
  advisory when an earlier run left a file of the record patched,
  which this run does not put back.
- CLI_CONTRACT.md says for every native-extension refusal that it
  is printed once on the human path.

Refs #2117.

Assisted-by: Claude Code:claude-fable-5-2
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011f9aeP49dwo2SCWb55mzyj
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 18, 2026 14:19
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes gem rebuild error formatting and CLI stderr gating only on failure paths; behavior for successful applies is unchanged, but failure UX and advisory text affect operator workflows.

Overview
Fixes #2117: when gem native extension rebuild fails during apply, users now see the actual build cause (e.g. missing libpq-fe.h) instead of mkmf's long option list and a pointer to a deleted mkmf.log.

stderr_tail in gem_ext.rs now strips mkmf's "Provided configuration options" block and the mkmf.log path, keeps useful indented output elsewhere, and raises the diagnostic cap from 20 to 30 lines. Failure remedy text distinguishes precompiled gems (names gem install … --platform ruby and build-time libraries) from in-place rebuilds (does not assume only compiler/headers are missing). Failed rollback drops the redownload sidecar when this run put everything back, but keeps it when an earlier run left files patched.

On the human CLI path, restates_a_failure suppresses run Warning: lines that duplicate a package's Error: Failed to patch (JSON warnings[] unchanged). Docs in CLI_CONTRACT.md and ecosystems.md match; e2e/unit tests cover extconf failures and deduplication.

Reviewed by Cursor Bugbot for commit f545e89. Configure here.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f545e89. Configure here.

Merged via the queue into main with commit 59823ff Nov 18, 2026
82 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the agent/fix-gem-build-failure-hides-cause branch November 18, 2026 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants