Skip to content

lsp: Fix missing initial root#1692

Merged
anderseknert merged 2 commits intoopen-policy-agent:mainfrom
charlieegan3:no-initial-workspace-root
Sep 18, 2025
Merged

lsp: Fix missing initial root#1692
anderseknert merged 2 commits intoopen-policy-agent:mainfrom
charlieegan3:no-initial-workspace-root

Conversation

@charlieegan3
Copy link
Copy Markdown
Contributor

Fixes #1671

when there is a user level config
Screenshot 2025-09-18 at 11 20 10

when there is no config at all
Screenshot 2025-09-18 at 11 19 06

Fixes open-policy-agent#1671

Signed-off-by: Charlie Egan <charlie_egan@apple.com>
Copy link
Copy Markdown
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.

Thanks! Looks very good to me. Just some nits and a suggestion.

Comment thread internal/lsp/server.go Outdated
if l.workspaceRootURI == "" {
err := l.updateRootURI(ctx,
// get the URI of the file's immediate parent
uri.FromPath(l.client.Identifier, filepath.Dir(uri.ToPath(l.client.Identifier, params.TextDocument.URI))),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can use l.fromPath and l.toPath here

Comment thread internal/lsp/server.go Outdated
} else {
l.log.Message("no config file found for workspace")
}
configRoots, err := lsconfig.FindConfigRoots(uri.ToPath(l.client.Identifier, normalizedRootURI))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here and later in this function.

Comment thread internal/lsp/server.go
if l.workspaceRootURI != "" {
workspaceRootPath := l.workspacePath()
if params.RootURI != "" {
err := l.updateRootURI(ctx, params.RootURI)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good!

Comment thread internal/lsp/server.go

if l.workspaceRootURI != "" {
workspaceRootPath := l.workspacePath()
if params.RootURI != "" {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we also look for this in the workspaceFolders param?
The spec suggests that's where it should be, and we've mostly been lucky that clients don't seem to honor that https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initializeParams

(and it's easy to see why they don't given how poorly that is documented)

Not suggesting we handle multiple workspaces as part of this, but we could perhaps just check if there's something there, and if there is we go with the first one (if nothing else was given).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah that's fair, I have something in there now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

noted in a comment that it's untested but based on the spec


files := map[string]string{
"foo/main.rego": mainRegoContents,
// here we are ignoring two issues with the file, so we expect no to
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no -> not :)

mainRegoURI := uri.FromPath(clients.IdentifierGeneric, filepath.Join(tempDir, filepath.FromSlash(mainRegoFileName)))

receivedMessages := make(chan types.FileDiagnostics, defaultBufferedChannelSize)
clientHandler := test.HandlerFor(methodTdPublishDiagnostics, test.SendsToChannel(receivedMessages))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Humble brag, but these helpers are so nice!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, big improvement

Address other PR comments.

Signed-off-by: Charlie Egan <charlie_egan@apple.com>
@charlieegan3
Copy link
Copy Markdown
Contributor Author

Thanks for the review, I have made a number of edits in a second commit: 60e9e01

@anderseknert anderseknert merged commit 819bbec into open-policy-agent:main Sep 18, 2025
8 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.

Language server crash on opening single file for editing

2 participants