Skip to content

Commit b69fc03

Browse files
Read the governing go.work in Go discovery (#2011)
* Read the governing go.work in Go discovery Go discovery read only a go.work at the project root and only members inside the root. With GOWORK unset it ignored an ancestor go.work and read the root go.mod in module mode, so in a member of a parent workspace it could attest a go.mod replace that the workspace overrides, doesn't `use`, or makes inert by version selection. Hosted and vendored modes resolve the workspace go itself would use and refuse to write such a replace. On disk, discovery now loads the governing workspace with the same go_workspace::load_with loader hosted mode uses: GOWORK (environment, then the go env -w file), else the nearest go.work at or above the root, with `use` members anywhere. Out-of-root members' requires and replaces now count toward selection and conflicts. In-root members are still re-read through the view, so recognition and parse diagnostics stay the same. A wired Socket replace that this workspace keeps from the build, at exactly the points where hosted mode's host_conflict check refuses, stays a ref (list, rollback and remove still find it) but is marked unattested with the new UnattestedKind::GoWorkspaceOverride. The same applies to a replace in the root go.work while GOWORK makes go build another one. vex_sources reports these under the existing `go_workspace_override` reason, the one vex's own go.mod gate already uses. A replace that workspace selection makes inert is still diagnosed, as in module mode. A GOWORK that go rejects still withholds every ref as patched_ref_invalid. In memory there is no directory to search from and no environment the project builds under. The root go.work and its in-root members are the workspace there, as the in-memory hosted engine reads them, and the same conflict gate applies. GOWORK keeps its old effect in memory and GOFLAGS is not read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep Go replaces the workspace ignores even when they look inert Version-selection inertness in replaced_purl, and the hosted selection checks, ran before Workspace::bypass. A Socket replace that the governing go.work also refuses (not used, overridden by a go.work replace, member conflict) was dropped as inert with a diagnostic instead of being kept as a GoWorkspaceOverride ref, so list, rollback and remove could not find wiring that re-running scan or vendor would never clear. Compute the bypass first and skip the inertness checks for a bypassed replace; it stays a ref and is reported unattested. Found by Cursor Bugbot on #2011. Co-Authored-By: Claude <noreply@anthropic.com> * Address code review findings on Go workspace discovery This fixes the confirmed /code-review high findings on #2011. Each fix has a test that fails on the previous commit. - A Go workspace mark now applies to one file only. The new UnattestedKind::file_scoped is true only for GoWorkspaceOverride. vex_sources drops the bypassed ref from the candidate's basis and vetoes the patch only when no live ref of it remains. wiring_conflicts ignores bypassed refs. So a root go.work replace that go never reads no longer withholds the root go.mod's mirror of it. A leftover member pin that Socket's own go.work replace overrides no longer turns into a wiring conflict that also withholds the go.work patch, which is what vex.rs's WorkHosted exemption intends. - A replace of a module that is itself a `use` member is diagnosed as inert again, both on disk and in memory, hosted and vendored. This check now runs before the workspace bypass. - SumPins now also reads the sum files the loader already read: the governing go.work.sum and each out-of-root member's go.sum. They are used only when they lie inside the git repository. - In memory, workspace conflicts are evaluated in go_workspace::PathSpace::Memory. Paths are compared lexically against the view's files and never against the filesystem or the working directory. conflict/work_conflict delegate to the new *_in variants, so disk behavior is unchanged. - The snapshot probe list is now built by one shared helper (workspace_probes), spelled the way resolve_with spells it. - A root go.work that go doesn't read is no longer judged unparseable, and its wiring stays an unattested ref. - The go_workspace_override omission phrase now covers every case. - read_loaded_workspace uses members_within. The view re-reads only the in-root members' go.mod, which recognition and parse diagnostics need. The duplicate go.sum and go.work.sum reads are gone. - Tests that use a nested project now create a .git, so they keep working once reads are bounded to the repository root (#2025). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep a root go.mod pin of another patch a Go wiring conflict 2984629 dropped every workspace-bypassed ref from wiring_conflicts, so a leftover member pin that Socket's go.work replace overrides would not withhold the go.work patch. That also caught the root go.mod. A GOWORK=off build from the root reads the root go.mod, so a different patch there still disagrees with go.work about what the build links. The behavior is the same reasoning ReplaceHost::mirrors_root_go_mod uses. e2e_vex_lockfile golang_go_mod_and_go_work_disagreeing_attest_neither went red in coverage (2): it got go_workspace_override plus go_version_not_selected skips, not one wiring_conflict skip. Count bypassed refs in the root go.mod as wiring again. Member go.mod pins are still exempt, as a_bypassed_leftover_pin_is_no_wiring_conflict requires. Co-Authored-By: Claude <noreply@anthropic.com> * Keep inert Go member replaces as refs; recognize stale sums Second review round on Go workspace discovery. - A Socket replace of a module that is itself a `use` member (every replace of it is inert, and the writers refuse it with `WorkspaceConflict::MainModule`, which a re-run never clears) was dropped with a diagnostic, so rollback, remove and list lost wiring still in the file. It stays a ref, marked `GoWorkspaceOverride` with the same "is inert" detail (owner decision 4). - go.sum / member sums / go.work.sum were read only when a hosted ref existed, so a sum left by a reverted patch was no longer recognized and a dead ledger claim could be re-derived from its raw text (rule 11). They are recognized either way. - The plan both attested a purl (the live go.work patch) and reported it skipped for a lockfile-only candidate whose only ref is a stale, bypassed leftover pin of another patch. That candidate is now dropped with a note when another candidate attests the purl. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6d5fe02 commit b69fc03

7 files changed

Lines changed: 1175 additions & 160 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 5 additions & 3 deletions
Large diffs are not rendered by default.

‎crates/socket-patch-cli/src/commands/vex.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use socket_patch_core::vex::{
3131

3232
use crate::args::{apply_env_toggles, parse_bool_flag, GlobalArgs};
3333
use crate::commands::vex_sources::{
34-
self, Plan, RunApiClient, Sources, RECORD_MISMATCH, RECORD_UNAVAILABLE, REDIRECT_UNWIRED,
35-
VENDOR_UNWIRED, WIRING_CONFLICT,
34+
self, Plan, RunApiClient, Sources, GO_WORKSPACE_OVERRIDE, RECORD_MISMATCH, RECORD_UNAVAILABLE,
35+
REDIRECT_UNWIRED, VENDOR_UNWIRED, WIRING_CONFLICT,
3636
};
3737
use crate::ecosystem_dispatch::{find_manifest_package_copies_reusing, JvmScope};
3838
use crate::json_envelope::{Command, Envelope, EnvelopeError, PatchAction, PatchEvent, RunWarning};
@@ -1953,10 +1953,6 @@ fn join_and(items: &[&str]) -> String {
19531953
}
19541954
}
19551955

1956-
/// Omission tag for a Go patch whose go.mod replace the `go.work` in effect
1957-
/// overrides, ignores or rejects (#393, #458, #531).
1958-
const GO_WORKSPACE_OVERRIDE: &str = "go_workspace_override";
1959-
19601956
/// Omission tag for a global Go patch while a tool built from the unpatched
19611957
/// module with `go install` remains (#422).
19621958
const GO_INSTALLED_TOOL_STALE: &str = socket_patch_core::utils::go_installed_tools::STALE_WARNING;
@@ -2125,8 +2121,9 @@ fn omission_phrase(reason: &str) -> &'static str {
21252121
package any more"
21262122
}
21272123
GO_WORKSPACE_OVERRIDE => {
2128-
"the go.work in effect overrides, ignores or conflicts with its go.mod replace, \
2129-
so the build does not link the patched module"
2124+
"the Go workspace in effect keeps its replace from the build (overrides, ignores \
2125+
or rejects it, or go builds from a go.work other than the one holding it), so \
2126+
the build does not link the patched module; the run warning names which"
21302127
}
21312128
GO_INSTALLED_TOOL_STALE => {
21322129
"a tool installed with `go install` was built from the unpatched module and keeps \

‎crates/socket-patch-cli/src/commands/vex_sources.rs‎

Lines changed: 170 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ use socket_patch_core::vendor::state::{VendorArtifact, VendorEntry, VendorState}
8484
use socket_patch_core::vex::discover::{
8585
vendor_ref, Discovery, LedgerLiveness, PatchedRef, WiringMode,
8686
};
87-
use socket_patch_core::vex::{FailedPatch, UnattestedKind};
87+
use socket_patch_core::vex::{FailedPatch, Unattested, UnattestedKind};
8888

8989
use crate::args::GlobalArgs;
9090
use crate::ui::plural;
@@ -232,6 +232,12 @@ const NOTE_MAVEN_REACTOR_ROOT: &str = "vex_maven_reactor_root";
232232
/// classifier copy at another version, which the pin does not reach
233233
/// (`vex::Unattested`).
234234
const NOTE_MAVEN_CLASSIFIER_UNPATCHED: &str = "vex_maven_classifier_unpatched";
235+
/// Omission tag for a Go patch whose replace the `go.work` in effect
236+
/// overrides, ignores or rejects (#393, #458, #531): the reason of `vex`'s
237+
/// own go.mod gate, and the note of a discovered hosted or vendored ref the
238+
/// governing workspace keeps from the build (`vex::Unattested`,
239+
/// `UnattestedKind::GoWorkspaceOverride`).
240+
pub(crate) const GO_WORKSPACE_OVERRIDE: &str = "go_workspace_override";
235241

236242
/// The omission tag of an [`Unattested`](socket_patch_core::vex::Unattested)
237243
/// ref, and the remedy its note ends with.
@@ -268,6 +274,11 @@ fn unattested_note(kind: UnattestedKind) -> (&'static str, &'static str) {
268274
"not attested while that classifier copy resolves an unpatched version; the \
269275
wiring itself is intact, so re-running `scan` does not change this",
270276
),
277+
UnattestedKind::GoWorkspaceOverride => (
278+
GO_WORKSPACE_OVERRIDE,
279+
"not attested while the Go workspace in effect keeps it from the build; the wiring \
280+
itself is intact, so re-running `scan` / `vendor` does not change this",
281+
),
271282
}
272283
}
273284

