Skip to content
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

lsp: Address empty module issues on rename #1078

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ func (l *LanguageServer) StartTemplateWorker(ctx context.Context) {
newContents, err := l.templateContentsForFile(evt.URI)
if err != nil {
l.logError(fmt.Errorf("failed to template new file: %w", err))

continue
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not the fix, but when there is an error in templating we should skip the rest.

}

// generate the edit params for the templating operation
Expand Down Expand Up @@ -790,6 +792,14 @@ func (l *LanguageServer) templateContentsForFile(fileURI string) (string, error)
return "", errors.New("file already has contents, templating not allowed")
}

diskContent, err := os.ReadFile(uri.ToPath(l.clientIdentifier, fileURI))
Copy link
Member Author

Choose a reason for hiding this comment

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

this is also not the fix but is an additional guard for templating over files that have contents on disk already.

Copy link
Member

Choose a reason for hiding this comment

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

Any added safeguards are good 👍

if err == nil {
// then we found the file on disk
if string(diskContent) != "" {
return "", errors.New("file on disk already has contents, templating not allowed")
}
}

path := uri.ToPath(l.clientIdentifier, fileURI)
dir := filepath.Dir(path)

Expand Down Expand Up @@ -1955,17 +1965,24 @@ func (l *LanguageServer) handleWorkspaceDidRenameFiles(
continue
}

_, content, err := cache.UpdateCacheForURIFromDisk(
l.cache,
uri.FromPath(l.clientIdentifier, renameOp.NewURI),
uri.ToPath(l.clientIdentifier, renameOp.NewURI),
)
if err != nil {
return nil, fmt.Errorf("failed to update cache for uri %q: %w", renameOp.NewURI, err)
content, ok := l.cache.GetFileContents(renameOp.OldURI)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the fix for the empty module issue, were we load from the cache first, and only load from disk if it's missing or empty in the cache

Copy link
Member

Choose a reason for hiding this comment

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

Nice!

// if the content is not in the cache then we can attempt to load from
// the disk instead.
if !ok || content == "" {
_, content, err = cache.UpdateCacheForURIFromDisk(
l.cache,
uri.FromPath(l.clientIdentifier, renameOp.NewURI),
uri.ToPath(l.clientIdentifier, renameOp.NewURI),
)
if err != nil {
return nil, fmt.Errorf("failed to update cache for uri %q: %w", renameOp.NewURI, err)
}
}

l.cache.Delete(renameOp.OldURI)

l.cache.SetFileContents(renameOp.NewURI, content)

evt := fileUpdateEvent{
Reason: "textDocument/didRename",
URI: renameOp.NewURI,
Expand All @@ -1975,6 +1992,8 @@ func (l *LanguageServer) handleWorkspaceDidRenameFiles(

l.diagnosticRequestFile <- evt
l.builtinsPositionFile <- evt
// if the file being moved is empty, we template it too (if empty)
l.templateFile <- evt
}

return struct{}{}, nil
Expand Down