Skip to content

Commit 04a1a9d

Browse files
committed
Keep go.mod as host for an unreadable in-memory root module
In memory, GoWorkspace::files counts a root go.mod that exists but has no text the rewriter holds (a link, an unreadable file), and conflict() reports such a root as "not a member" when go.work does not use it. The rewriter passes has_go_mod from its candidate texts, so replace_host saw no root module, chose go.work, and ran work_conflict: the replace went into go.work, where it patches the members but never the project. When the root is not a member, treat a root go.mod that the in-memory workspace lists as the root's module, as on disk, so go.mod hosts the replace and conflict() refuses it. Found by Bugbot on df219a6. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent df219a6 commit 04a1a9d

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎crates/socket-patch-core/src/vendor/go_workspace.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,12 @@ impl GoWorkspace {
799799
.members
800800
.iter()
801801
.any(|m| paths.key(&m.dir) != own && m.lists(module));
802+
// In memory a root go.mod can exist with no text the caller holds
803+
// (a link, an unreadable file): it is still the root's module, so a
804+
// root go.work doesn't `use` keeps go.mod and [`GoWorkspace::conflict`]
805+
// refuses it, as on disk.
806+
let root_has_go_mod = root_has_go_mod
807+
|| (self.files.is_some() && !root_is_member && paths.exists(&root.join("go.mod")));
802808
let shared = !root_has_go_mod || (root_is_member && (ours_here || listed_elsewhere));
803809
if shared {
804810
ReplaceHost::GoWork
@@ -2332,7 +2338,33 @@ mod tests {
23322338
),
23332339
"{root:?}"
23342340
);
2341+
// The rewriter holds no text for an unreadable root go.mod, but
2342+
// it is the root's module all the same: go.mod hosts the replace,
2343+
// so `conflict` (not a member) refuses it.
2344+
assert_eq!(
2345+
ws.replace_host(Path::new(""), false, DEP),
2346+
ReplaceHost::GoMod,
2347+
"{root:?}"
2348+
);
23352349
}
2350+
// A root module go.work uses still shares go.work's replace.
2351+
let used = "go 1.21
2352+
2353+
use (
2354+
.
2355+
./b
2356+
)
2357+
";
2358+
let mut reader = MapFiles::new(&[
2359+
("go.mod", None),
2360+
("go.work", Some(used)),
2361+
("b/go.mod", Some(b)),
2362+
]);
2363+
let (ws, _) = read_memory(used, &mut reader).await;
2364+
assert_eq!(
2365+
ws.replace_host(Path::new(""), false, DEP),
2366+
ReplaceHost::GoWork
2367+
);
23362368
// No go.mod at all: nowhere to go.
23372369
let mut reader = MapFiles::new(&[("go.work", Some(work))]);
23382370
let (ws, _) = read_memory(work, &mut reader).await;

0 commit comments

Comments
 (0)