Skip to content

Implement "find file references"#3277

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/fix-issue-3250
Draft

Implement "find file references"#3277
Copilot wants to merge 5 commits intomainfrom
copilot/fix-issue-3250

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 27, 2026

Implements the "find file references" feature (right-click a JS/TS file in VS Code explorer → Find File References), which finds all files that import or triple-slash-reference a given file.

The core challenge: getReferencesForNonModule was a stub, and the multi-project search logic required a document position (which file-level queries don't have).

Changes

Core algorithm (internal/ls/findallreferences.go)

  • Implement getReferencesForNonModule() using program.GetIncludeReasons() + FileIncludeReason.GetSpanInReferrer() to enumerate non-module references pointing to a file
  • Add GetFileReferences(ctx, uri, orchestrator) to LanguageService — dispatches to getReferencedSymbolsForModule for module files (has exports), or getReferencesForNonModule for script files; when a CrossProjectOrchestrator is provided, searches all initial projects and expands via GetProjectsLoadingProjectTree to find additional projects in the dependency tree, deduplicating results

Compiler helper (internal/compiler/fileInclude.go)

  • Add FileIncludeReason.GetSpanInReferrer(program) — exported method that returns the referring file name and text range for a file-include reason, with trivia-skipping matching TypeScript's behavior. The logic lives in ls via program.GetIncludeReasons() + this method.

LSP protocol (internal/lsp/lsproto/)

  • Add custom/findFileReferences request: params = FindFileReferencesParams{textDocument} (no position), result = Location[] | null

LSP server (internal/lsp/server.go)

  • Register handleFindFileReferences — uses getLanguageServiceAndCrossProjectOrchestrator (same pattern as registerMultiProjectReferenceRequestHandler) and delegates to GetFileReferences with the orchestrator, enabling full cross-project search including project dependency tree traversal

VS Code extension (_extension/)

  • Add Client.findFileReferences(uri) calling custom/findFileReferences
  • Register _typescript.findAllFileReferences command (the standard VS Code command hooked by the explorer context menu) that calls the above and shows results via editor.action.showReferences

Fourslash test infrastructure

  • Add VerifyBaselineFindFileReferences to the fourslash framework
  • Add manual tests TestFindFileReferences, TestFindFileReferencesNonModule, and TestGetFileReferences_deduplicate (cross-project deduplication)
  • Update convertFourslash.mts to support verify.baselineGetFileReferences(...) and fix directory traversal so server-subdirectory tests are reachable when filtering by basename; port getFileReferences1, getFileReferences2, getFileReferences_server1, and getFileReferences_server2 from the TypeScript fourslash suite

Side effect

Implementing getReferencesForNonModule also fixes findAllReferences on triple-slash /// <reference path/types/lib> nodes — previously they returned no span and omitted transitive referencing files (e.g. require("./b") in a .js file). The findAllReferencesTripleSlash submodule baseline is updated to match TypeScript's expected output, and the stale diff file is removed.

Original prompt

Fix this issue #3250

Created from VS Code.


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI and others added 2 commits March 27, 2026 21:59
Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/19d44b3e-7bd1-4dc3-8327-ef40363fd5f7

Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/19d44b3e-7bd1-4dc3-8327-ef40363fd5f7

Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix issue 3250 in Typescript Go integration Implement "find file references" Mar 27, 2026
Copilot AI requested a review from DanielRosenwasser March 27, 2026 22:15
Copy link
Copy Markdown
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update convertfourslash.mts to add any existing tests that need to be ported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 38e7195. Added baselineGetFileReferences support to convertFourslash.mts (parser + generator + interface + union type membership), and generated TestGetFileReferences1 and TestGetFileReferences2 from the existing TypeScript fourslash tests.

// that are recorded in the program's file-include-reasons map. These are references
// via triple-slash directives (/// <reference path="...">, /// <reference types="...">,
// /// <reference lib="...">), as opposed to module-symbol-based import references.
func (p *Program) GetNonModuleFileReferences(referencedFile *ast.SourceFile) []NonModuleFileReference {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just live in ls? Explain why if not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to ls in 38e7195. Instead of putting the logic on Program, I added a single exported method FileIncludeReason.GetSpanInReferrer(program) to compiler/fileInclude.go that handles the isReferencedFile() check + location retrieval + synthetic filtering + trivia-skipping (matching TypeScript's skipTrivia(file.text, importLiteral.pos)). The ls code calls program.GetIncludeReasons() + ref.GetSpanInReferrer() directly. The GetNonModuleFileReferences/NonModuleFileReference types on Program are removed.

}, nil
}

func (s *Server) handleFindFileReferences(ctx context.Context, params *lsproto.FindFileReferencesParams, _ *lsproto.RequestMessage) (lsproto.CustomFindFileReferencesResponse, error) {
Copy link
Copy Markdown
Member

@jakebailey jakebailey Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a cross-project aware call? Or did Strada really only do one project? I bet it does need to be cross-project a la registerMultiProjectReferenceRequestHandler.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be cross-project. That's the challenge I was talking about in the original issue. Fix it @copilot.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in f9bedbc. GetFileReferences now accepts a CrossProjectOrchestrator and uses it to search all initial projects (GetAllProjectsForInitialRequest) plus any additional projects discovered via GetProjectsLoadingProjectTree, deduplicating results across all of them. handleFindFileReferences is simplified to call getLanguageServiceAndCrossProjectOrchestrator and delegate to the LS. Also ported getFileReferences_server1, _server2, and _deduplicate from TypeScript's fourslash suite to validate the cross-project behavior.

…rchestrator

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/e57e7f66-ca9e-4e43-b376-fe3c262a720f

Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants