Skip to content

Commit

Permalink
lsp: Correct Path to URI encoding (#1083)
Browse files Browse the repository at this point in the history
Spaces must be encoded as %20. This case causing the state worker to
reload contents from disk as it appeared a file did not exist in the cache.

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 authored Sep 9, 2024
1 parent 6badb5d commit 146895c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
31 changes: 9 additions & 22 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,14 @@ func (l *LanguageServer) StartDiagnosticsWorker(ctx context.Context) {
case <-ctx.Done():
return
case evt := <-l.diagnosticRequestFile:
success, err := l.processTextContentUpdate(ctx, evt.URI, evt.Content)
err := l.processTextContentUpdate(ctx, evt.URI, evt.Content)
if err != nil {
l.logError(fmt.Errorf("failed to process text content update: %w", err))

continue
}

if !success {
continue
}

// otherwise, lint the file and send the diagnostics
// lint the file and send the diagnostics
err = updateFileDiagnostics(ctx, l.cache, l.loadedConfig, evt.URI, l.workspaceRootURI)
if err != nil {
l.logError(fmt.Errorf("failed to update file diagnostics: %w", err))
Expand Down Expand Up @@ -936,33 +932,24 @@ func (l *LanguageServer) processTextContentUpdate(
ctx context.Context,
fileURI string,
content string,
) (bool, error) {
) error {
if l.ignoreURI(fileURI) {
return false, nil
return nil
}

currentContent, ok := l.cache.GetFileContents(fileURI)
if ok && currentContent == content {
return false, nil
return nil
}

l.cache.SetFileContents(fileURI, content)

success, err := updateParse(ctx, l.cache, l.regoStore, fileURI)
_, err := updateParse(ctx, l.cache, l.regoStore, fileURI)
if err != nil {
return false, fmt.Errorf("failed to update parse: %w", err)
}

if success {
return true, nil
}

err = l.sendFileDiagnostics(ctx, fileURI)
if err != nil {
return false, fmt.Errorf("failed to send diagnostic: %w", err)
return fmt.Errorf("failed to update parse: %w", err)
}

return false, nil
return nil
}

// processHoverContentUpdate updates information about built in, and keyword
Expand Down Expand Up @@ -2132,7 +2119,7 @@ func (l *LanguageServer) loadWorkspaceContents(ctx context.Context, newOnly bool
}

// if the caller has requested only new files, then we can exit early
if _, ok := l.cache.GetModule(fileURI); newOnly && ok {
if _, ok := l.cache.GetFileContents(fileURI); newOnly && ok {
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/lsp/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func FromPath(client clients.Identifier, path string) string {
// Convert Windows path separators to Unix separators
path = filepath.ToSlash(path)

// spaces must be percent-encoded
path = strings.ReplaceAll(path, " ", "%20")

// If the path is a Windows path, the colon after the drive letter needs to be
// percent-encoded.
if parts := strings.Split(path, ":"); len(parts) > 1 {
Expand Down
4 changes: 4 additions & 0 deletions internal/lsp/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func TestPathToURI_VSCode(t *testing.T) {
path: "/foo/bar",
want: "file:///foo/bar",
},
"unix spaces": {
path: "/foo/bar baz",
want: "file:///foo/bar%20baz",
},
"unix prefixed": {
path: "file:///foo/bar",
want: "file:///foo/bar",
Expand Down

0 comments on commit 146895c

Please sign in to comment.