When triggering go-to-definition on a label inside a load() statement that has multiple string arguments (e.g. load("@gazelle//:def.bzl", "gazelle")), the extension produces a malformed Bazel query and logs:
ERROR: too many arguments to function 'kind' at ', gazelle )'
See an example below
When trying to CMD+click on any of the functions above (load(), pip_compile(), create_venv()) I get the errors displayed above.
After investigating a bit, I think I found the reason. The LABEL_REGEX regex matches a double-quoted Bazel label: it opens with ", captures the label content, and closes with ". The two character classes [^:] inside mean "any character except :" but they didn't exclude ", so the greedy match could eat through closing quotes.
For load("@gazelle//:def.bzl", "gazelle"):
- Opens at the first
"
- Matches
@gazelle// via the first alternative
- Then
[^:]+ greedily consumes :def.bzl", "gazelle (crosses the " boundary)
- Finally finds a
" to close on — the one after gazelle
- Capture becomes
@gazelle//:def.bzl", "gazelle → 3-arg kind() query
With [^:"], the greedy match stops at the first " it hits, so the match correctly ends at "@gazelle//:def.bzl".
Fix: Change [^:] → [^:"] in both character classes so the match stops at the closing quote.
From:
const LABEL_REGEX = /"((?:@\w+)?\/\/|(?:.+\/)?[^:]*(?::[^:]+)?)"/;
to:
const LABEL_REGEX = /"((?:@\w+)?\/\/|(?:.+\/)?[^:"]*(?::[^:"]+)?)"/;
I've already confirmed this works by manually patching my ~/.vscode-server/extensions/bazelbuild.vscode-bazel-0.14.0/dist/extension.js.
I have a fix ready with tests if you'd like a PR.
When triggering go-to-definition on a label inside a
load()statement that has multiple string arguments (e.g.load("@gazelle//:def.bzl", "gazelle")), the extension produces a malformed Bazel query and logs:See an example below
When trying to CMD+click on any of the functions above (
load(),pip_compile(),create_venv()) I get the errors displayed above.After investigating a bit, I think I found the reason. The LABEL_REGEX regex matches a double-quoted Bazel label: it opens with
", captures the label content, and closes with". The two character classes[^:]inside mean "any character except:" but they didn't exclude", so the greedy match could eat through closing quotes.For
load("@gazelle//:def.bzl", "gazelle"):"@gazelle//via the first alternative[^:]+greedily consumes:def.bzl", "gazelle(crosses the"boundary)"to close on — the one aftergazelle@gazelle//:def.bzl", "gazelle→ 3-argkind()queryWith
[^:"], the greedy match stops at the first"it hits, so the match correctly ends at"@gazelle//:def.bzl".Fix: Change
[^:]→[^:"]in both character classes so the match stops at the closing quote.From:
to:
I've already confirmed this works by manually patching my
~/.vscode-server/extensions/bazelbuild.vscode-bazel-0.14.0/dist/extension.js.I have a fix ready with tests if you'd like a PR.