Skip to content
Open
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
11 changes: 10 additions & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,16 @@ worse, lets a warm cache silently serve unpatched bytes):
with `bundle remove`, or moved to another release — is not drift: it warns
`vendor_lock_entry_removed`, both files stay as the user left them, and the artifact and entry
are removed, so `vendor --revert` / `rollback` / `remove` / `scan --prune` clean up after it (a
file of the two that cannot be read fails the revert, which keeps both); composer / maven / nuget, whose
file of the two that cannot be read fails the revert, which keeps both; and where the `Gemfile`
line vendor wrote is still the gem's wiring after the user edited it — it is the gem's one
declaration, pins the vendored version, and its `path:` names this patch's copy, with options
added or taken off, the quotes switched, or the line moved into a `group`, `platforms` or
conditional block — so the revert
takes the `path:` off, puts the pre-vendor version requirement back and keeps the rest of the
line as the user has it, and the managed block of a transitive gem goes whole with an edit of
its line; a line that pins another version, carries a statement modifier or a second
statement, or sits in a `source` / `git` / `github` / `path` block stays drift, as does any
edited line while another line declares the gem too); composer / maven / nuget, whose
whole-file wiring cannot tell a converged fragment from a drifted one, keep the artifact exactly
while the live `composer.lock` / `pom.xml` / `nuget.config` still names its
`.socket/vendor/<eco>/<uuid>` dir — a file that no longer references it is warned about and the
Expand Down
124 changes: 124 additions & 0 deletions crates/socket-patch-cli/tests/e2e_vendor_gem_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,130 @@ fn gem_vendor_dropped_gem_is_cleaned_up_by_every_undo_command() {
}
}

/// #2338: the user edits the Gemfile line of a vendored gem (an option
/// added, the line moved into a `group` block, the quotes switched).
/// Bundler takes the edited pair as it is, with the lock unchanged, and
/// `vendor --check` passes. Each undo command then reverts the gem: the
/// line gets its version requirement back, loses the `path:` and keeps the
/// edit, the lock is the pre-vendor one byte for byte, the copy and its
/// ledger record go, and a frozen install passes.
#[test]
#[ignore = "host capstone: shells out to a real bundler >= 1.17; the unpinned `test` job \
skips it, the e2e job runs it with a pinned toolchain via --ignored"]
fn gem_vendor_edited_gemfile_line_is_undone_by_every_undo_command() {
const ORIGINAL_LINE: &str = "gem \"rack\", \"~> 3.1\"";
// The user's edit of a `gem` line, and the command that undoes the
// vendoring.
type Edit = fn(&str) -> String;
let cases: [(&str, Edit, &[&str]); 4] = [
(
"require: false added",
|line| format!("{line}, require: false"),
&["vendor", "--revert"],
),
(
"moved into a group block",
|line| format!("group :test do\n {line}\nend"),
&["rollback"],
),
(
"group: option added",
|line| format!("{line}, group: :web"),
&["remove", "--yes"],
),
(
"quotes switched",
|line| line.replace('"', "'"),
&["rollback"],
),
];
for (how, edit, undo) in cases {
let what = format!("{how}, then `{}`", undo.join(" "));
let Some((_tmp, proj, _bundler, purl)) = staged_rack_project("edited line") else {
return;
};
let cwd = proj.to_str().unwrap();
let original_gemfile = std::fs::read_to_string(proj.join("Gemfile")).unwrap();
assert!(original_gemfile.contains(ORIGINAL_LINE), "test premise");
let original_lock = std::fs::read(proj.join("Gemfile.lock")).unwrap();
let (code, stdout, stderr) =
run_socket(&proj, &["vendor", "--json", "--offline", "--cwd", cwd]);
assert_eq!(code, 0, "{what}: vendor failed.\n{stdout}\n{stderr}");
let uuid_dir = proj.join(".socket/vendor/gem").join(UUID);
assert!(uuid_dir.is_dir(), "{what}: vendored copy written");
let vendored_lock = std::fs::read(proj.join("Gemfile.lock")).unwrap();

// The user's edit: bundler takes the pair as it is.
let wired = std::fs::read_to_string(proj.join("Gemfile")).unwrap();
let wired_line = wired
.lines()
.find(|l| l.contains("path:"))
.expect("the wired line");
let edited = wired.replace(wired_line, &edit(wired_line));
std::fs::write(proj.join("Gemfile"), &edited).unwrap();
for frozen in [false, true] {
let install = bundle(&proj, &["install"], frozen);
assert!(
install.status.success(),
"{what}: bundler accepts the edited pair (test premise, frozen: {frozen}):\n{}",
String::from_utf8_lossy(&install.stderr)
);
}
assert_eq!(
std::fs::read(proj.join("Gemfile.lock")).unwrap(),
vendored_lock,
"{what}: the edit leaves the lock as vendor wrote it (test premise)"
);
let (code, stdout, stderr) = run_socket(&proj, &["vendor", "--check", "--cwd", cwd]);
assert_eq!(
code, 0,
"{what}: vendor --check takes the edited line as wired.\n{stdout}\n{stderr}"
);

// The undo.
let mut args: Vec<&str> = undo.to_vec();
if undo[0] != "vendor" {
args.push(&purl);
}
args.extend(["--json", "--cwd", cwd]);
let (code, stdout, stderr) = run_socket(&proj, &args);
assert_eq!(
code, 0,
"{what}: failed.\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
let env = parse_json_envelope(&stdout);
assert_eq!(env["status"], "success", "{what}: {env}");
let all = format!("{stdout}{stderr}");
assert!(
!all.contains("drift"),
"{what}: a still-wired line is not reported as drift:\n{all}"
);

assert!(!uuid_dir.exists(), "{what}: the copy is removed");
let state = proj.join(".socket/vendor/state.json");
assert!(
!state.exists() || !std::fs::read_to_string(&state).unwrap().contains(UUID),
"{what}: the ledger record is dropped"
);
assert_eq!(
std::fs::read_to_string(proj.join("Gemfile")).unwrap(),
original_gemfile.replace(ORIGINAL_LINE, &edit(ORIGINAL_LINE)),
"{what}: the requirement is back, the `path:` is gone and the edit is kept"
);
assert_eq!(
std::fs::read(proj.join("Gemfile.lock")).unwrap(),
original_lock,
"{what}: the lock is the pre-vendor one"
);
let install = bundle(&proj, &["install"], true);
assert!(
install.status.success(),
"{what}: a frozen install passes after the undo:\n{}",
String::from_utf8_lossy(&install.stderr)
);
}
}

/// #1821: vendoring over a CRLF Gemfile keeps every line CRLF (the rewired
/// `gem` line used to end in a bare LF), a real frozen install accepts the
/// result, and revert restores the Gemfile byte for byte.
Expand Down
Loading
Loading