Repository navigation
Description
[agent] Filed by the scheduled architecture refactor routine. Found by running the vendored Gemfile rewrite over 4,000 generated Gemfiles and asking real Bundler what each result means, then through the binary with Bundler 4.0.18. main at cd7c5c5.
Problem
vendor replaces the project's gem line with gem "<name>", "<version>", path: "<copy>" and keeps the options the line had. It keeps what it reads as keyword arguments (key: v, "key": v, key => v, **opts) and drops every other argument as a version requirement that the exact pin supersedes (#847).
Bundler takes a trailing Hash as the call's options, however it is written:
gem "rack", "~> 3.1", { require: false }
OPTS = { require: false, group: :web }
gem "rack", "~> 3.1", OPTSThe rewrite reads { require: false } and OPTS as requirements and drops them:
gem "rack", "3.2.9", path: ".socket/vendor/gem/<uuid>/rack-3.2.9"vendor exits 0 with status: success, applied: 1 and no warning. The declaration has lost its options, so the project behaves differently from then on:
- without
require: false,Bundler.requireloads the gem at boot (what Vendored mode declares a transitive gem withoutrequire: false, soBundler.requirestarts loading it at boot (hosted mode adds the option) #2084 fixed for the line socket-patch adds itself); - without
group:, the gem is in the default group: installed and loaded where its group was left out (BUNDLE_WITHOUT); - without
platforms:, a gem Bundler skipped on this platform is now a dependency on it.
Hosted mode refuses both lines. For the first its refusal names vendored mode as the way out ("spell its options out as literal key: value arguments, or patch rack with --mode vendored or --mode agent"), which leads here.
Evidence on main
Through the binary, with real Bundler 4.0.18. A test added to the vendored gem capstone (e2e_vendor_gem_build, on its staged_rack_project): install, socket-patch vendor --json --offline, a frozen bundle install, then bundle exec ruby -e 'require "bundler/setup"; Bundler.require; …' before and after.
| Gemfile line | vendor |
Line after | Bundler.require loads rack: before → after |
Group: before → after |
|---|---|---|---|---|
gem "rack", "~> 3.1", { require: false } |
exit 0, success, applied: 1 |
gem "rack", "3.2.9", path: "…/rack-3.2.9" |
no → yes | default → default |
OPTS = { require: false, group: :web } ↵ gem "rack", "~> 3.1", OPTS |
exit 0, success, applied: 1 |
gem "rack", "3.2.9", path: "…/rack-3.2.9" |
no → yes | web → default |
gem "rack", "~> 3.1", require: false (control) |
exit 0, success, applied: 1 |
gem "rack", "3.2.9", path: "…/rack-3.2.9", require: false |
no → no | default → default |
The frozen install passes in all three rows, so nothing fails: the change shows only in what the application loads. vendor --revert exits 0 and restores the Gemfile byte for byte in each row, so that is the way out today.
Which spellings, and what hosted mode does with the same line. The vendored planner (plan_gemfile_edit) and the hosted rewriter (rewrite_registry_redirect) over one Gemfile each, the results read by Bundler::Dsl (rack 2.2.3 here):
| Declaration | Vendored | What Bundler reads after | Hosted |
|---|---|---|---|
gem "rack", "~> 2.2", { require: false } |
rewritten, hash dropped | autorequire [] → nil |
refused, redirect_gem_source_option |
gem "rack", "~> 2.2", { :require => false } |
rewritten, hash dropped | autorequire [] → nil |
refused |
gem "rack", "~> 2.2", { "require" => false } |
rewritten, hash dropped | autorequire [] → nil |
refused |
gem "rack", "~> 2.2", { platforms: :jruby } |
rewritten, hash dropped | platforms [jruby] → []; not a dependency on MRI → a dependency |
refused |
gem "rack", { require: false, group: :test } |
rewritten, hash dropped | group test → default, autorequire [] → nil |
refused |
OPTS = { require: false } ↵ gem "rack", "~> 2.2", OPTS |
rewritten, OPTS dropped |
autorequire [] → nil |
refused, redirect_gem_unrecognized_declaration ("its last argument OPTS is not a literal, so it may be an options hash") |
opts = { group: :test } ↵ gem "rack", opts |
rewritten, opts dropped |
group test → default |
refused |
DEV = { require: false, group: :development } ↵ gem "rack", "2.2.3", DEV # dev only |
rewritten, DEV dropped |
group development → default, autorequire [] → nil |
refused |
gem "rack", "~> 2.2", **OPTS |
rewritten, **OPTS kept |
unchanged | refused |
gem "rack", "~> 2.2", require: false |
rewritten, option kept | unchanged | rewritten |
RACK_VERSION = "~> 2.2" ↵ gem "rack", RACK_VERSION |
rewritten, constant dropped (a requirement) | unchanged but for the pin | rewritten (the lock shows one requirement for the one argument) |
gem "rack", "~> 2.2", OPTS.merge(group: :x) |
refused (a method call) | — | refused |
How wide. 4,000 generated Gemfiles with the declaration mostly at top level. Of the 696 the planner rewrote in place, 27 lost options, each with a { … } literal or a constant as the last argument; 618 read the same under Bundler but for the pin and the path.
A unit test next to the code (vendor/gem.rs, with the module's fixture, run_vendor and LOCK_DIRECT):
#[tokio::test]
async fn a_trailing_options_hash_is_not_a_version_constraint() {
let mut lost = Vec::new();
for decl in [
"gem \"rack\", \"~> 3.1\", { require: false }",
"gem \"rack\", \"~> 3.1\", OPTS",
"gem \"rack\", \"~> 3.1\", { platforms: :jruby }",
] {
let gemfile = format!(
"source \"https://rubygems.org\"\n\nOPTS = {{ require: false, group: :web }}\n{decl}\n"
);
let (_tmp, root, installed, blobs, record) = fixture(&gemfile, LOCK_DIRECT).await;
let _ = run_vendor(&root, &blobs, &installed, &record, false).await;
let after = tokio::fs::read_to_string(root.join(GEMFILE)).await.unwrap();
let line = after.lines().find(|l| l.starts_with("gem \"rack\"")).unwrap_or("");
let kept = after == gemfile
|| line.contains("require: false")
|| line.contains("OPTS")
|| line.contains("platforms:");
if !kept {
lost.push(format!("{decl}\n -> {line}"));
}
}
assert!(lost.is_empty(), "the options are gone:\n{}", lost.join("\n"));
}$ cargo test -p socket-patch-core --lib a_trailing_options_hash
the options are gone:
gem "rack", "~> 3.1", { require: false }
-> gem "rack", "3.2.6", path: ".socket/vendor/gem/9f6b2c4e-1d3a-4f6b-8c2d-7e5a9b1c3d5f/rack-3.2.6"
gem "rack", "~> 3.1", OPTS
-> gem "rack", "3.2.6", path: ".socket/vendor/gem/9f6b2c4e-1d3a-4f6b-8c2d-7e5a9b1c3d5f/rack-3.2.6"
gem "rack", "~> 3.1", { platforms: :jruby }
-> gem "rack", "3.2.6", path: ".socket/vendor/gem/9f6b2c4e-1d3a-4f6b-8c2d-7e5a9b1c3d5f/rack-3.2.6"
test result: FAILED. 0 passed; 1 failed
Where
- The split:
vendor/gem.rs#L3239-L3300(split_kept_tail: "Everything before it … is positional and dropped"), used at#L3079-L3098, under the comment "Trailing options (require: false,group: :test, …) must survive the rewrite: droppingrequire: falseauto-requires the gem at boot, changing app behavior while vendored". - A comment that says otherwise:
#L3358-L3359("A**optssplat or hash literal is kept afterpath:"). Only the splat is. - What counts as droppable:
formats/gem/gemfile.rs#L326-L342(inert_positionals: a hash of literals "whose keys select no source" is a positional argument). - The hosted rule: [
patch/redirect/mod.rs#L8694-L8729](https://github.com/SocketDev/socket-patch/blob/cd7c5c56e3e0049a5c06260c354a597b8f07658b/crates/socket-patch-core/src/patch/redirect/mod.rs#L8694-L8729) ("A trailing argument whose value is unknown may be the call's options hash"), overformats/gem/declarations.rs(ArgKind::Dynamic,opaque_last). - The contract:
CLI_CONTRACT.md, "Gem rewrite." ("Constants, splats, local variables,ENVand other one-key lookups (and any option, which the vendored line keeps verbatim) are rewritten as before").
Why it is in the charter
- Drifted duplicates. Two readers split one
gemcall into requirements and options. The hosted one (Fix the defects found by the 5.1.0 full review: do no harm, Bundler hosted mode, VEX, monorepo scans #2052) knows a trailing hash, or a last argument it cannot read, may be the options, and refuses. The vendored one (Fix vendored gem rewrite breaking positional args (#847) #849) drops it as a requirement. The table above is the same input through both. - A mechanism bug users can see, and do no harm. A successful
vendorchanges what the application loads at boot, in which group, and on which platform. - A printed remedy that leads to it. Hosted mode's refusal of the
{ … }line names--mode vendored. - For the block comment: the same drift (one reader masks what is not code, the other does not), and a red frozen install after
success.
Planned fix
Find the declaration with the hosted reader (formats::gem::declarations), so that only a line it lists is taken for one, and refuse a rewrite in place with gemfile_declaration_not_editable, before any write, when:
- its last argument is a
{ … }hash literal; or - its last argument is a positional whose value is not known (a constant, a local variable, a lookup) and the lock's
DEPENDENCIESentry does not show one requirement for each positional argument. This is the hosted rule, sogem "rack", RACK_VERSIONandgem "rack", ENV.fetch("V", "~> 3.1")are rewritten as today.
The refusal says to spell the options out as key: value arguments, as hosted mode's does. **opts stays kept. The comment at L3358 and the contract's "Gem rewrite." paragraph get the clause. Tests: the unit test above, the rows of the first table as a capstone, and the existing #847 cases unchanged. About 40 production lines.
Keeping the hash instead, as **{ … } or **OPTS after path:, would change more of the user's line than the contract's "keeps verbatim" allows, and vendor --revert would have to undo it. Say so on this issue if that is preferred.
Not opened as a PR now: three of this routine's PRs await review.
Seen with the same reader
A gem line that is not code. The vendored planner reads the Gemfile line by line and skips only lines that start with #. Hosted mode reads a masked copy in which comments, strings and heredoc bodies are blank. So a line inside a =begin … =end block is a declaration for vendored mode and nothing for hosted mode.
Through the binary (the same capstone, rack 3.2.9 a dependency of rack-test):
source "https://rubygems.org"
=begin
gem "rack", "~> 3.1"
=end
gem "rack-test"vendor exit 0, status "success", summary {"applied":1,"failed":0,…}
Gemfile after:
=begin
gem "rack", "3.2.9", path: ".socket/vendor/gem/<uuid>/rack-3.2.9"
=end
gem "rack-test"
Gemfile.lock after: a PATH section for rack, and "rack (= 3.2.9)!" under DEPENDENCIES
BUNDLE_FROZEN=true bundle install:
The list of sources changed, but the lockfile can't be updated because frozen mode is set
You have deleted from the Gemfile:
* rack (= 3.2.9)
vendor --check: exit 1, vendor_check_failed, "wiring missing: no lockfile or config references .socket/vendor/gem/<uuid> any more, so a fresh install gets the unpatched package; re-run `socket-patch vendor` to rewire it"
The comment was edited, the Gemfile declares nothing new, and the lock names a dependency the Gemfile does not have: a frozen install fails, and vendor --check fails straight after a successful vendor. It takes a block comment that holds a top-level gem line for the patched gem, in a project that does not declare the gem on a line the planner reads. In the 4,000 generated Gemfiles the planner rewrote such a line in 230 (a =begin block, a heredoc body, a line after __END__); hosted mode took none of them for a declaration in the batches of #2517.
The plan above covers it when the declaration is found by the hosted reader: a line that reader does not list is not a declaration.
Not run
get <uuid> --mode vendoredand a vendoredscan(the same backend);gems.rb; Bundler 1.17 to 2.7.- A trial build of the fix.
get <uuid>andscanafter the block-comment row, andvendor --revertof it. vendor --checkandvexafter the rewrite: nothing they read changed, so they have nothing to report.
The fixtures
The capstone tests. In tests/e2e_vendor_gem_build.rs, for each Gemfile of the first table: staged_rack_project, write the Gemfile, bundle install, the probe (require "bundler/setup"; Bundler.require, then whether Rack is defined, the dependency's groups and autorequire, and Gem.loaded_specs["rack"].full_gem_path), socket-patch vendor --json --offline --cwd <proj>, a frozen bundle install, the probe again, vendor --revert. The block-comment row: the same project with the Gemfile above, bundle install, vendor, a frozen bundle install, vendor --check --json --offline. cargo test -p socket-patch-cli --test e2e_vendor_gem_build -- --ignored <name>.
The planner probe. An example in the core crate that calls plan_gemfile_edit, refuse_append_of_direct_dependency and apply_gemfile_plan (made pub in the working tree for the build) over a directory of cases; the hosted side is rewrite_registry_redirect with one rubygems-compact-index override.
The generator and the oracle are those of #2517: a seeded Ruby generator (three gems, the patched one in many spellings and places, decoys in comments and heredocs), a lock whose DEPENDENCIES are Bundler's own reading of the Gemfile, and Bundler::Dsl#eval_gemfile on the input and the output, comparing each dependency's requirement, source, groups, platforms, autorequire and whether it applies on this platform. For this run the declaration sits at top level in 85 of 100 files.
Generated by Claude Code
Activity
refactor: L216, L217 filed as #2517, #2518 (Gemfile)