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

Conversation

charlieegan3
Copy link
Member

This PR makes changes to where the contents of files come from when being moved. Previously, preference was given to the contents on disk. This turned out to be incorrect as the files on disk are often empty until saved.

I found that I was able to replicate one of the empty module problems by:

  • creating a file, and not saving it
  • creating a dir
  • editing the file
  • moving the file into the dir while the file on disk is empty

before

Screen.Recording.2024-09-09.at.10.32.12.mov

after

Screen.Recording.2024-09-09.at.10.34.16.mov

@@ -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.

@@ -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 {
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!

@charlieegan3 charlieegan3 force-pushed the template-fun branch 2 times, most recently from 0f5cacb to d241bbf Compare September 9, 2024 10:01
Copy link
Member

@anderseknert anderseknert left a comment

Choose a reason for hiding this comment

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

LGTM! Test would be good, as with anything touching the LSP, but I know they can be hard to write given the many steps required in sequence. Let's look into how we improve on that later 👍

@@ -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

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 {
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

Choose a reason for hiding this comment

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

Nice!

@charlieegan3 charlieegan3 merged commit 28bac91 into StyraInc:main Sep 9, 2024
4 checks passed
srenatus pushed a commit to srenatus/regal that referenced this pull request Oct 1, 2024
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.

2 participants