Skip to content

Never take a native gem build that compiled nothing of a patch for a patched build; test agent mode on real nokogiri - #2136

Merged
Mikola Lysenko (mikolalysenko) merged 11 commits into
mainfrom
test/nokogiri-e2e
Nov 18, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 11 commits into
mainfrom
test/nokogiri-e2e

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

Summary

Adds an end-to-end test of agent-mode patches on real nokogiri installs, run
over 25 combinations of Ruby, nokogiri release, install shape and platform,
and fixes the defects it found, plus the ones reported in #2144 (passenger).
They share one cause: a native gem's build exits cleanly without compiling
what the patch changes, and apply took that for a build of the patch.

  • apply replaced the libxml2 a precompiled nokogiri ships with the
    system's.
    A project that still carries Bundler's build.nokogiri --use-system-libraries gets that argument recorded for the precompiled
    platform gem too, and the rebuild followed it.

  • A patch to the packed libxml2 was attested on an install that never
    compiles it.
    On a nokogiri built against the system's libxml2, a record
    that only adds patches/libxml2/*.patch (the shape of about a third of the
    live nokogiri patches) was written, "rebuilt" against the same system
    library, and reported by vex as patched.

  • A gem whose extension script downloads the binary it runs was reported
    as patched (apply reports a passenger gem patch as applied while the downloaded agent it runs is unchanged; on main the rebuild runs 16 minutes and its product is discarded silently #2144).
    passenger's only extension fetches a prebuilt agent.
    A patch to the agent's C++ was "applied" and would be attested while the
    agent was byte-for-byte unchanged, and on main a 16-minute staged build
    ran with nothing printed and its product was discarded.

The first two are not in a release: the rebuild from the source gem landed on
main after 5.1.0.

Fixes #2144

What was measured

On nokogiri 1.18.10, macOS arm64, Ruby 3.4.10 (system libxml2 2.9.13, packed
libxml2 2.13.9):

Install Record main This branch
precompiled, project has build.nokogiri --use-system-libraries C source applied; Ruby then loads libxml2 2.9.13 instead of 2.13.9 applied; libxml2 2.13.9 as shipped
built with --use-system-libraries adds patches/libxml2/… applied in 20 s, apply --check in sync, vex not_affected; libxml2 2.9.13 with no patch file written, exit 0 with a native_extension_not_rebuilt warning, apply --check not in sync, not attested
built with --use-system-libraries C source rebuilt against the system's unchanged
built from the packed libraries, or precompiled adds patches/libxml2/… rebuilt, patch compiled in unchanged

The fix

Both rules are about RubyGems builds in general. Nothing in the CLI names
nokogiri, its switches, its log or its directories.

  • A build that built nothing of the patch is not taken for one. A
    compiler reads what it compiles, and a build script reads the patch files
    it applies. apply watches the record's build inputs during the build,
    staged or in place: on Linux with a kernel watch on those files being
    opened (inotify), elsewhere by giving them an access time in the past and
    looking at it right after. A build that exited cleanly having read NONE of
    them did not build the patch. Neither did one that left no compiled file
    (nothing in RubyGems' extension directory, nothing Ruby loads in the gem's
    tree). One read is enough to pass, because a record also carries files no
    build opens. Where a read leaves no trace (noatime or Windows, off
    Linux), which apply tests with a file of its own, nothing is concluded
    from the reads.
  • Such a patch is written, not refused, and never attested. The record's
    files go into the installed copy, the compiled code stays as it was
    installed, the package's event is applied with exit 0, and the run
    carries a native_extension_not_rebuilt warning that names the files and
    the reason. No build marker is written, so apply --check reports the
    package as not in sync and vex does not attest it. This keeps the
    patch's Ruby files in effect, and a source the gem compiles later on its
    own (passenger builds its agent the first time it starts without one) is
    the patched one. An earlier version of this branch refused instead; apply reports a passenger gem patch as applied while the downloaded agent it runs is unchanged; on main the rebuild runs 16 minutes and its product is discarded silently #2144
    showed why that is wrong for a patch that relies on its Ruby half. The one
    refusal left: a record that changes a build input the installed copy no
    longer holds, which only a build from the source gem accounts for.
  • A build says when it starts and how it ended. One stderr line each
    (Rebuilding the native extension of <gem> from <what>; this can take several minutes. / The build of <gem> finished after 4m 12s.), not
    under --json or --silent.
  • A precompiled copy is rebuilt without recorded build arguments.
    RubyGems records the arguments a gem was installed with, and Bundler
    records its build.<gem> setting for a platform gem too, although nothing
    was built with them. A copy RubyGems built is given its recorded
    arguments again, as gem pristine gives them; a precompiled copy, which
    its publisher built, takes none.
  • A refusal for a gem whose lock names a private gem server now says to set
    SOCKET_RUBYGEMS_URL when that server is a mirror, and a server named with
    credentials there matches the same server named without them in the lock.
  • CLI_CONTRACT.md and docs/ecosystems.md describe the rules.

The nokogiri test

tests/nokogiri/nokogiri_e2e.rb is one Ruby file with no gem dependencies
(Ruby 2.1 syntax), so it runs wherever the install under test runs. It
installs one nokogiri release with the real Bundler, lays three hand-built
records over it with the real binary, and checks what a fresh Ruby loads
through bundle exec:

  • a Ruby-only change (no build),
  • a C source change (ext/nokogiri/nokogiri.c),
  • a new patch for the packed libxml2 (its name is compiled into the
    extension).

Each scenario runs apply --check, apply, the load, apply --check,
apply again, vex and rollback, then compares every file of the install
with what it started from. With --rails it also boots a Rails application
and sanitizes HTML through the patched nokogiri.

.github/workflows/nokogiri-compatibility.yml runs it (not required for
merge; path-filtered on PRs, on pushes to main that touch the rebuild, and
nightly):

Legs What
Ruby 3.4 to 2.7, nokogiri 1.19.3 / 1.18.10 / 1.16.8 / 1.15.7 / 1.13.10 / 1.11.7, Rails 8.1 to 6.0 precompiled x86_64-linux and -gnu gems
Ruby 2.6 / 2.5 / 2.3, nokogiri 1.10.10 / 1.8.5 / 1.6.8.1, Rails 5.2 / 5.1 / 4.2 compiled by RubyGems (no precompiled gem exists)
Ruby 3.4, nokogiri 1.18.10 forced source build; --use-system-libraries (the libxml2 record is written and not attested); the legacy setting on a precompiled gem; no compiler on PATH
JRuby 9.4 Ruby-only applies, a C record is refused
ubuntu-24.04-arm, macOS, Windows the aarch64-linux-gnu, arm64-darwin and x64-mingw-ucrt gems; a source build on macOS
ruby:3.4-alpine, ruby:3.4-slim (with and without a compiler), ruby:2.7, 2.5, 2.3, 2.1 inside official images, gems in the image's own gem home, musl and old Debian toolchains, RubyGems 2.2

The x86_64 Linux legs share one static musl binary; macOS, Windows and ARM
build their own.

What a reviewer should weigh

  • Off Linux the read check rests on file access times. APFS records the
    first read after an access time older than the modification time, which is
    what the check sets up. On a noatime mount off Linux and on Windows it
    cannot tell, and says nothing. The CI runners for the x86_64 Linux legs
    are mounted noatime, which is why Linux uses inotify. A build that
    copies its whole source tree before compiling reads every file, so it
    always passes.
  • An install whose build compiles nothing of a patch now gets exit 0 and a
    warning from apply, and a failing apply --check for as long as that
    patch is in the manifest. That is deliberate: the files are written and
    the compiled code is not patched, and --check answers the second.
    scan picks one patch per package version by severity, not by whether it
    can take effect on this install.
  • Nothing remembers that a build compiled nothing of a record, so every
    apply runs that build again before it says so: about 10 to 20 seconds
    for a system-library nokogiri, as long as the gem's build takes otherwise. A
    memo beside the build marker would save it; the marker file is
    special-cased in enough places that I left it out of this change.
  • A staged build with no compiled file is not attested even for a gem that
    compiles nothing on this platform by design. Nothing tells the two apart,
    and withholding is the side that cannot be wrong.
  • A rebuild runs in the environment apply was started in, as gem install
    does. A gem installed with a build switch given only in the environment of
    that one install (nothing records it) is rebuilt without it, as gem pristine would rebuild it.
  • The Windows leg passes only with a short project path. The stage adds
    about 57 characters to every build path, and MinGW's ar stops at 260. A
    rebuild there takes about 8 minutes.

Code review

/code-review high ran twice.

The first run, on the first version of the fix (which read nokogiri's
switches), raised ten findings. Its main one, that the rule was specific to
one gem's layout and left others (rugged, ffi) attested, is what the read
check answers: it holds for any gem. Also fixed then: a refusal message that
had lost its line continuations; the mirror hint offered where it could not
work; the leftover development trigger; fourteen redundant CLI builds; and a
retry filter that matched every Bundler failure.

The second run, on this version, raised ten more. Fixed:

  • vex told the user to run apply to rebuild a copy whose build compiles
    nothing of the patch.
  • The staged install of a patched gem (hosted mode's stale-install refresh)
    and of a gem a build needs printed nothing while they ran.
  • The credential-insensitive registry match lived in one caller, so the lock
    restore disagreed with it; it is in the shared is_rubygems_remote now,
    with the shared URL stripper instead of a second one.
  • The warning listed the patch's files twice; comments still described the
    refusal.

Not fixed, and why:

  • A patch that is fully in effect can be left unattested. The input
    classification is broad on purpose (anything that is not clearly plain
    counts, because a build script reads whatever the gem packs). A record
    whose only inputs are files no build opens (a runtime data file, a source
    compiled only on another platform) beside Ruby files is now written and
    not attested, where main rebuilt and attested it. Reads cannot tell that
    case from a system-library nokogiri, where the patch really is in nothing
    that runs, so the rule withholds in both. apply --check then stays out
    of sync for that package.
  • Exit 0 and an applied event for a patch whose compiled part is not in
    effect.
    A pipeline that gates only on the exit code proceeds. apply reports a passenger gem patch as applied while the downloaded agent it runs is unchanged; on main the rebuild runs 16 minutes and its product is discarded silently #2144 asks
    for a qualified apply, not a failure, because the patch's Ruby half is
    what makes passenger build its patched agent. The qualification is the
    native_extension_not_rebuilt warning, a failing apply --check and no
    VEX statement. A failing exit code is the alternative if you prefer it.
  • Every apply repeats the build before it reaches the same conclusion
    (see above). A memo is a follow-up.
  • The tests take the read check for granted on Linux. In a sandbox with
    no inotify on a noatime mount the two hermetic tests would fail instead
    of skipping. CI has inotify.

Testing

  • Unit test for the read check: a deleted input is not marked, looking at a
    file or listing its directory is not a read, one read passes, nothing
    marked concludes nothing. Unit tests for credentials in a server URL.
  • e2e_gem_native_ext_build, hermetic tests with the stand-in Ruby: a
    precompiled copy built without recorded arguments and a built one with
    them; a staged build that reads nothing of the patch writes it, warns, is
    not attested, says the same on a second run, rolls back to the same tree,
    and the same record is built by a build that reads it; an in-place build
    that reads nothing keeps the previous build; a build that leaves no
    compiled file (the passenger shape) writes the patch and attests nothing;
    the start and end lines are printed, and not under --json or --silent.
    30 pass locally.
  • The nine real-toolchain tests of that suite pass locally, including
    a_build_that_compiles_nothing_is_refused_and_not_attested, which failed
    on the previous head because the read check answered before the
    stale-build check. So do e2e_redirect_gem_build (47) and
    e2e_vendor_gem_build (11), 191 unit tests in the gem modules, and CI's
    clippy command.
  • Real nokogiri 1.18.10 built with --use-system-libraries, locally with
    the final code (40 checks pass): the Ruby record applies, the C record is
    rebuilt against the same system libxml2 and attested, and the libxml2
    record is written in 8 s with the warning, loads the same binary, is not
    in sync for apply --check, is not attested, and rolls back to a
    byte-identical tree. The other install shapes run in CI.
  • Not part of this PR's tests, run by hand on an earlier commit of this
    branch: the 51 live catalog patches for nokogiri 1.10.10, 1.11.7, 1.13.10,
    1.17.2, 1.18.3 and 1.19.3, on precompiled and source installs (59 runs on
    Ruby 2.6 and 3.4, macOS arm64), each applied, loaded, checked, attested and
    rolled back to a byte-identical tree.
  • passenger itself was not run. The stand-in build reproduces its shape (a
    clean exit, an empty extension dir, a binary outside what Ruby loads).

🤖 Generated with Claude Code

tests/nokogiri/nokogiri_e2e.rb installs one nokogiri release with the real
Bundler, applies a Ruby-only record, a C source record and a
vendored-libxml2 patch record with the real binary, and checks what the
install's Ruby loads, apply --check, vex and a byte-identical rollback.

nokogiri-compatibility.yml runs it on the runner (Ruby 2.3 to 3.4 and
JRuby, the precompiled gem of each line, source and system-library builds,
macOS, Windows, ARM) and inside official Ruby images (Alpine, slim, the
Debian releases old Rubies shipped on) with a static binary.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
libxml2's configure script reads INSTALL from the environment as the
install program, so a step that set INSTALL=precompiled broke every build.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Each precompiled and source leg now installs the Rails of its years and
checks that a booted application sanitizes HTML through the patched
nokogiri. The ruby:2.3 leg names the image's manifest list, which Docker
can pull, and ruby:2.1 covers the oldest RubyGems a rebuild supports.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Two defects found with real nokogiri installs:

A precompiled gem was rebuilt with the build arguments RubyGems recorded
for it, which were never used to build it. Bundler's build.nokogiri
--use-system-libraries, a setting many projects kept from before nokogiri
shipped precompiled gems, is recorded for the platform gem too, so apply
replaced the libxml2 2.13 the gem ships with the system's 2.9. A
precompiled copy is now rebuilt as its publisher built it: no recorded
arguments, and NOKOGIRI_USE_SYSTEM_LIBRARIES removed from the build.

A gem built against the system's libraries compiles nothing of the library
it packs. A record that only changes the packed library (a new
patches/libxml2/*.patch, a third of the published nokogiri patches) was
written, rebuilt against the same system library and attested by vex as
patched. It is now refused from files alone (recorded build arguments, the
build log, or the environment) with nothing written, and never attested.

The nokogiri matrix covers both, and its Rails legs hold the loofah of
their nokogiri's years.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A precompiled gem whose lock names a private gem server is refused with
"put the source gem in cache/". Most such servers are mirrors of
rubygems.org, and naming one in SOCKET_RUBYGEMS_URL is all it takes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Review of the previous commit found the system-library handling covered
precompiled copies only and guessed at mkmf's rules:

- A gem RubyGems built is rebuilt against what its installed build linked,
  read from that build's log, else from the recorded build arguments. A
  nokogiri installed with NOKOGIRI_USE_SYSTEM_LIBRARIES set for the install
  alone was rebuilt with the packed libxml2, and one built from the packed
  libraries was rebuilt against the system's when the variable was set at
  apply time.
- Build arguments are read as mkmf reads them (--enable, then --disable,
  then --use, any value a yes), word by word, since Bundler 1 recorded a
  whole setting on one line.
- The variable alone counts only for nokogiri, and only when neither the
  log nor the arguments say what the copy links.
- apply --check and vex do not attest a packed-library record on a
  system-library build, whatever marker the copy holds.
- The refusal message had lost its line continuations.
- A lock's gem server and SOCKET_RUBYGEMS_URL name one server with or
  without credentials, and the mirror hint is given only where it can work.

The x86_64 Linux legs of the nokogiri matrix run the one static binary
instead of building the CLI fourteen times, a leg covers the variable-only
install, and the workflow's development trigger is gone.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

High Risk
Changes apply/refusal and attestation for native gem patches—especially nokogiri—where incorrect linking or false vex success has direct security implications, though behavior is heavily tested.

Overview
Fixes agent-mode native extension rebuilds for nokogiri when installs use --use-system-libraries or still carry legacy Bundler build.nokogiri settings on precompiled platform gems. Precompiled copies are now rebuilt like the publisher (packed libxml2), without replaying recorded build args or honoring NOKOGIRI_USE_SYSTEM_LIBRARIES. RubyGems-built copies rebuild against whatever the installed build actually linked, inferred from gem_make.out and build_info (mkmf/Bundler 1 argument parsing), with the env var set or cleared to match.

Patches that only touch vendored library inputs (patches/**, ports/**, dependencies.yml) are refused with native_extensions_unsupported when the install links system libraries, and apply --check / vex no longer attest those cases on such builds.

Adds tests/nokogiri/nokogiri_e2e.rb and .github/workflows/nokogiri-compatibility.yml (broad matrix: Ruby 2.1–3.4, precompiled/source/system/JRuby, runners and official Ruby images, shared static musl CLI on Linux). Hermetic Rust e2e tests and docs (CLI_CONTRACT.md, ecosystems.md) are updated; gem_source improves registry/mirror detection and suggests SOCKET_RUBYGEMS_URL when a lock points at a private HTTPS gem server.

Reviewed by Cursor Bugbot for commit 95b54a1. Configure here.

The system-library handling knew nokogiri: its environment variable, the
wording of its build log, mini_portile's directories and a check on the
gem's name. It is replaced by what holds for every gem.

A compiler reads what it compiles, and a build script reads the patch
files it applies. The record's build inputs are given an access time in
the past before the build, staged or in place, and a build that exits
cleanly having read none of them is not a build of the patch: the copy is
left as it was and is never attested. That is the case of a gem built
against the system's copy of a library it packs, without the CLI knowing
any gem's switches. One read is enough, since a record also carries files
no build opens, and nothing is concluded where the filesystem records no
reads.

What stays is the rule about RubyGems' own record: a copy RubyGems built
is rebuilt with the arguments recorded for it, and a precompiled copy,
which its publisher built, with none.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Access times say whether a build read a file only where the filesystem
records reads. This repo's own CI runners are mounted noatime, and so are
many container hosts, so there the check concluded nothing and a patch
that was compiled into nothing was still attested. On Linux the kernel
says which of the watched files were opened, whatever the mount options;
access times stay as the way to tell elsewhere.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Without a kernel watch, an input whose access time could not be set may
be the one the build read.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A gem's build does not always compile what a patch changes. One built
against the system's copy of a library it packs never opens the packed
copy, and one whose extension script downloads the binary it runs
(passenger's agent) compiles nothing at all. apply reported such a patch
as applied, and vex attested it, while the code the gem runs was
unchanged (#2144). Since the read check it was refused with nothing
written, which also kept the patch's Ruby files out.

apply now writes the record's files, leaves the compiled code as it was
installed, and warns with native_extension_not_rebuilt when a clean
build read none of the record's build inputs or left no compiled file.
No build marker is written, so apply --check reports the package as not
in sync and vex does not attest it. A record that changes a build input
the installed copy no longer holds is still refused: only a build from
the source gem accounts for a file added that way.

An in-place build that kept the library it found is still a failed
build: that reason is reported ahead of the read check, which the
real-toolchain test of it expects.

A build can run for many minutes with nothing printed. Each one now
says on stderr when it starts and how it ended (not under --json or
--silent).

Fixes #2144

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko Mikola Lysenko (mikolalysenko) changed the title Fix nokogiri rebuilds that swap or skip the packed libxml2, and test real nokogiri installs Never take a native gem build that compiled nothing of a patch for a patched build; test agent mode on real nokogiri Nov 18, 2026
- vex told the user to run apply to rebuild a copy whose build compiles
  nothing of the patch, which changes nothing there. It now says that
  apply rebuilds it or says why it cannot.
- The staged install of a patched gem (the hosted stale-install refresh)
  and of a gem a build needs printed nothing while they ran. Both now
  say what they are doing.
- The registry test that ignores credentials lived in gem_source alone,
  so the lock restore still took a mirror named with credentials for a
  different server. The shared is_rubygems_remote now does it, with the
  shared URL stripper in place of a second one.
- The warning listed the patch's files twice in one sentence.
- Comments in the nokogiri harness and in apply still described the
  refusal the previous commit replaced.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Merged via the queue into main with commit 4fe0859 Nov 18, 2026
81 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the test/nokogiri-e2e branch November 18, 2026 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants