Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
334 changes: 334 additions & 0 deletions .github/workflows/nokogiri-compatibility.yml

Large diffs are not rendered by default.

44 changes: 40 additions & 4 deletions crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,12 @@ name: nothing of it is run. This holds whatever lock governs the gem home.
(`<home>/.socket-patch-build/<gem>-<version>[-<platform>]/`), by RubyGems, and must be that gem,
declare extensions, and be the copy's own source (above). The record's files are laid over the
staged gem, and every `beforeHash` must hold there, whatever the mismatch policy of the run.
RubyGems then builds the staged gem's extension in the stage, with the build arguments it recorded
for the installed copy (`build_info/`). A gem the staged gem depends on at runtime that is not on
RubyGems then builds the staged gem's extension in the stage. A gem RubyGems built is given the
build arguments RubyGems recorded for the installed copy (`build_info/`), as `gem pristine` gives
them. A precompiled gem was built by its publisher, not on this machine: arguments recorded for
it were never used to build it (Bundler records its `build.<gem>` setting for a platform gem
too), so its rebuild takes none. Either build runs in the environment `apply` was started in, as
`gem install` would. A gem the staged gem depends on at runtime that is not on
the build's `GEM_PATH` (the stage home, the copy's home, the caller's `GEM_PATH`, RubyGems'
defaults) is staged too: the newest ruby-platform release the requirement allows that runs on the
building Ruby, downloaded from the registry under the same rules and verified against its compact
Expand All @@ -969,6 +973,38 @@ library under `lib/`, a Ruby file the build generates under a require path. Such
loaded from where it was built. The staged build is set aside: a gem RubyGems built is rebuilt in
place (below), a precompiled gem is refused.

**A build that builds nothing of the patch.** A build that exits cleanly has not always built
the patch: what a gem compiles depends on how its build is configured. A gem set up to link the
system's copy of a library it packs (nokogiri installed with `--use-system-libraries`) never
opens the packed copy's sources, so a record that changes only those would be written, rebuilt
and attested with nothing of it in what Ruby loads. A gem whose extension script downloads the
binary it runs (passenger's agent) compiles nothing at all. `apply` knows no gem's switches. It
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, which works whatever the mount options), elsewhere
by giving them an access time in the past and looking at it right after. A build that read NONE
of them did not build the patch. One read is enough, because a record also carries files no
build opens. Neither did a build that left no compiled file: nothing in RubyGems' extension
directory, and nothing Ruby loads in the gem's own tree.

In both cases the record's files are written into the installed copy and its compiled code is
left as it was installed: the package's event is `applied`, the run exits 0, and it carries a
`native_extension_not_rebuilt` warning that names the files and says why. The patch's Ruby files
are 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. No build marker is written, so
`apply --check` reports the package as not in sync and `vex` never attests it. A later `apply`
runs the build again and says the same. The one exception is a record that changes a build input
the installed copy no longer holds: only a build from the source gem accounts for a file added
that way, so that package fails with `native_extensions_unsupported` and nothing is written.

Off Linux, where a read leaves no trace in a file's access time (a `noatime` mount, Windows by
default), which `apply` tests with a file of its own, nothing is concluded from the reads and
the build is taken as it is.

**What a build prints.** A build can take many minutes. Each one prints a line to stderr when
it starts (`Rebuilding the native extension of <gem> from <what>; this can take several
minutes.`) and one when it ends (`The build of <gem> finished after <time>.`, or `failed
after`). Neither is printed under `--json` or `--silent`.

**Into the installed copy.** The record's files are written into the copy, and the build takes the
place of the compiled code:

