bugfix: find references, double timeout

Tai Groot created

Change summary

internal/agent/tools/diagnostics.go | 5 +----
internal/agent/tools/references.go  | 6 +++++-
internal/lsp/client.go              | 5 +++++
3 files changed, 11 insertions(+), 5 deletions(-)

Detailed changes

internal/agent/tools/diagnostics.go 🔗

@@ -73,16 +73,13 @@ func waitForLSPDiagnostics(
 		return
 	}
 
-	waitCtx, cancel := context.WithTimeout(ctx, timeout)
-	defer cancel()
-
 	var wg sync.WaitGroup
 	for client := range manager.Clients().Seq() {
 		if !client.HandlesFile(filepath) {
 			continue
 		}
 		wg.Go(func() {
-			client.WaitForDiagnostics(waitCtx, timeout)
+			client.WaitForDiagnostics(ctx, timeout)
 		})
 	}
 	wg.Wait()

internal/agent/tools/references.go 🔗

@@ -71,7 +71,11 @@ func NewReferencesTool(lspManager *lsp.Manager) fantasy.AgentTool {
 					continue
 				}
 				allLocations = append(allLocations, locations...)
-				// XXX: should we break here or look for all results?
+				// Once we have results, we're done - LSP returns all references
+				// for the symbol, not just from this file.
+				if len(locations) > 0 {
+					break
+				}
 			}
 
 			if len(allLocations) > 0 {

internal/lsp/client.go 🔗

@@ -533,6 +533,11 @@ func (c *Client) FindReferences(ctx context.Context, filepath string, line, char
 	if err := c.OpenFileOnDemand(ctx, filepath); err != nil {
 		return nil, err
 	}
+
+	// Add timeout to prevent hanging on slow LSP servers.
+	ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
+	defer cancel()
+
 	// NOTE: line and character should be 0-based.
 	// See: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
 	return c.client.FindReferences(ctx, filepath, line-1, character-1, includeDeclaration)