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
18 changes: 11 additions & 7 deletions .github/workflows/nokogiri-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ name: nokogiri patch compatibility
# The `hosted` legs run the script's hosted scenario instead: a hosted scan
# of a lock that pins nokogiri as platform gems, against a stand-in for the
# patch service (tests/nokogiri/patch_service.rb) that serves a patched build
# of each real platform gem; then a frozen install from a fresh checkout,
# `vex` and a byte-identical `rollback`. On JRuby the platform is `java`.
# of each real platform gem, each as a patch of its own built from the ruby
# gem's patch, whose registry serves them all; then a frozen install from a
# fresh checkout, `vex` and a byte-identical `rollback`. On JRuby the
# platform is `java`.
#
# The `vendored` legs run the script's vendored scenario instead: a vendored
# scan of a lock that pins nokogiri as platform gems, against a stand-in for
# the patch service (tests/nokogiri/patch_service.rb) that serves a patched
# build of each real platform gem; then a frozen install from a fresh
# checkout with the service unreachable, `vex` and a `rollback` to the lock
# as it was. On JRuby the platform is `java`.
# scan of a lock that pins nokogiri as platform gems, against the same
# stand-in; then a frozen install from a fresh checkout with the service
# unreachable, `vex` and a `rollback` to the lock as it was. On JRuby the
# platform is `java`.
#
# Every leg downloads gems from rubygems.org, and a rebuild of a precompiled
# install downloads the source gem and `mini_portile2` from it too.
Expand All @@ -90,6 +91,9 @@ on:
- 'crates/socket-patch-core/src/patch/redirect/grant.rs'
- 'crates/socket-patch-core/src/patch/redirect/upstream/gem.rs'
- 'crates/socket-patch-cli/src/commands/gem_refresh.rs'
# Which patches a run takes for a gem locked as platform gems.
- 'crates/socket-patch-core/src/hosted/gem_fixes.rs'
- 'crates/socket-patch-core/src/hosted/gem_index.rs'
# Vendored mode's gem backend.
- 'crates/socket-patch-core/src/vendor/gem.rs'
- 'crates/socket-patch-core/src/vendor/verify.rs'
Expand Down
21 changes: 12 additions & 9 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

55 changes: 37 additions & 18 deletions crates/socket-patch-cli/src/commands/agent_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,7 @@ pub(crate) async fn filter_to_installed_releases(
(kept, warnings, views)
}

/// Order a patch selection the way every other collection in the JSON
/// envelope is ordered: by purl, uuid breaking a tie (a release-variant
/// base can keep several qualified purls, and `--all-releases` can keep
/// several patches for one purl).
pub(crate) fn sort_by_purl(patches: &mut [PatchSearchResult]) {
patches.sort_by(|a, b| a.purl.cmp(&b.purl).then_with(|| a.uuid.cmp(&b.uuid)));
}
pub(crate) use socket_patch_core::api::ranking::sort_by_purl;

/// Which state store the shared fetch loop classifies each selected patch
/// against — the one non-presentational difference between the vendored
Expand Down Expand Up @@ -1343,9 +1337,7 @@ pub(crate) fn detached_ledger_record<'a>(
let RecordStore::Ledger(entries) = store else {
return None;
};
lookup_entry(entries, purl)
.filter(|e| e.detached && e.uuid == uuid)
.and_then(|e| e.record.as_ref())
socket_patch_core::vendor::detached_record_at(entries, purl, uuid)
}

