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
113 changes: 102 additions & 11 deletions crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2860,8 +2860,8 @@ impl StagedSource {
/// story after a hosted scan (#1105): `uv sync` / `poetry install` from
/// the hosted lock installs a hosted patch's bytes, whichever generation
/// the pin names now. The pin names no release variant, so it only answers
/// for a sole candidate; gems keep their installed dir, which the gem
/// backend reads to refuse a platform gem.
/// for a sole candidate. A gem in that state is not superseded but
/// [`hosted_gem_install`]: it keeps its installed dir.
fn superseded_install(
ledger: &VendorState,
candidate: &str,
Expand All @@ -2876,6 +2876,22 @@ fn superseded_install(
&& matches!(Ecosystem::from_purl(candidate), Some(Ecosystem::Pypi)))
}

/// Whether an installed gem that fails `candidate`'s variant probe is still
/// `candidate` itself, under a live hosted pin (#2129): a hosted scan or a
/// `bundle install` from the hosted lock put a hosted patch's bytes there,
/// and the patch has since been superseded, so they match neither side of
/// this record. The gem backend takes no file of the installed copy (the
/// vendored copy is the service's build), so the candidate goes on with
/// its installed dir as the source: the backend still reads that dir's
/// name and gemspec to refuse a platform gem or a native extension. As for
/// PyPI, the pin names no release variant, so it only answers for a sole
/// candidate.
fn hosted_gem_install(candidate: &str, sole_candidate: bool, hosted_pinned: bool) -> bool {
sole_candidate
&& hosted_pinned
&& matches!(Ecosystem::from_purl(candidate), Some(Ecosystem::Gem))
}

/// The stand-in source of a superseded install (see [`superseded_install`]),
/// relative to the project root: a path that never exists, so the backend
/// takes the pristine artifact from the lock, the registry or the service
Expand Down Expand Up @@ -3004,16 +3020,22 @@ impl<'a> CandidateGates<'a> {
if !variant_matches_installed(Some(
&verify_file_patch(dir, file, info).await.status,
)) {
if !superseded_install(
ledger,
candidate,
record,
group.candidates.len() == 1,
(self.hosted_pinned)(candidate),
) {
return CandidateVerdict::Skip;
let sole_candidate = group.candidates.len() == 1;
let hosted_pinned = (self.hosted_pinned)(candidate);
// The hosted gem is asked first: it goes on from
// its installed dir, whatever the ledger records.
if !hosted_gem_install(candidate, sole_candidate, hosted_pinned) {
if !superseded_install(
ledger,
candidate,
record,
sole_candidate,
hosted_pinned,
) {
return CandidateVerdict::Skip;
}
superseded = true;
}
superseded = true;
}
}
} else if group.candidates.len() > 1
Expand Down Expand Up @@ -5453,6 +5475,75 @@ mod plan_gate_tests {
));
}

/// #2129: a live hosted pin lets a sole gem candidate past a failed
/// variant probe with its installed dir as the source. Other ecosystems
/// and a gem with several variant records are not its business.
#[test]
fn hosted_pin_keeps_a_gem_install_as_the_source() {
let gem = "pkg:gem/rack@3.2.8";
assert!(hosted_gem_install(gem, true, true));
assert!(!hosted_gem_install(gem, false, true));
assert!(!hosted_gem_install(gem, true, false));
assert!(!hosted_gem_install("pkg:pypi/six@1.16.0", true, true));
}

/// #2129 at the gate: an installed gem that matches neither side of
/// the record is skipped, unless the lockfiles pin it hosted. Then it
/// is vendored from its installed dir (never the stand-in a superseded
/// install gets), even when the ledger also records it at an older
/// uuid, so the backend still sees the dir's name and gemspec.
#[tokio::test]
async fn judge_sends_a_hosted_gem_on_from_its_installed_dir() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let purl = "pkg:gem/rack@3.2.8";
let dir = root.join("gems/rack-3.2.8");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("index.php"), "an older patch's bytes").unwrap();
let all_packages = vec![(purl.to_string(), StagedSource::Installed(dir))];
let records = HashMap::from([(purl.to_string(), record(UUID_B))]);
let none = HashSet::new();
let groups = candidate_groups(&all_packages, &HashMap::new());
let unpinned = gates(root, &records, &none);
let pinned = CandidateGates {
hosted_pinned: &|_| true,
..gates(root, &records, &none)
};
let mut older = VendorState::default();
older.entries.insert(
purl.to_string(),
VendorEntry::new(
"gem".to_string(),
purl.to_string(),
UUID_A.to_string(),
socket_patch_core::vendor::state::VendorArtifact {
yarn_berry10c0: None,
path: format!(".socket/vendor/gem/{UUID_A}/rack-3.2.8"),
sha256: String::new(),
size: None,
platform_locked: None,
file_inventory: None,
},
Vec::new(),
),
);
let fresh = VendorState::default();

assert!(matches!(
unpinned.judge(&fresh, &groups[0], purl).await,
CandidateVerdict::Skip
));
for ledger in [&fresh, &older] {
assert!(matches!(
pinned.judge(ledger, &groups[0], purl).await,
CandidateVerdict::Vendor {
superseded: false,
..
}
));
}
}

/// Gates with no vendored refusals, no hosted pins and no `--force`.
pub(super) fn gates<'a>(
root: &'a Path,
Expand Down
Loading
Loading