Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 14, 2025

Fix nil pointer dereference in getAdjustedLocation when handling textDocument/documentHighlight

Summary

Fixed a bug in the getAdjustedLocation function that caused nil pointer dereference panics when processing textDocument/documentHighlight requests on TypeScript files with type-only export statements.

Changes Made

  • Fixed line 917: Corrected from parent.Parent.AsExportDeclaration() to parent.AsExportDeclaration() for ExportDeclaration nodes
  • Verified changes match TypeScript implementation
  • All tests pass (go test ./...)
  • Code formatted and linted successfully
  • CodeQL security check completed
  • Updated failing test list - TestReferencesForStatementKeywords now passes

Root Cause

The panic occurred when ExportDeclaration handling incorrectly accessed parent.Parent instead of parent itself. When parent is already an ExportDeclaration with isTypeOnly, we should pass parent directly to getAdjustedLocationForExportDeclaration, not parent.Parent.

Solution

Applied minimal, surgical fix that aligns with the TypeScript reference implementation:

  • Corrected node reference for ExportDeclaration case (line 917)

For the ImportClause case, the TypeScript AST definition guarantees that an ImportClause's parent is always either ImportDeclaration or JSDocImportTag (never nil), so no nil check is needed there.

Test Results

The fix allowed TestReferencesForStatementKeywords to pass, which was previously failing and has been removed from the failing tests list.

Testing

  • ✅ All existing tests pass
  • ✅ Build successful
  • ✅ Lint successful
  • ✅ Format successful
  • ✅ CodeQL check complete
  • ✅ Regenerated fourslash tests and updated failing list

Security Summary

No security vulnerabilities introduced or fixed by this change.

Original prompt

This section details on the original issue you should resolve

<issue_title>panic handling request textDocument/documentHighlight in getAdjustedLocation</issue_title>
<issue_description>> I ran into a similar error (my own built version which is sync'd with main as of today) :

panic handling request textDocument/documentHighlight runtime error: invalid memory address or nil pointer dereference goroutine 547756 [running]:
runtime/debug.Stack()
	/opt/homebrew/Cellar/go/1.25.2/libexec/src/runtime/debug/stack.go:26 +0x64
github.com/microsoft/typescript-go/internal/lsp.(*Server).recover(0x1400017cdc0, 0x1429addd140)
	/Users/rishi/Developer/forks/typescript-go/internal/lsp/server.go:542 +0x44
panic({0x102be4e40?, 0x10338fe50?})
	/opt/homebrew/Cellar/go/1.25.2/libexec/src/runtime/panic.go:783 +0x120
github.com/microsoft/typescript-go/internal/ls.getAdjustedLocation.func1(...)
	/Users/rishi/Developer/forks/typescript-go/internal/ls/utilities.go:920
github.com/microsoft/typescript-go/internal/ls.getAdjustedLocation(0x142915a7b90, 0x0, 0x0)
	/Users/rishi/Developer/forks/typescript-go/internal/ls/utilities.go:944 +0x1a4
github.com/microsoft/typescript-go/internal/ls.getMeaningFromLocation(0x14115abca18?)
	/Users/rishi/Developer/forks/typescript-go/internal/ls/utilities.go:1245 +0x24
github.com/microsoft/typescript-go/internal/ls.getIntersectingMeaningFromDeclarations(0x14287c36f50?, 0x14287c36f50, 0x99e5c608?)
	/Users/rishi/Developer/forks/typescript-go/internal/ls/utilities.go:1322 +0x24
github.com/microsoft/typescript-go/internal/ls.getReferencedSymbolsForSymbol(0x14287c36f50, 0x142915a7b90, {0x14178ee8040, 0x1, 0x1}, 0x14178ee8050, 0x14299e5c608, {0x0, 0x0, 0x2, ...})
	/Users/rishi/Developer/forks/typescript-go/internal/ls/findallreferences.go:1197 +0xb0
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).getReferencedSymbolsForNode(0x142b4c11bf0, {0x102dce3b0, 0x142b4f6d4f0}, 0x0, 0x142915a7b90, 0x1412c00a1e0, {0x14178ee8040, 0x1, 0x1}, {0x0, ...}, ...)
	/Users/rishi/Developer/forks/typescript-go/internal/ls/findallreferences.go:683 +0x5ec
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).getSemanticDocumentHighlights(0x142b4c11bf0, {0x102dce3b0, 0x142b4f6d4f0}, 0x0, 0x142915a7b90, 0x1412c00a1e0, 0x1412f498608)
	/Users/rishi/Developer/forks/typescript-go/internal/ls/documenthighlights.go:55 +0xc4