/// The fetch loop both download engines share: installed-release
Expand All @@ -1372,14 +1364,39 @@ pub(crate) async fn fetch_selected_patches(
// once selection (and get's confirm prompt) is done.
let mut status = crate::ui::StatusLine::stderr(params.json, params.silent);
status.set("Preparing download");
let (selected, warnings, views) = filter_to_installed_releases(
selected,
// Vendored mode commits what the project's Bundler lock names, on
// every platform it names: the patches of a gem the lock names go by
// its builds (each platform gem has a patch of its own), not by the
// one copy installed on this machine.
let (locked_gems, selected): (Vec<PatchSearchResult>, Vec<PatchSearchResult>) =
if matches!(store, RecordStore::Ledger(_)) {
let cwd = params.crawler_options().cwd;
let view = socket_patch_core::vendor::lock_inventory::ProjectView::Disk(&cwd);
let lock = socket_patch_core::hosted::gem_fixes::GemLock::read(
&view,
selected.iter().map(|patch| patch.purl.as_str()),
)
.await;
let (gems, rest): (Vec<_>, Vec<_>) = selected
.iter()
.cloned()
.partition(|patch| lock.names_gem(&patch.purl));
(lock.narrow_selected(gems), rest)
} else {
(Vec::new(), selected.to_vec())
};
let (mut selected, warnings, views) = filter_to_installed_releases(
&selected,
params.all_releases,
&params.crawler_options(),
quiet,
api_client,
)
.await;
if !locked_gems.is_empty() {
selected.extend(locked_gems);
sort_by_purl(&mut selected);
}
// Agent mode writes into the installed copies. A patch no copy can
// take, because conda built each of them as something other than the
// pure-Python PyPI release, is left out here instead of being recorded
Expand Down Expand Up @@ -1546,12 +1563,14 @@ pub(crate) async fn fetch_selected_patches(
RecordStore::Manifest(manifest) => {
decide_patch_action(manifest, &patch.purl, &patch.uuid)
}
RecordStore::Ledger(entries) => match lookup_entry(entries, &patch.purl) {
Some(entry) if entry.uuid != patch.uuid => PatchAction::Updated {
old_uuid: entry.uuid.clone(),
},
_ => PatchAction::Added,
},
RecordStore::Ledger(entries) => {
match socket_patch_core::vendor::vendored_uuid(entries, &patch.purl) {
Some(old) if old != patch.uuid => PatchAction::Updated {
old_uuid: old.to_string(),
},
_ => PatchAction::Added,
}
}
};
if action == PatchAction::Skipped {
// No line of its own: the caller has listed the patch, and the
Expand Down
97 changes: 95 additions & 2 deletions crates/socket-patch-cli/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,23 @@ pub async fn run(args: GetArgs) -> i32 {
&telemetry,
)
.await;
let selected = vec![search_result_from_response(&patch)];
// Hosted and vendored mode change what Bundler installs: a
// gem the lock names as platform builds moves on every
// platform or on none.
let selected = if mode == super::scan::ScanMode::Agent {
vec![search_result_from_response(&patch)]
} else {
status.set("Reading the project's locked gems...");
let selected = with_gem_fix_builds(
&args.common.cwd,
&api_client,
use_public_proxy,
&patch,
)
.await;
status.finish();
selected
};
let user_patch_warnings =
match super::user_patches::check(&args.common, [&patch.purl]).await {
Ok(warnings) => {
Expand Down Expand Up @@ -1899,6 +1915,33 @@ pub async fn run(args: GetArgs) -> i32 {
Err(code) => return code,
};

// Hosted and vendored mode change what Bundler installs: a gem the lock
// names as platform builds takes one fix for every locked build.
// Settled before anything is said of the selection, so that what is
// printed as chosen, and what is reported as waiting for a merged
// patch, is the selection the run uses.
let selected = if mode == super::scan::ScanMode::Agent {
selected
} else {
let (settled, unsettled) = socket_patch_core::hosted::gem_fixes::settle(
&socket_patch_core::vendor::lock_inventory::ProjectView::Disk(&args.common.cwd),
&api_client,
&accessible,
selected,
)
.await;
if let Some(detail) = unsettled {
if !quiet {
eprintln!("Warning: {detail}");
}
narrow_warnings.push((
socket_patch_core::hosted::gem_fixes::GEM_FIX_UNSETTLED.to_string(),
detail,
));
}
settled
};

// The candidates can hold several patches per package and the pick
// was made without the user (paid auto-pick, `--yes`, non-TTY): say
// which will be installed. A menu pick is not echoed back, and paid
Expand Down Expand Up @@ -1936,7 +1979,26 @@ pub async fn run(args: GetArgs) -> i32 {
// engines. Hosted runs and agent previews need the same narrowing here.
let agent_preview = args.common.dry_run && mode == super::scan::ScanMode::Agent;
let selected = if agent_preview || mode == super::scan::ScanMode::Hosted {
let (selected, variant_warnings, _) = filter_to_installed_releases(
// A hosted run pins what the project's Bundler lock names, on
// every platform it names: the patches of a gem the lock names go
// by its builds (each platform gem has a patch of its own), not by
// the one copy installed on this machine.
let (locked_gems, selected) = if mode == super::scan::ScanMode::Hosted {
let view =
socket_patch_core::vendor::lock_inventory::ProjectView::Disk(&args.common.cwd);
let lock = socket_patch_core::hosted::gem_fixes::GemLock::read(
&view,
selected.iter().map(|patch| patch.purl.as_str()),
)
.await;
let (gems, rest): (Vec<_>, Vec<_>) = selected
.into_iter()
.partition(|patch| lock.names_gem(&patch.purl));
(lock.narrow_selected(gems), rest)
} else {
(Vec::new(), selected)
};
let (mut selected, variant_warnings, _) = filter_to_installed_releases(
&selected,
args.all_releases,
&args.common.crawler_options(),
Expand All @@ -1949,6 +2011,10 @@ pub async fn run(args: GetArgs) -> i32 {
.into_iter()
.map(|w| (RELEASE_NARROWING.to_string(), w)),
);
if !locked_gems.is_empty() {
selected.extend(locked_gems);
crate::commands::agent_download::sort_by_purl(&mut selected);
}
selected
} else {
selected
Expand Down Expand Up @@ -2491,6 +2557,33 @@ fn get_download_params(args: &GetArgs, save_only: bool, persist_blobs: bool) ->
}
}

/// What `get <uuid>` selects for `patch` in hosted and vendored mode: the
/// patch itself and, for a gem the project's Bundler lock names as platform
/// builds, the other patches of its fix that the lock has a build for
/// ([`socket_patch_core::hosted::gem_fixes::fix_siblings`]). Asking for one
/// patch of such a gem is asking for its fix: a run that changes what
/// Bundler installs moves every locked build or none.
async fn with_gem_fix_builds(
cwd: &Path,
api_client: &ApiClient,
use_public_proxy: bool,
patch: &PatchResponse,
) -> Vec<PatchSearchResult> {
let mut selected = vec![search_result_from_response(patch)];
selected.extend(
socket_patch_core::hosted::gem_fixes::fix_siblings(
&socket_patch_core::vendor::lock_inventory::ProjectView::Disk(cwd),
api_client,
use_public_proxy,
&patch.purl,
&patch.uuid,
)
.await,
);
crate::commands::agent_download::sort_by_purl(&mut selected);
selected
}

/// `get … --mode hosted`: hand the selected (purl, uuid) pairs to scan's
/// hosted engine ([`super::scan::boxed_run_redirect_selected`]) — lockfile
/// rewrite only, no manifest, no blobs, no ledger — so the on-disk result
Expand Down
55 changes: 50 additions & 5 deletions crates/socket-patch-cli/src/commands/scan/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ pub(super) async fn preverify_vendor_baselines<W: std::io::Write>(
use socket_patch_core::crawlers::gradle_cache;
use socket_patch_core::manifest::schema::PatchFileInfo;
use socket_patch_core::patch::apply::{verify_file_patch, VerifyStatus};
use socket_patch_core::vendor::lookup_entry;

let mut mismatched: HashSet<String> = HashSet::new();
let mut views: HashMap<String, PatchResponse> = HashMap::new();
Expand All @@ -390,13 +389,18 @@ pub(super) async fn preverify_vendor_baselines<W: std::io::Write>(
return None;
}
let pkg = crawled.iter().find(|c| PurlKey::same(&c.purl, base))?;
// A platform gem's patch says what that platform's gem holds:
// the one copy installed here is another platform's for every
// other build the lock names, and differs from them by design.
if !gem_patch_is_of_copy(&patch.purl, &pkg.path) {
return None;
}
// The same predicate as the download phase's ledger
// idempotency skip: its no-fetch set and this one must be
// the same set.
let embedded = vendor
.and_then(|entries| lookup_entry(entries, &patch.purl))
.filter(|e| e.detached && e.uuid == patch.uuid)
.and_then(|e| e.record.as_ref());
let embedded = vendor.and_then(|entries| {
socket_patch_core::vendor::detached_record_at(entries, &patch.purl, &patch.uuid)
});
Some((pkg, embedded))
})
.collect();
Expand Down Expand Up @@ -1041,8 +1045,49 @@ pub(crate) async fn nested_project_skips(
(skipped, warnings)
}

/// Whether the installed copy at `copy` is the gem that the patch purl
/// `patch_purl` is a patch of. Always for a patch of a ruby-platform gem
/// (and of any other ecosystem); for a platform gem's patch
/// (`?platform=<platform>`), only a copy installed as that platform's gem
/// (`<name>-<version>-<platform>`).
fn gem_patch_is_of_copy(patch_purl: &str, copy: &std::path::Path) -> bool {
let Some(platform) = socket_patch_core::utils::purl::gem_platform_qualifier(patch_purl) else {
return true;
};
let Some(leaf) = copy.file_name().and_then(|leaf| leaf.to_str()) else {
return false;
};
// `None` for a purl that is no gem's: its qualifier is not ours to read.
match socket_patch_core::formats::gem::GemBuild::ruby(patch_purl) {
None => true,
Some(_) => socket_patch_core::formats::gem::GemBuild::of_leaf(patch_purl, leaf)
.is_some_and(|build| build.platform.as_deref() == Some(platform)),
}
}

#[cfg(test)]
mod tests {
/// A platform gem's patch is compared only with a copy of its own
/// platform: on an x86_64 machine the aarch64 patch of the same lock
/// has no copy here, and must not be held against the x86_64 one.
#[test]
fn a_platform_gems_patch_is_compared_with_its_own_platform_only() {
let copy = |leaf: &str| std::path::PathBuf::from("/home/gems").join(leaf);
let x86 = copy("nokogiri-1.18.10-x86_64-linux-gnu");
let ruby = copy("nokogiri-1.18.10");
let patch = |platform: &str| format!("pkg:gem/nokogiri@1.18.10?platform={platform}");
assert!(gem_patch_is_of_copy(&patch("x86_64-linux-gnu"), &x86));
assert!(!gem_patch_is_of_copy(&patch("aarch64-linux-gnu"), &x86));
assert!(!gem_patch_is_of_copy(&patch("x86_64-linux-gnu"), &ruby));
// The ruby gem's patch, and any other ecosystem's, as before.
assert!(gem_patch_is_of_copy(&patch("ruby"), &x86));
assert!(gem_patch_is_of_copy("pkg:gem/nokogiri@1.18.10", &ruby));
assert!(gem_patch_is_of_copy(
"pkg:pypi/numpy@2.4.4?platform=aarch64-linux-gnu",
&copy("numpy")
));
}

use super::*;
use socket_patch_core::api::types::BatchPatchInfo;
use socket_patch_core::ledgers::merge_ledger_records_for_updates;
Expand Down
Loading
Loading