diff --git a/internal/server/execution_safety_test.go b/internal/server/execution_safety_test.go index f1b5840a..95dc4f27 100644 --- a/internal/server/execution_safety_test.go +++ b/internal/server/execution_safety_test.go @@ -151,3 +151,28 @@ func TestWorkspaceOverlapIncludesParentsDescendantsAndSymlinks(t *testing.T) { } } } + +// A hub folder holding separate repositories (the operator's ~/caplens with +// clones under repos/) must not block, nor be blocked by, a run inside one of +// those repositories; the same repository still overlaps itself. +func TestWorkspaceOverlapSeparatesNestedRepositories(t *testing.T) { + hub := t.TempDir() + repo := filepath.Join(hub, "repos", "fix-d") + inner := filepath.Join(repo, "src") + nested := filepath.Join(repo, "vendor", "lib") + for _, dir := range []string{filepath.Join(repo, ".git"), inner, filepath.Join(nested, ".git")} { + if err := os.MkdirAll(dir, 0700); err != nil { + t.Fatal(err) + } + } + for _, pair := range [][2]string{{hub, repo}, {repo, hub}, {hub, inner}, {repo, nested}} { + if workspacesOverlap(pair[0], pair[1]) { + t.Fatal("separate repositories falsely overlap", pair) + } + } + for _, pair := range [][2]string{{repo, repo}, {repo, inner}, {inner, repo}} { + if !workspacesOverlap(pair[0], pair[1]) { + t.Fatal("one repository does not overlap itself", pair) + } + } +} diff --git a/internal/server/runner.go b/internal/server/runner.go index 55c17d6d..6ea8e8e1 100644 --- a/internal/server/runner.go +++ b/internal/server/runner.go @@ -473,7 +473,7 @@ func (r *runner) checkWorkspaceFree(workspace string) error { func (r *runner) checkWorkspaceFreeExcept(workspace string, ownGate int64) error { for id, entry := range r.active { if workspacesOverlap(entry.workspace, workspace) { - return fmt.Errorf("run %d is already active in workspace %s; wait for it to finish or start this run in a different workspace (for example another checkout of the repository)", id, workspace) + return fmt.Errorf("run %d is already active in %s, which overlaps this run's workspace %s; wait for run %d to finish or use a different workspace (for example another checkout of the repository)", id, entry.workspace, workspace, id) } } check, cancel := context.WithTimeout(context.WithoutCancel(r.ctx), 5*time.Second) @@ -490,12 +490,41 @@ func (r *runner) checkWorkspaceFreeExcept(workspace string, ownGate int64) error return nil } +// workspacesOverlap reports whether two resolved workspaces are the same +// tree or nested in one another within the same repository. A folder that +// merely holds separate repositories (a hub such as ~/project with clones +// under repos/) does not overlap a run inside one of those repositories, and +// neither do two different repositories nested in one another: each run +// works in its own repository. func workspacesOverlap(a, b string) bool { within := func(parent, child string) bool { rel, err := filepath.Rel(parent, child) return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) } - return within(a, b) || within(b, a) + switch { + case filepath.Clean(a) == filepath.Clean(b): + return true + case within(a, b): + return repositoryRoot(b) == repositoryRoot(a) + case within(b, a): + return repositoryRoot(a) == repositoryRoot(b) + } + return false +} + +// repositoryRoot is the nearest directory at or above dir holding a .git +// entry (a repository or a linked worktree), or "" outside any repository. +func repositoryRoot(dir string) string { + for current := filepath.Clean(dir); ; { + if _, err := os.Lstat(filepath.Join(current, ".git")); err == nil { + return current + } + parent := filepath.Dir(current) + if parent == current { + return "" + } + current = parent + } } func (r *runner) getRun(id int64) (*protocol.RunDetail, error) {