fix(cli): honor local installation discovery precedence

This commit is contained in:
2026-08-15 22:29:55 +02:00
parent 66614fe8cf
commit 2c800cc521
4 changed files with 83 additions and 21 deletions
+13 -14
View File
@@ -41,8 +41,8 @@ func Resolve(explicit string, environment func(string) string, workingDirectory
return "", errors.New("installation discovery could not resolve the current directory")
}
candidates := make(map[string]string)
for directory := filepath.Clean(start); ; directory = filepath.Dir(directory) {
candidates := make(map[string]string)
for _, path := range discoveryCandidates(directory) {
if hasArtifactsComponent(path) {
continue
@@ -57,6 +57,17 @@ func Resolve(explicit string, environment func(string) string, workingDirectory
canonical := canonicalPath(path)
candidates[canonical] = path
}
paths := make([]string, 0, len(candidates))
for _, path := range candidates {
paths = append(paths, path)
}
sort.Strings(paths)
if len(paths) == 1 {
return paths[0], nil
}
if len(paths) > 1 {
return "", fmt.Errorf("multiple valid installation descriptors found: %s; use --installation <absolute-path>/%s", formatCandidates(paths, root.Path), installationFileName)
}
if directory == root.Path {
break
}
@@ -66,19 +77,7 @@ func Resolve(explicit string, environment func(string) string, workingDirectory
}
}
paths := make([]string, 0, len(candidates))
for _, path := range candidates {
paths = append(paths, path)
}
sort.Strings(paths)
switch len(paths) {
case 1:
return paths[0], nil
case 0:
return "", fmt.Errorf("installation descriptor not found from %s; run tht setup or use --installation <absolute-path>/%s", filepath.Clean(friendlyRoot), installationFileName)
default:
return "", fmt.Errorf("multiple valid installation descriptors found: %s; use --installation <absolute-path>/%s", formatCandidates(paths, root.Path), installationFileName)
}
return "", fmt.Errorf("installation descriptor not found from %s; run tht setup or use --installation <absolute-path>/%s", filepath.Clean(friendlyRoot), installationFileName)
}
func formatCandidates(paths []string, root string) string {
+52 -1
View File
@@ -64,6 +64,44 @@ func TestResolveFindsDeployDescriptorFromNestedDirectory(t *testing.T) {
}
}
func TestResolveUsesLocalCandidateBeforeAncestorCandidate(t *testing.T) {
root := t.TempDir()
writeDiscoverableProject(t, root)
ancestor := writeDiscoverableInstallation(t, root, filepath.Join(root, "deploy", "ancestor"))
localDirectory := filepath.Join(root, "frontend", "feature")
local := writeDiscoverableInstallation(t, root, filepath.Join(localDirectory, "deploy", "local"))
got, err := Resolve("", func(string) string { return "" }, localDirectory)
if err != nil {
t.Fatal(err)
}
want, err := filepath.EvalSymlinks(local)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("Resolve() = %q, want local %q before ancestor %q", got, want, ancestor)
}
}
func TestResolveDiscoversDescriptorFromLinkedWorktree(t *testing.T) {
root := t.TempDir()
writeDiscoverableLinkedWorktree(t, root)
want := writeDiscoverableInstallation(t, root, filepath.Join(root, "deploy", "worktree"))
got, err := Resolve("", func(string) string { return "" }, filepath.Join(root, "frontend"))
if err != nil {
t.Fatal(err)
}
canonicalWant, err := filepath.EvalSymlinks(want)
if err != nil {
t.Fatal(err)
}
if got != canonicalWant {
t.Fatalf("Resolve() = %q, want %q from linked worktree", got, canonicalWant)
}
}
func TestResolveIgnoresArtifactsAndRejectsAmbiguousValidCandidates(t *testing.T) {
root := t.TempDir()
writeDiscoverableProject(t, root)
@@ -112,13 +150,26 @@ func TestResolveDoesNotSearchAboveTheDiscoveredProjectRoot(t *testing.T) {
}
func writeDiscoverableProject(t *testing.T, root string) {
writeDiscoverableProjectWithGitMarker(t, root, false)
}
func writeDiscoverableLinkedWorktree(t *testing.T, root string) {
writeDiscoverableProjectWithGitMarker(t, root, true)
}
func writeDiscoverableProjectWithGitMarker(t *testing.T, root string, linkedWorktree bool) {
t.Helper()
for _, path := range []string{"backend", "frontend", "harness", "tools", "deploy"} {
if err := os.MkdirAll(filepath.Join(root, path), 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.MkdirAll(filepath.Join(root, ".git"), 0o755); err != nil {
gitPath := filepath.Join(root, ".git")
if linkedWorktree {
if err := os.WriteFile(gitPath, []byte("gitdir: /tmp/thothii-worktree.git\n"), 0o600); err != nil {
t.Fatal(err)
}
} else if err := os.MkdirAll(gitPath, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "compose.yaml"), []byte("services: {}\n"), 0o600); err != nil {