-
Notifications
You must be signed in to change notification settings - Fork 697
[LSP] fix go to definition #1627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@microsoft-github-policy-service agree |
Keeping an eye on this... |
internal/project/compilerhost.go
Outdated
// Fallback to underlying FS for files not tracked by the program (like .d.ts.map) | ||
return fs.source.FS().ReadFile(path) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
This is working for multiple engineers internally for us 😄 |
There was a problem hiding this 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.
There was a problem hiding this 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 { |
There was a problem hiding this comment.
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.
func createGetCanonicalFileName(useCaseSensitiveFileNames bool) func(string) string { | ||
if useCaseSensitiveFileNames { | ||
return func(path string) string { return path } | ||
} | ||
return strings.ToLower | ||
} |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
ReadFile(path string) (string, bool) | ||
FileExists(path string) bool |
There was a problem hiding this comment.
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
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 likein a directory like
apps/app-one
in anindex.ts
file and the@latticehr
is an alias that should referencepackages/o11y
and it does go to thepackages/o11y/dist/*.d.ts
file although not the source file implementationThis 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
internal/sourcemap/mapper.go:11-15
_submodules/TypeScript/src/compiler/types.ts:9889-9892
2. DocumentPositionMapper Interface
internal/sourcemap/mapper.go:17-21
_submodules/TypeScript/src/compiler/types.ts:9883-9886
3. DocumentPositionMapperHost Interface
internal/sourcemap/mapper.go:23-28
_submodules/TypeScript/src/compiler/types.ts:9872-9876
4. SourceFileLike Interface
internal/sourcemap/mapper.go:30-33
_submodules/TypeScript/src/compiler/types.ts:4274-4280
Core Implementation Functions
5. createDocumentPositionMapper Function
internal/sourcemap/mapper.go:36-49
(CreateDocumentPositionMapper)_submodules/TypeScript/src/compiler/sourcemap.ts:699-713
Service Layer Interfaces
6. SourceMapper Interface
internal/sourcemap/sourcemapper.go:10-15
_submodules/TypeScript/src/services/sourcemaps.ts:33-39
7. SourceMapperHost Interface
internal/sourcemap/sourcemapper.go:17-24
_submodules/TypeScript/src/services/sourcemaps.ts:42-51
8. getSourceMapper Function
internal/sourcemap/sourcemapper.go:40-47
(CreateSourceMapper)_submodules/TypeScript/src/services/sourcemaps.ts:54-65
9. getDocumentPositionMapper Function (within SourceMapper)
internal/sourcemap/sourcemapper.go:83-98
(getDocumentPositionMapper method)_submodules/TypeScript/src/services/sourcemaps.ts:71-91
10. tryGetSourcePosition Function (within SourceMapper)
internal/sourcemap/sourcemapper.go:55-70
(TryGetSourcePosition method)_submodules/TypeScript/src/services/sourcemaps.ts:93-99
Key Implementation Details
11. VLQ Decoding and Mapping Processing
internal/sourcemap/mapper.go:135-180
(ensureMappingsDecoded)_submodules/TypeScript/src/compiler/sourcemap.ts:737-752
The Go code maintains the same separation of concerns, with the
sourcemap
package handling low-level mapping operations and thels
package providing LSP-specific integration, just as TypeScript separates compiler-level and service-level functionality.