Skip to content

Conversation

rgoomar
Copy link

@rgoomar rgoomar commented Aug 22, 2025

Overview

These changes made it so I could go to a proper definition. This approach aligns with the way the TypeScript version of the compiler does it using VFS only and VLQ decoding.

Basically we have a situation where we have an import in a .ts file like

import { getLogger } from '@latticehr/o11y';

in a directory like apps/app-one in an index.ts file and the @latticehr is an alias that should reference packages/o11y and it does go to the packages/o11y/dist/*.d.ts file although not the source file implementation

This gets to the correct file and line position using VLQ decoding like the TS version does. This also enables the differentiation between "Go to Definition" and "Go to Type Definition". They both work now.

TypeScript Source Code References

Our Go implementation replicates several key interfaces and functions from the TypeScript codebase. Here are the specific references:

Core Interfaces and Types

1. DocumentPosition Interface

export interface DocumentPosition {
    fileName: string;
    pos: number;
}

2. DocumentPositionMapper Interface

export interface DocumentPositionMapper {
    getSourcePosition(input: DocumentPosition): DocumentPosition;
    getGeneratedPosition(input: DocumentPosition): DocumentPosition;
}

3. DocumentPositionMapperHost Interface

export interface DocumentPositionMapperHost {
    getSourceFileLike(fileName: string): SourceFileLike | undefined;
    getCanonicalFileName(path: string): string;
    log(text: string): void;
}

4. SourceFileLike Interface

export interface SourceFileLike {
    readonly text: string;
    /** @internal */
    lineMap?: readonly number[];
    /** @internal */
    getPositionOfLineAndCharacter?(line: number, character: number, allowEdits?: true): number;
}

Core Implementation Functions

5. createDocumentPositionMapper Function

export function createDocumentPositionMapper(host: DocumentPositionMapperHost, map: RawSourceMap, mapPath: string): DocumentPositionMapper

Service Layer Interfaces

6. SourceMapper Interface

export interface SourceMapper {
    toLineColumnOffset(fileName: string, position: number): LineAndCharacter;
    tryGetSourcePosition(info: DocumentPosition): DocumentPosition | undefined;
    tryGetGeneratedPosition(info: DocumentPosition): DocumentPosition | undefined;
    clearCache(): void;
    documentPositionMappers: Map<string, DocumentPositionMapper>;
}

7. SourceMapperHost Interface

export interface SourceMapperHost {
    useCaseSensitiveFileNames(): boolean;
    getCurrentDirectory(): string;
    getProgram(): Program | undefined;
    fileExists?(path: string): boolean;
    readFile?(path: string, encoding?: string): string | undefined;
    getSourceFileLike?(fileName: string): SourceFileLike | undefined;
    getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined;
    log(s: string): void;
}

8. getSourceMapper Function

export function getSourceMapper(host: SourceMapperHost): SourceMapper

9. getDocumentPositionMapper Function (within SourceMapper)

function getDocumentPositionMapper(generatedFileName: string, sourceFileName?: string)

10. tryGetSourcePosition Function (within SourceMapper)

function tryGetSourcePosition(info: DocumentPosition): DocumentPosition | undefined

Key Implementation Details

11. VLQ Decoding and Mapping Processing

function getDecodedMappings() {
    if (decodedMappings === undefined) {
        const decoder = decodeMappings(map.mappings);
        const mappings = arrayFrom(decoder, processMapping);
        // ...
    }
    return decodedMappings;
}

The Go code maintains the same separation of concerns, with the sourcemap package handling low-level mapping operations and the ls package providing LSP-specific integration, just as TypeScript separates compiler-level and service-level functionality.

@rgoomar
Copy link
Author

rgoomar commented Aug 23, 2025

@microsoft-github-policy-service agree

@rgoomar rgoomar changed the title [WIP] fix go to definition fix go to definition Aug 23, 2025
@rgoomar rgoomar changed the title fix go to definition [LSP] fix go to definition Aug 23, 2025
@alexwork1611
Copy link

Keeping an eye on this...

Comment on lines 147 to 148
// Fallback to underlying FS for files not tracked by the program (like .d.ts.map)
return fs.source.FS().ReadFile(path)
Copy link
Member

@iisaduan iisaduan Aug 25, 2025

Choose a reason for hiding this comment

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

This shouldn't be added onto the compilerhost since d.ts.map is only used by ls features. Can you refactor so only the calls to readFile in definitionSourceMap has this fallback for d.ts.map files? It should probably also read into the snapshot's fs, instead of the underlying file system

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense, I'll adjust that

Copy link
Author

Choose a reason for hiding this comment

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

@iisaduan I made the change with an alternative method here. Turns out that fs.source.FS() is the snapshot FS

@rgoomar rgoomar requested a review from iisaduan August 25, 2025 22:35
@rgoomar
Copy link
Author

rgoomar commented Aug 27, 2025

This is working for multiple engineers internally for us 😄

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

I think this is going in the right direction, and I've now skimmed the old code to see how this is going, but I think the separation of concerns isn't quite right.

The source map decoding stuff needs to move into the sourcemap package and not reference the LS code. There is a lot of code already there that should be reused.

I also don't like the hardcoding of the ^ stuff, which we didn't have to do before as far as I can tell. That to me means something isn't quite working, but I think I'd like to see the code split a bit to help me be less distracted at the actual .map code.

@rgoomar rgoomar requested a review from jakebailey September 9, 2025 21:29
@rgoomar rgoomar requested a review from jakebailey September 10, 2025 00:48
Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

I think the core idea is right, but I still don't like how much indirection and layering this introduces. It seems like a bunch of hosts, create functions returning more layers, etc, when it could all just be one thing, one host, etc. The fallback thing I can understand needing, but it's simpler to instead accept an FS that won't fail on reads, and we typically do not subdivide out specific read/write methods, unlike the old codebase with the many many host implementations.

I don't know what specific feedback I can give to make this better, though. It's not really one thing, more of a general design feeling that makes this seem much more indirected than other code we have in the repo.


// Helper functions

func isDeclarationFileName(fileName string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

This already exists in the tspath package, and other places you've used HasSuffix for this can be replaced too.

Comment on lines +192 to +197
func createGetCanonicalFileName(useCaseSensitiveFileNames bool) func(string) string {
if useCaseSensitiveFileNames {
return func(path string) string { return path }
}
return strings.ToLower
}
Copy link
Member

Choose a reason for hiding this comment

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

This isn't a correct implementation, unfortunately. If we need to compare paths like this, we use tspath.ToPath.

We also don't pass around "canonical filename functions" anymore, instead choosing to pass around Paths and the case sensitivity.

return strings.ToLower
}

func tryGetSourceMappingURL(content string) string {
Copy link
Member

Choose a reason for hiding this comment

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

This func already exists in util.go.

type SourceMapper interface {
TryGetSourcePosition(info DocumentPosition) *DocumentPosition
TryGetGeneratedPosition(info DocumentPosition) *DocumentPosition
ClearCache()
Copy link
Member

Choose a reason for hiding this comment

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

This ClearCache infra is unused.

Comment on lines +22 to +23
ReadFile(path string) (string, bool)
FileExists(path string) bool
Copy link
Member

Choose a reason for hiding this comment

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

This could really just be an FS() method

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.

4 participants