@@ -421,15 +432,49 @@ pub(crate) async fn plan(
421432
// package-lock.json twin, which npm >= 12 ignores): the ref keeps
422433
// rollback, remove, list and the ledgers' liveness working, and the
423434
// patch is omitted.
424-
cands.retain(|c| {
435+
// A file-scoped mark (a Go replace the workspace keeps from the build)
436+
// bypasses only its own file's ref: another file's live ref of the
437+
// same patch attests on its own basis, without the bypassed one.
438+
// Purls some candidate attests through a ref no mark bypasses: a
439+
// lockfile-only candidate left with nothing but file-scoped bypassed
440+
// refs of the same purl (a stale pin the workspace keeps from the
441+
// build) is then dropped, never both attested and reported skipped.
442+
let live_purls: Vec<String> = cands
443+
.iter()
444+
.filter(|c| c.discovered.iter().any(|r| !bypassed_ref(&discovery, r)))
445+
.map(|c| canonical_base_purl(&c.key))
446+
.collect();
447+
cands.retain_mut(|c| {
425448
let pkg = canonical_base_purl(&c.key);
426-
let Some(u) = discovery
449+
let marks: Vec<&Unattested> = discovery
427450
.unattested
428451
.iter()
429-
.find(|u| u.uuid == c.uuid && PurlKey::same(&u.purl, &pkg))
430-
else {
452+
.filter(|u| u.uuid == c.uuid && PurlKey::same(&u.purl, &pkg))
453+
.collect();
454+
let Some(&first) = marks.first() else {
431455
return true;
432456
};
457+
let u = match marks.iter().find(|u| !u.kind.file_scoped()) {
458+
Some(u) => *u,
459+
None => {
460+
c.discovered.retain(|r| !bypassed_ref(&discovery, r));
461+
if !c.discovered.is_empty() {
462+
return true;
463+
}
464+
if c.lockfile_only && live_purls.iter().any(|p| PurlKey::same(p, &pkg)) {
465+
notes.push(PlanNote {
466+
code: unattested_note(first.kind).0,
467+
detail: format!(
468+
"{}: patch {}'s wiring is bypassed ({}); another patch's live \
469+
wiring of the same package is attested",
470+
c.key, c.uuid, first.detail
471+
),
472+
});
473+
return false;
474+
}
475+
first
476+
}
477+
};
433478
let (code, remedy) = unattested_note(u.kind);
434479
gated.push(failed(&c.key, code));
435480
notes.push(PlanNote {
@@ -717,7 +762,15 @@ fn build_candidates(
717762
/// disagreeing wiring (`<uuid> (<file>), …`) for the note.
718763
fn wiring_conflicts(discovery: &Discovery) -> BTreeMap<String, String> {
719764
let mut by_pkg: BTreeMap<&str, BTreeMap<&str, BTreeSet<String>>> = BTreeMap::new();
720-
for r in &discovery.refs {
765+
// A ref whose file the build never reads wires nothing it installs.
766+
// The root go.mod is the exception: a `GOWORK=off` build from the root
767+
// reads it (cf. `ReplaceHost::mirrors_root_go_mod`), so a different
768+
// patch there still disagrees with the go.work that overrides it.
769+
for r in discovery
770+
.refs
771+
.iter()
772+
.filter(|r| r.source_file == std::path::Path::new("go.mod") || !bypassed_ref(discovery, r))
773+
{
721774
by_pkg
722775
.entry(r.purl.as_str())
723776
.or_default()
@@ -743,6 +796,17 @@ fn wiring_conflicts(discovery: &Discovery) -> BTreeMap<String, String> {
743796
.collect()
744797
}
745798

799+
/// Whether a file-scoped [`Unattested`] mark ([`UnattestedKind::file_scoped`])
800+
/// covers `r`: the same patch and package, in the same file.
801+
fn bypassed_ref(discovery: &Discovery, r: &PatchedRef) -> bool {
802+
discovery.unattested.iter().any(|u| {
803+
u.kind.file_scoped()
804+
&& u.uuid == r.uuid
805+
&& u.file == r.source_file
806+
&& PurlKey::same(&u.purl, &canonical_base_purl(&r.purl))
807+
})
808+
}
809+
746810
/// Attach every lockfile reference (except those of `conflicts`' packages,
747811
/// already gated) to the candidate(s) for its package and patch; a package the lockfile wires to a patch no candidate records gets
748812
/// a new lockfile-only candidate, and a candidate for a wired package whose
@@ -1762,6 +1826,106 @@ mod tests {
17621826
assert!(live.hosted.contains_key(PURL));
17631827
}
17641828

1829+
/// A hosted Go ref of `uuid` in `file`, and the Go workspace mark on it.
1830+
fn go_ref(purl: &str, uuid: &str, file: &str) -> PatchedRef {
1831+
PatchedRef::hosted(
1832+
purl.to_string(),
1833+
uuid.to_string(),
1834+
file,
1835+
None,
1836+
Some(LockIntegrity::GoH1("h1:x".into())),
1837+
true,
1838+
)
1839+
}
1840+
1841+
fn go_bypass(purl: &str, uuid: &str, file: &str) -> socket_patch_core::vex::Unattested {
1842+
socket_patch_core::vex::Unattested {
1843+
purl: purl.into(),
1844+
uuid: uuid.into(),
1845+
file: file.into(),
1846+
detail: format!("go builds the workspace of ../ci/go.work, never this {file}"),
1847+
kind: UnattestedKind::GoWorkspaceOverride,
1848+
}
1849+
}
1850+
1851+
/// Review finding 1: a Go workspace mark bypasses only its own file. A
1852+
/// root go.work replace go never reads does not withhold the patch the
1853+
/// root go.mod's mirror of it wires, which attests on its own basis;
1854+
/// alone, the bypassed ref is omitted with `go_workspace_override`.
1855+
#[tokio::test]
1856+
async fn a_go_workspace_mark_withholds_only_its_own_file() {
1857+
const PURL: &str = "pkg:golang/github.com/foo/bar@v1.4.2";
1858+
let tmp = tempfile::tempdir().unwrap();
1859+
let mut redirect = RedirectState::new();
1860+
redirect.records.insert(PURL.into(), record(U1));
1861+
let sources = |discovery: Discovery| Sources {
1862+
manifest: PatchManifest::new(),
1863+
vendor: VendorState::new(),
1864+
redirect: Some(redirect.clone()),
1865+
discovery,
1866+
};
1867+
let mut both = discovery(vec![
1868+
go_ref(PURL, U1, "go.work"),
1869+
go_ref(PURL, U1, "go.mod"),
1870+
]);
1871+
both.unattested.push(go_bypass(PURL, U1, "go.work"));
1872+
let live = plan(
1873+
&common(tmp.path()),
1874+
sources(both),
1875+
&[],
1876+
&RunApiClient::new(),
1877+
)
1878+
.await;
1879+
assert!(live.gated.is_empty(), "{:?}", live.gated);
1880+
let wiring = &live.hosted[PURL];
1881+
assert_eq!(wiring.refs.len(), 1, "{:?}", wiring.refs);
1882+
assert_eq!(wiring.refs[0].source_file, Path::new("go.mod"));
1883+
1884+
let mut alone = discovery(vec![go_ref(PURL, U1, "go.work")]);
1885+
alone.unattested.push(go_bypass(PURL, U1, "go.work"));
1886+
let gated = plan(
1887+
&common(tmp.path()),
1888+
sources(alone),
1889+
&[],
1890+
&RunApiClient::new(),
1891+
)
1892+
.await;
1893+
assert!(gated.view.patches.is_empty() && gated.hosted.is_empty());
1894+
assert_eq!(gated.gated, vec![failed(PURL, GO_WORKSPACE_OVERRIDE)]);
1895+
}
1896+
1897+
/// Review finding 4: Socket's own go.work replace overriding a leftover
1898+
/// member pin of another patch is what the build links (as `vex`'s
1899+
/// go.mod gate holds): the overridden pin is withheld, never a wiring
1900+
/// conflict that also withholds the go.work patch.
1901+
#[tokio::test]
1902+
async fn a_bypassed_leftover_pin_is_no_wiring_conflict() {
1903+
const PURL: &str = "pkg:golang/github.com/foo/bar@v1.4.2";
1904+
let tmp = tempfile::tempdir().unwrap();
1905+
let mut redirect = RedirectState::new();
1906+
redirect.records.insert(PURL.into(), record(U1));
1907+
let mut found = discovery(vec![
1908+
go_ref(PURL, U1, "go.work"),
1909+
go_ref(PURL, U2, "app/go.mod"),
1910+
]);
1911+
found.unattested.push(go_bypass(PURL, U2, "app/go.mod"));
1912+
let sources = Sources {
1913+
manifest: PatchManifest::new(),
1914+
vendor: VendorState::new(),
1915+
redirect: Some(redirect),
1916+
discovery: found,
1917+
};
1918+
let plan = plan(&common(tmp.path()), sources, &[], &RunApiClient::new()).await;
1919+
assert_eq!(plan.hosted[PURL].uuid, U1);
1920+
// Review on #2011 (round 2): the attested purl is never also
1921+
// reported skipped for its stale, bypassed leftover pin.
1922+
assert!(
1923+
!plan.gated.iter().any(|f| PurlKey::same(&f.purl, PURL)),
1924+
"{:?}",
1925+
plan.gated
1926+
);
1927+
}
1928+
17651929
/// Ledger-only records with no wiring anywhere are gated, except the
17661930
/// manifest-owned one, which falls back to agent-mode verification; an
17671931
/// in-run confirmed purl is live by construction.

0 commit comments

Comments
 (0)