github.com/microsoft/typescript-go/internal/ls.(*LanguageService).ProvideDocumentHighlights(0x142b4c11bf0, {0x102dce3b0, 0x142b4f6d4f0}, {0x14053ad3770?, 0x14000360000?}, {0x2dce3b0?, 0x1?})
	/Users/rishi/Developer/forks/typescript-go/internal/ls/documenthighlights.go:45 +0x34c
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleDocumentHighlight(0x14000360000?, {0x102dce3b0?, 0x142b4f6d4f0?}, 0x14053ad3770?, 0x46?)
	/Users/rishi/Developer/forks/typescript-go/internal/lsp/server.go:863 +0x38
github.com/microsoft/typescript-go/internal/lsp.init.func1.registerLanguageServiceDocumentRequestHandler[...].24({0x102dce3b0, 0x142b4f6d4f0}, 0x1429addd140)
	/Users/rishi/Developer/forks/typescript-go/internal/lsp/server.go:528 +0xe0
github.com/microsoft/typescript-go/internal/lsp.(*Server).handleRequestOrNotification(0x1400017cdc0, {0x102dce3b0, 0x142b4f6d4f0}, 0x1429addd140)
	/Users/rishi/Developer/forks/typescript-go/internal/lsp/server.go:425 +0xf4
github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop.func1()
	/Users/rishi/Developer/forks/typescript-go/internal/lsp/server.go:330 +0x34
created by github.com/microsoft/typescript-go/internal/lsp.(*Server).dispatchLoop in goroutine 22
	/Users/rishi/Developer/forks/typescript-go/internal/lsp/server.go:350 +0x6f8

All the file has is something similar to this in jest.config.ts

export default {
  preset: '@package/build',
};

Originally posted by @rgoomar in #1867</issue_description>

Comments on the Issue (you are @copilot in this section)

@ahejlsberg This was fixed in #1920? @jakebailey That PR was `getMappedLocation`, but this issue is `getAdjustedLocation`, so I don't think they're the same?

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix panic handling request textDocument/documentHighlight Fix nil pointer dereference in getAdjustedLocation for type-only imports/exports Nov 14, 2025
Copilot AI requested a review from jakebailey November 14, 2025 12:28
Copilot finished work on behalf of jakebailey November 14, 2025 12:28
@jakebailey
Copy link
Member

@copilot run npm run updatefailing and commit the result

Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 14, 2025

@copilot run npm run updatefailing and commit the result

Done. The fix allowed TestReferencesForStatementKeywords to pass, which has now been removed from the failing tests list. (51b3e5e)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • iojs.org
    • Triggering command: curl -q --fail --compressed -L -s REDACTED -o - (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of jakebailey November 14, 2025 13:07
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 14, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • centralus-2.in.applicationinsights.azure.com
    • Triggering command: go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 custom (dns block)
    • Triggering command: go build -ldflags -s -w -X &#39;main.version=v2.6.0-custom-gcl-94jqzDflNoq1ZZ2lGqIGB8QB8Hs6XG3sDaDhRRIDkMc&#39; -X &#39;main.date=2025-11-14 13:21:12.978908271 &#43;0000 UTC&#39; -o custom-gcl ./cmd/golangci-lint (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Fix nil pointer dereference in getAdjustedLocation for type-only imports/exports Fix nil pointer dereference in getAdjustedLocation for type-only exports Nov 14, 2025
Copilot AI requested a review from jakebailey November 14, 2025 13:24
Copilot finished work on behalf of jakebailey November 14, 2025 13:24
@jakebailey jakebailey marked this pull request as ready for review November 14, 2025 22:19
Copilot AI review requested due to automatic review settings November 14, 2025 22:19
Copilot finished reviewing on behalf of jakebailey November 14, 2025 22:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a nil pointer dereference bug in the getAdjustedLocation function that caused panics when handling textDocument/documentHighlight requests on TypeScript files with type-only export statements (e.g., export type * from "./module").

Key Changes:

  • Fixed incorrect parent node traversal for ExportDeclaration handling in getAdjustedLocation
  • Enabled and updated the TestReferencesForStatementKeywords test which now passes

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
internal/ls/utilities.go Corrected line 917 to use parent instead of parent.Parent when parent is already an ExportDeclaration, fixing the nil pointer dereference
internal/fourslash/tests/gen/referencesForStatementKeywords_test.go Removed t.Skip() to enable the test that now passes with the fix
internal/fourslash/_scripts/failingTests.txt Removed TestReferencesForStatementKeywords from the failing tests list
testdata/baselines/reference/fourslash/findAllReferences/referencesForStatementKeywords.baseline.jsonc Added baseline test output showing correct behavior for finding references in type-only import/export statements

@jakebailey jakebailey added this pull request to the merge queue Nov 14, 2025
Merged via the queue into main with commit 7ebe277 Nov 14, 2025
28 checks passed
@jakebailey jakebailey deleted the copilot/fix-document-highlight-panic branch November 14, 2025 22:45
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.

panic handling request textDocument/documentHighlight in getAdjustedLocation

3 participants