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
4 changes: 2 additions & 2 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

93 changes: 88 additions & 5 deletions crates/socket-patch-cli/src/commands/agent_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,9 @@ pub(crate) fn selection_pairs(selected: &[PatchSearchResult]) -> Vec<(&str, &str
/// Every refusal the vendored download phase raises before it fetches a
/// view: [`vendor_refusals`] over the selection, plus a hosted gem the
/// takeover would refuse (#775, [`gem_takeover_refusals_for`]), refused
/// here with the takeover's code instead of after its fetch. The download
/// here with the takeover's code instead of after its fetch (unless its
/// pin is the selected patch and the gem is installed: the vendor loop then
/// judges, with the view, whether every copy already holds it). The download
/// phase only runs online (`--offline` refuses `get` and `scan` before it),
/// so the gem's dry-run restore may resolve the registry entry. `prior` is
/// scan's npm crawl, when the caller has it (the installed-copy lookup
Expand All @@ -1191,6 +1193,53 @@ pub(crate) async fn vendored_download_refusals(
ledger: LedgerLoad<'_>,
prior: Option<&crate::ecosystem_dispatch::NpmCrawlSnapshot>,
) -> VendorRefusals {
vendored_download_refusals_and_deferred(params, selected, ledger, prior)
.await
.0
}

/// The gem takeover refusals [`vendored_download_refusals`] leaves to the
/// patch view, keyed by the selected purl: the refusal, and the hosted
/// pin that already holds the selected patch.
pub(crate) type ViewJudgedRefusals = HashMap<
String,
(
VendorRefusal,
socket_patch_core::patch::redirect::upstream::HostedPin,
),
>;

/// A refusal [`vendored_download_refusals`] left to the patch view,
/// settled with `patch`, that view: the skip of a gem that already holds
/// the patch through `pin` in every installed copy, or the refusal
/// itself.
pub(crate) async fn settle_view_judged_refusal(
common: &GlobalArgs,
patch: &PatchResponse,
refusal: &VendorRefusal,
pin: &socket_patch_core::patch::redirect::upstream::HostedPin,
) -> VendorRefusal {
let (_, record) = socket_patch_core::manifest::records::record_from_patch_response(patch);
let (code, detail) = crate::commands::vendor::settle_gem_refusal(
common,
&patch.purl,
&record,
Some(pin),
refusal.code,
refusal.detail.clone(),
)
.await;
VendorRefusal { code, detail }
}

/// [`vendored_download_refusals`], with the refusals it left to the patch
/// view: `get <uuid>` holds that view already and settles them itself.
pub(crate) async fn vendored_download_refusals_and_deferred(
params: &DownloadParams,
selected: &[PatchSearchResult],
ledger: LedgerLoad<'_>,
prior: Option<&crate::ecosystem_dispatch::NpmCrawlSnapshot>,
) -> (VendorRefusals, ViewJudgedRefusals) {
let cwd = params.cwd.as_path();
let origins = crate::commands::hosted_unwind::patch_server_origins_of(
params.patch_server_url.as_deref(),
Expand Down Expand Up @@ -1230,12 +1279,46 @@ pub(crate) async fn vendored_download_refusals(
origins,
)
.await;
// A gem refused for its declaration whose pin is the selected patch
// and that is installed may hold that patch in every installed copy,
// which only the patch view can tell: its view is fetched, and the
// vendor loop raises this refusal or the skip of a gem already
// patched in hosted mode (#2395). Any other gem is refused here,
// before any fetch.
let pinned_at_selection: Vec<String> = gem
.iter()
.filter(|(purl, (code, _))| {
socket_patch_core::vendor::gem::refusal_names_another_mode(code)
&& selected.iter().any(|sr| {
sr.purl == **purl
&& pins
.iter()
.any(|pin| pin.uuid == sr.uuid && PurlKey::same(&pin.purl, purl))
})
})
.map(|(purl, _)| purl.clone())
.collect();
let installed_at_selection = match pinned_at_selection.is_empty() {
true => std::collections::HashSet::new(),
false => installed(pinned_at_selection).await,
};
let mut judged_by_view = ViewJudgedRefusals::new();
for (purl, (code, detail)) in gem {
refusals
.entry(purl)
.or_insert(VendorRefusal { code, detail });
let refusal = VendorRefusal { code, detail };
let pin = installed_at_selection
.contains(&purl)
.then(|| pins.iter().find(|pin| PurlKey::same(&pin.purl, &purl)))
.flatten();
match pin {
Some(pin) => {
judged_by_view.insert(purl, (refusal, pin.clone()));
}
None => {
refusals.entry(purl).or_insert(refusal);
}
}
}
refusals
(refusals, judged_by_view)
}

/// The project's hosted pins: the purls whose vendored run is a hosted →
Expand Down
26 changes: 22 additions & 4 deletions crates/socket-patch-cli/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use crate::commands::agent_download::{
decide_patch_action, download_patch_records_preflighted, download_patch_records_reusing,
filter_to_installed_releases, max_vuln_severity, nested_apply_args,
next_steps as agent_next_steps, patch_event_metadata, record_apply_outcome, recorded_purls,
report_lock_failure, roll_back_replaced_patches, run_nested_apply, run_outcome, tag_mode,
unwind_new_blobs, vendored_download_refusals, warn_if_manifest_gitignored,
report_lock_failure, roll_back_replaced_patches, run_nested_apply, run_outcome,
settle_view_judged_refusal, tag_mode, unwind_new_blobs,
vendored_download_refusals_and_deferred, warn_if_manifest_gitignored,
warn_on_vendored_uuid_drift, write_all_patch_blobs, DetachedDownload, PatchAction,
ALREADY_IN_MANIFEST, BLOB_WRITE_FAILED, RELEASE_NARROWING, REPLACED_PATCH_NOT_RESTORED,
REPLACED_ROLLED_BACK_UNRECORDED,
Expand Down Expand Up @@ -2603,14 +2604,31 @@ async fn run_get_vendored(
// Human: `Error (<code>): <detail>` on stderr — an error, so it is
// exempt from `--silent` like every other `Error (…)` line here.
let vendor_state = load_state(&args.common.project_root()).await;
let refusals = Box::pin(vendored_download_refusals(
let (refusals, judged_by_view) = Box::pin(vendored_download_refusals_and_deferred(
&params,
selected,
vendor_state.as_ref().map(|s| &s.entries),
None,
))
.await;
if let Some(VendorRefusal { code, detail }) = refusals.get(&patch.purl) {
// A hosted gem refused for its declaration, pinned at this patch
// and installed: the view in hand tells whether every installed
// copy holds the patch (the skip of a gem already patched in
// hosted mode) or the refusal stands, here as well (#2395).
let settled = match (refusals.get(&patch.purl), judged_by_view.get(&patch.purl)) {
(Some(refusal), _) => Some(refusal.clone()),
(None, Some((refusal, pin))) => Some(
Box::pin(settle_view_judged_refusal(
&args.common,
patch,
refusal,
pin,
))
.await,
),
(None, None) => None,
};
if let Some(VendorRefusal { code, detail }) = settled.as_ref() {
// An expected skip (a gem version this project doesn't lock)
// is not an error: the same `skipped` event and `[skip]` line
// as the search path and `scan`, exit 0, nothing written.
Expand Down
35 changes: 26 additions & 9 deletions crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,9 @@ async fn keep_older_vendored_patch(
}

/// The mode in which the gem `candidate` already holds `record`, the
/// patch the run selected for it, for a gem the vendored backend refused
/// for its native extension: `hosted` when the lock still pins that patch
/// patch the run selected for it, for a gem vendored mode refused for
/// its native extension or its declaration: `hosted` when the lock still
/// pins that patch
/// (`live_pin`, the candidate's hosted pin unless this run restored its
/// upstream entry), `agent` when the manifest records it. Either way
/// every installed copy of the gem must hold the patch, as `vex` and
Expand Down Expand Up @@ -270,19 +271,20 @@ async fn gem_patch_held_in(
}
}

/// A backend refusal as the run reports it: a gem refused for its native
/// extension that already holds the selected patch in hosted or agent
/// mode is the skip [`vendor::gem::GEM_PATCHED_IN_OTHER_MODE`] (#2269);
/// every other refusal is itself.
async fn settle_gem_refusal(
/// A gem refusal as the run reports it: a gem refused for its native
/// extension or for its Gemfile declaration, which already holds the
/// selected patch in hosted or agent mode, is the skip
/// [`vendor::gem::GEM_PATCHED_IN_OTHER_MODE`] (#2269, #2395); every other
/// refusal is itself.
pub(crate) async fn settle_gem_refusal(
common: &GlobalArgs,
candidate: &str,
record: &PatchRecord,
live_pin: Option<&HostedPin>,
code: &'static str,
detail: String,
) -> (&'static str, String) {
if code != vendor::gem::NATIVE_EXTENSIONS_UNSUPPORTED {
if !vendor::gem::refusal_names_another_mode(code) {
return (code, detail);
}
match gem_patch_held_in(common, candidate, record, live_pin).await {
Expand All @@ -292,7 +294,7 @@ async fn settle_gem_refusal(
.map_or(&*purl, |(name, _)| name);
(
vendor::gem::GEM_PATCHED_IN_OTHER_MODE,
vendor::gem::patched_in_other_mode_detail(name, mode, &record.uuid),
vendor::gem::patched_in_other_mode_detail(code, name, mode, &record.uuid),
)
}
None => (code, detail),
Expand Down Expand Up @@ -4061,6 +4063,21 @@ pub(crate) async fn vendor_records_reusing(
let refusal =
gem_takeover_refusal(&common.cwd, candidate, pin, &restore_opts).await;
if let Some((code, detail)) = refusal {
// The pin is still live here: a gem that holds
// the selected patch through it is a skip (#2395).
let (code, detail) =
settle_gem_refusal(common, candidate, record, live_pin, code, detail)
.await;
if refusal_is_benign(code) {
if !common.silent && !common.json {
eprintln!("Skipping {}: {detail}", normalize_purl(candidate));
}
env.record(
PatchEvent::new(PatchAction::Skipped, candidate.clone())
.with_reason(code, detail),
);
continue;
}
has_errors = true;
env.record(
PatchEvent::new(PatchAction::Failed, candidate.clone())
Expand Down
6 changes: 4 additions & 2 deletions crates/socket-patch-cli/tests/e2e_redirect_gem_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3519,8 +3519,10 @@ async fn gem_hosted_group_block_pin_survives_a_refused_vendored_takeover() {
vendor_takeover_keeps_the_hosted_group_pin(&fx, command, selector, dry_run);
// The refusal is known before the download phase: a wet scan
// never fetches the patch view (nor its files) of a gem it
// cannot vendor. `get <uuid>` fetches the view once to resolve
// its identifier, and that is all it fetches.
// cannot vendor and that is not installed here (an installed
// copy is judged by the view: it may hold the pinned patch
// already). `get <uuid>` fetches the view once to resolve its
// identifier, and that is all it fetches.
let fetched = view_requests(&fx).await - views;
let allowed = usize::from(command == "get");
assert!(
Expand Down
Loading
Loading