Expand Down Expand Up @@ -1080,9 +1116,9 @@ a file the copy does not hold is not written: the record fails on it as on any m
| Code | Run result | Meaning |
|---|---|---|
| `native_extensions_unsupported` | refused, exit 1 | The record changes build inputs that cannot be rebuilt here: the copy is not a RubyGems install with a `specifications/` entry (a Bundler git checkout); the gem neither declares extensions nor is a platform gem; it is the JRuby (`java`) build; no Ruby that owns the install was found; a precompiled gem cannot be built from its source gem (the archive is neither in the home's `cache/` nor downloadable, is not the copy's own source, the record does not apply to it, a gem its build needs could not be staged, its build leaves a library in the gem's own tree, or the build has no counterpart among the gem's prebuilt binaries); a record changes an input a build removed and the source gem cannot be built. Nothing is written. Also: another run is rebuilding the same gem right now, or an interrupted rebuild could not be undone (the error says which). |
| `native_extension_build_failed` | refused, exit 1 | The extension did not build from the patched sources (no compiler or Ruby headers, a build error, an in-place build that exited cleanly without compiling anything), or the finished build could not be moved into place. The installed copy is as it was before the run; the error carries the build output's tail (of the build from the source gem and of the in-place one, when both ran), at most 30 lines of each, without mkmf's list of configuration options or its pointer to the `mkmf.log` that was removed with the failed build. For a precompiled gem it says that the rebuild needs what `gem install <name> -v <version> --platform ruby` needs. |
| `native_extension_build_failed` | refused, exit 1 | The extension did not build from the patched sources (no compiler or Ruby headers, a build error, an in-place build that exited cleanly and kept the library it found instead of compiling it again), or the finished build could not be moved into place. The installed copy is as it was before the run; the error carries the build output's tail (of the build from the source gem and of the in-place one, when both ran), at most 30 lines of each, without mkmf's list of configuration options or its pointer to the `mkmf.log` that was removed with the failed build. For a precompiled gem it says that the rebuild needs what `gem install <name> -v <version> --platform ruby` needs. |
| `native_extension_rebuilt` | warning | The sources were already patched (by a release before #1996, with `--no-gem-native-rebuild`, or by `apply` under another Ruby version of a precompiled gem) and the extension was rebuilt from them. Under `--dry-run`: the rebuild a real `apply` would run; no Ruby is started and nothing is downloaded to check that it can. |
| `native_extension_not_rebuilt` | warning | `--no-gem-native-rebuild`: the sources were written without a rebuild; Ruby loads the old build until the caller compiles it. |
| `native_extension_not_rebuilt` | warning | The record's files were written and no build holds them. With `--no-gem-native-rebuild`: Ruby loads the old build until the caller compiles it. Without it: a build ran and built nothing of the patch (it read none of the files the patch changes, or left no compiled file), so the compiled code is as it was installed. `vex` and `apply --check` do not take the package for patched. |
| `native_extension_restore_failed` | rollback: that copy fails | The sources were restored, but the original compiled extension could not be put back. Run `gem pristine <gem> --extensions` (a precompiled gem: `gem pristine <gem>`). |

A refusal is printed once on the human path, as the package's `Error: Failed to patch` line. Under
Expand Down
11 changes: 11 additions & 0 deletions crates/socket-patch-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,17 @@ impl GlobalArgs {
socket_patch_core::utils::repo_root::ReadScope::of_global(self.is_global())
}

/// Where a gem's native-extension build says that it started and how
/// it ended (`RubyEnv::progress`): a stderr line each on the human
/// path, so a build of many minutes does not look like a hung run.
/// Nothing under `--json` or `--silent`.
pub(crate) fn gem_build_progress(&self) -> Option<fn(&str)> {
fn to_stderr(line: &str) {
eprintln!("{line}");
}
(!self.json && !self.silent).then_some(to_stderr as fn(&str))
}

/// Whether `--ecosystems` selects `eco` (every ecosystem when unset or
/// empty). The names are validated at parse time, so this is an exact
/// match.
Expand Down
5 changes: 4 additions & 1 deletion crates/socket-patch-cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3048,7 +3048,9 @@ async fn apply_patches_inner(

attempted = true;
// A gem patch to native-extension sources rebuilds the
// extension, or is refused with nothing written (#1996).
// extension (#1996). One it cannot build is refused
// with nothing written; one whose build compiles
// nothing of it is written with a warning (#2144).
let result = if Ecosystem::from_purl(purl) == Some(Ecosystem::Gem) {
let applied = gem_ext::apply_gem_package_patch(
args.common.read_scope(),
Expand All @@ -3062,6 +3064,7 @@ async fn apply_patches_inner(
cwd: args.common.cwd.clone(),
rebuild: !args.common.no_gem_native_rebuild,
offline: args.common.offline,
progress: args.common.gem_build_progress(),
},
)
.await;
Expand Down
2 changes: 2 additions & 0 deletions crates/socket-patch-cli/src/commands/gem_refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl Policy {
cwd: common.cwd.clone(),
rebuild: !common.no_gem_native_rebuild,
offline: common.offline,
progress: common.gem_build_progress(),
},
}
}
Expand Down Expand Up @@ -1128,6 +1129,7 @@ mod tests {
cwd: root.to_path_buf(),
rebuild: true,
offline: true,
progress: None,
},
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/socket-patch-cli/src/commands/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3664,6 +3664,7 @@ pub(crate) async fn rollback_patches_inner(
cwd: common.cwd.clone(),
rebuild: !common.no_gem_native_rebuild,
offline: common.offline,
progress: common.gem_build_progress(),
};
if let Err(e) = gem_ext::restore_original_build(pkg_path, &env).await {
result.success = false;
Expand Down
3 changes: 2 additions & 1 deletion crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2408,7 +2408,8 @@ fn omission_phrase(reason: &str) -> &'static str {
}
"native_extension_not_rebuilt" => {
"the patched native-extension sources were not compiled into the extension Ruby \
loads; run socket-patch apply to rebuild it"
loads; socket-patch apply rebuilds it, or says why this install's build compiles \
nothing of the patch"
}
COMPOSER_INSTALL_UNREAD => {
"composer.json's config.vendor-dir puts the install outside the project, where \
Expand Down
Loading
Loading