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/templating: gracefully handle unknown root #1171

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

charlieegan3
Copy link
Member

This also disables templating for files in the root.

Fixes #1164

Related to #1141, but this needs more work

Sorry for the low res video...

Screen.Recording.2024-10-03.at.18.16.04.mov

// if there are no parsed modules in the cache, then there is
// no need to run the aggregate report. This can happen if the
// server is very slow to start up.
if len(l.cache.GetAllModules()) == 0 {
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 unrelated, but is something that I noticed when working with empty files, a better check here is to test if there are modules rather than if there are files since empty files have no parse.

@@ -124,6 +116,73 @@ func TestTemplateContentsForFile(t *testing.T) {
}
}

func TestTemplateContentsForFileInWorkspaceRoot(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Root is not allowed and is an error which can be silently ignored if the caller understands it.

}
}

func TestTemplateContentsForFileWithUnknownRoot(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

When the root is unknown, but there is a path, we can attempt to use the server's root URI as a root instead.

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, with one comment consider. Even if we use an error for "control flow", I don't think we'll want to log this as such

@@ -989,7 +989,9 @@ func (l *LanguageServer) StartTemplateWorker(ctx context.Context) {
// determine the new contents for the file, if permitted
newContents, err := l.templateContentsForFile(job.URI)
if err != nil {
l.logError(fmt.Errorf("failed to template new file: %w", err))
if !errors.Is(err, &templatingInRootError{}) {
l.logError(fmt.Errorf("failed to template new file: %w", err))
Copy link
Member

Choose a reason for hiding this comment

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

Is it really an error unless it's something the user asked for and didn't get? If I saw this in the logs, I would probably think I did something wrong and that I should look into how to fix it.

Copy link
Member Author

@charlieegan3 charlieegan3 Oct 7, 2024

Choose a reason for hiding this comment

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

I've opted to not use the error for control flow here now: 0936ca7b7827ed2fc91a93a3b00bbccf7556e3be

While it's still an error, the callers of the function are now expected to test if the file is in the root and avoid calling the function accordingly.


err = os.WriteFile(filepath.Join(td, ".regal/config.yaml"), []byte{}, 0o600)
if err != nil {
t.Fatalf("failed to create directory %s: %s", filepath.Join(td, ".regal"), err)
Copy link
Member

Choose a reason for hiding this comment

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

This is not creating a directory

This also disables templating for files in the root.

Fixes StyraInc#1164

Related to StyraInc#1141, but this needs
more work

Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
Signed-off-by: Charlie Egan <charlie@styra.com>
@charlieegan3 charlieegan3 merged commit b751858 into StyraInc:main Oct 7, 2024
4 checks passed
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.

Templating doesn't work reliably without a .regal directory
2 participants