Skip to content

lsp: Use filename path for current buffer for defns#1732

Merged
charlieegan3 merged 2 commits intoopen-policy-agent:mainfrom
charlieegan3:uri-defn
Oct 29, 2025
Merged

lsp: Use filename path for current buffer for defns#1732
charlieegan3 merged 2 commits intoopen-policy-agent:mainfrom
charlieegan3:uri-defn

Conversation

@charlieegan3
Copy link
Copy Markdown
Contributor

There was a bug here where the defn would only work when the result was another file. Now we use the same format of reference for the current buffer as we use for other modules (a path rather than a URI)

Fixes #1700

There was a bug here where the defn would only work when the result was
another file. Now we use the same format of reference for the current
buffer as we use for other modules (a path rather than a URI)

Fixes open-policy-agent#1700

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.

Thank you! This not working has been a real pain in the neck, so this is a much welcome fix <3 Perhaps worth breaking out the Filename and URI value creation just so that we can test them? And make sure we can't change them accidentally without also having to change tests (which likely will alert us about breaking something)?

(even if it means some pretty silly tests)

@charlieegan3
Copy link
Copy Markdown
Contributor Author

The tests are a pretty similar to the existing URI tests, but I've added some new helpers for the relative operations. We're doing these in a few other places too, if mostly in tests. Lmk if that look ok.

@charlieegan3
Copy link
Copy Markdown
Contributor Author

One thing I dislike is the client identifier functionality. I think I perhaps added that prematurely when after noticing some minor differences in VSCode. I think my preference is to keep it for now, and to do a review of client specific behaviour separately.

Comment thread internal/lsp/server.go Outdated
query := oracle.DefinitionQuery{
// The value of Filename is used if the defn in the current buffer.
Filename: params.TextDocument.URI,
Filename: uri.ToRelativePath(l.client.Identifier, params.TextDocument.URI, l.workspaceRootURI),
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.

There's also

func (l *LanguageServer) toRelativePath(fileURI string) string {
	return strings.TrimPrefix(l.toPath(fileURI), l.workspacePath()+string(os.PathSeparator))
}

Which could be updated to use your new function, and then we can use that here instead

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.

Ahh! I missed that one...

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.

OK, updated that function now.

Signed-off-by: Charlie Egan <charlie_egan@apple.com>
@charlieegan3 charlieegan3 enabled auto-merge (squash) October 29, 2025 11:14
@charlieegan3 charlieegan3 merged commit a373ae3 into open-policy-agent:main Oct 29, 2025
12 of 15 checks passed
@charlieegan3 charlieegan3 deleted the uri-defn branch October 29, 2025 11:45
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.

Go-to definition issues

2 participants