-
Notifications
You must be signed in to change notification settings - Fork 40
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
||
// generate the edit params for the templating operation | ||
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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 | ||
|
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 is not the fix, but when there is an error in templating we should skip the rest.