Skip to content

Fix vendored gem bytes changed by its own .gitattributes (#2483) - #2488

Open
Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
agent/fix-gem-shipped-gitattributes
Open

Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
agent/fix-gem-shipped-gitattributes

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fixes #2483

Summary

A gem can ship a .gitattributes inside its own tree. Git reads one at any depth, and for the files below it its rules win over the * -text -filter -ident -working-tree-encoding that vendored mode writes beside the gem. solargraph 0.62.2 ships *.rb text eol=lf over 30 .rb files with CRLF line endings, so git committed them with LF. Every fresh clone then held other bytes than the vendor ledger recorded: vendor --check failed with vendor_inventory_mismatch, vex left the gem out, and the repair the message names restored bytes git would not commit.

With this change:

  • New vendoring. A shipped .gitattributes whose rules turn a conversion on for a file of the copy is emptied while the tree is staged, and the run warns vendor_gem_gitattributes_neutralized with the files named. This is what a shipped .gitignore already gets. It applies to a gem's own tree and to each platform gem's tree. One that converts nothing (merge=ours, eol without text, a pattern that matches no file) and one the patch itself rewrites are kept byte for byte.
  • Copies vendored by 5.2.0 or earlier. They are not rewritten. repair restores a shipped .gitattributes as the ledger inventory records it, so such a copy still verifies. Every in-sync vendored run over one warns vendor_gem_copy_gitattributes and names a remedy that works: delete the copy directory and scan again, which vendors it afresh with the file emptied.
  • Docs. CLI_CONTRACT.md (the gem row and the two new warning codes) and docs/ecosystems.md.

No install is touched: Bundler still loads the same patched files. The only change to a gem's content is an emptied .gitattributes, which Ruby never reads.

Root cause

crates/socket-patch-core/src/vendor/gem.rs emptied a shipped .gitignore and nothing else, so no code read a shipped .gitattributes. The uuid dir's own .gitattributes sits one level above the gem, where a deeper file overrides it.

How a rule is judged

converts_a_file reads the file the way git does under the uuid dir's -text: for each file the last matching line that names an attribute decides it. A conversion is text in any form but -text (!text hands the file back to core.autocrlf), a filter or working-tree-encoding with a value, or ident. eol alone does nothing to a file that is not text, and binary is -text.

Where git's answer depends on the checkout, or the pattern is one the matcher cannot read as git does, the rule counts as a conversion: letter case is ignored (git does that under core.ignorecase), and a POSIX class or a pattern the matcher refuses stands for every file. That errs toward emptying a file and warning about it.

Evidence

All in crates/socket-patch-core/src/vendor/gem.rs:

Test What it shows
a_gitattributes_the_gem_ships_is_emptied_in_the_copy Own tree. Fails on main. Vendors, runs a real git commit and git clone with core.autocrlf off and on, and the clone holds the recorded bytes and passes the artifact check. Harmless, template and patch-owned files are kept; a re-run is in sync; repair reproduces the tree.
a_gitattributes_a_prebuild_ships_is_emptied_in_the_copy Platform gems. Fails on main. Also restores a copy whose ledger records the file as shipped, and checks the in-sync advisory names each platform's file.
a_shipped_gitattributes_is_read_as_git_reads_it 44 rule sets. For 39 of them each is committed below the uuid dir's file and cloned with core.autocrlf off and on, and git changes a file's bytes exactly when the reader calls the rule a conversion.
a_shipped_gitattributes_an_earlier_release_kept_is_reported The advisory for an older copy, dry run included, and its remedy end to end.
restore_keeps_a_shipped_gitattributes_an_earlier_release_recorded repair on an older copy.
a_prebuilds_gitattributes_over_the_stub_gemspec_is_emptied, a_gitattributes_a_platform_patch_rewrites_is_not_reported Two review findings (below).

Commands run locally:

cargo test -p socket-patch-core --lib                               9016 passed
cargo test -p socket-patch-cli --test in_process_vendor gem         17 passed
cargo test -p socket-patch-cli --test e2e_vendor_gem_build -- --ignored   16 passed (Bundler 4.0.18, Ruby 3.3.6)
cargo clippy --locked --workspace --all-features -- -D warnings     clean

cargo clippy -p socket-patch-core --all-targets --all-features -- -D warnings reports 81 errors in test code on this toolchain, none in lines this PR adds; the same command fails on main, and CI's clippy job does not lint test targets.

Review

/code-review high returned eight findings. Seven are fixed in the last commit, each with a test case: a byte order mark, quoted patterns with octal escapes or a leading #, !text=auto and -text=auto, letter case, POSIX classes and unreadable patterns, the stub gemspec being written after the rules were read (platform gems), and the advisory using the lead patch's file list for every platform tree.

Not fixed: a .gitattributes that the patch itself rewrites, with rules that convert a file, is kept and not reported. Its bytes are the patch's and must stay as recorded, so it cannot be emptied; the alternative is to refuse the gem. A security patch that adds line-ending rules to a gem is not a case I could find, and a refusal path would add unwinding to both copy paths, so I left it out.

Follow-ups

  • No real-Bundler case with solargraph was added. The loss happens at git add and does not depend on Bundler, and the core tests go through a real commit and clone; solargraph's dependency tree would add a large network install to every Bundler leg.
  • The issue notes that the vendored directories of other ecosystems that hold unpacked trees may have the same gap. That was not tested here and is not changed.
  • The vendor_inventory_mismatch message still names repair. In a clone of an older copy that does not help; the new advisory in the vendoring checkout is what names the working remedy.

https://claude.ai/code/session_011f9aeP7hTMRyxriRbp2zJU


Generated by Claude Code

Refs #2483.

Assisted-by: Claude Code:claude-fable-5-2
Two of the three cases fail on main: a .gitattributes the gem ships
whose rules convert a file of the vendored copy (*.rb text eol=lf
over CRLF files) is kept as shipped, for a gem vendored as its own
tree and as platform gems, so a clone of the commit holds other
bytes than the ledger recorded. The third pins what repair does for
a copy vendored before the fix.

Refs #2483.

Assisted-by: Claude Code:claude-fable-5-2
Git reads a .gitattributes at any depth, and one a gem ships inside
its own tree wins over the uuid dir's `* -text` for the files below
it. solargraph ships `*.rb text eol=lf` over files with CRLF line
endings, so git committed other bytes than the vendor ledger
recorded: every fresh clone failed vendor --check, vex left the gem
out, and repair restored bytes git would not commit.

A shipped .gitattributes whose rules turn a conversion on for a file
of the copy (text in any form but -text, a filter or
working-tree-encoding value, ident) is now emptied while the tree is
staged and named by vendor_gem_gitattributes_neutralized, for a gem's
own tree and for platform gems, the way a shipped .gitignore is. One
that converts nothing and one the patch rewrites are kept. The rules
are read as git reads them, which a test checks against git itself.

Repair restores such a file as the ledger inventory records it, so a
copy an earlier release vendored still verifies. An in-sync run over
such a copy says so (vendor_gem_copy_gitattributes) and names what
vendors it afresh.

Refs #2483.

Assisted-by: Claude Code:claude-fable-5-2
The CLI contract and the ecosystem guide describe the two new
advisories: a .gitattributes the gem ships that is emptied in the
vendored copy, and a copy an earlier release vendored that keeps one.

Refs #2483.

Assisted-by: Claude Code:claude-fable-5-2
Review of the reader for a gem-shipped .gitattributes found rules
git applies that it took for harmless, so the file was kept:

- a byte order mark before the first rule;
- a quoted pattern with octal or other C escapes, or one that starts
  with `#`;
- `!text=auto` and `-text=auto`, whose value git drops;
- a pattern that differs only in letter case, which git matches
  under core.ignorecase (the default on macOS and Windows);
- a POSIX class such as `[[:space:]]`, and a pattern the matcher
  refuses. Both now stand for every file.

For platform gems the stub gemspec is written before the shipped
rules are read, so a rule that names only it counts, and the in-sync
advisory leaves out the files each platform's own patch rewrites
rather than the lead patch's.

Refs #2483.

Assisted-by: Claude Code:claude-fable-5-2
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review December 3, 2026 18:17
@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

bugbot run


Generated by Claude Code

@cursor

cursor Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Scoped to Ruby gem vendoring, git metadata handling, and contract/docs; no auth or broad install-path changes, with heavy new test coverage.

Overview
Gem vendoring now treats shipped .gitattributes inside the gem tree like shipped .gitignore files: rules that would let git change copy bytes at commit/checkout (e.g. text, filter, ident) cause those files to be emptied during staging, with vendor_gem_gitattributes_neutralized warnings. Logic is shared via refactored empty_shipped / converts_a_file on both plain and platform-gem trees; repair can restore ledger-recorded attributes for older copies.

Existing copies vendored before this behavior still get vendor_gem_copy_gitattributes on in-sync runs (remedy: delete copy and re-vendor). Docs update CLI_CONTRACT.md, ecosystems.md, and redownload passes platform file inventory into restore.

Reviewed by Cursor Bugbot for commit 2e96d45. Configure here.

The reader of a gem-shipped .gitattributes dropped a leading byte
order mark by itself, which the guard on production BOM handling
(formats::text) refuses. It calls strip_bom now, which also drops
exactly one mark, as git does.

Refs #2483.

Assisted-by: Claude Code:claude-fable-5-2
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