Skip to content

fix: go to definition fails on load() statements with multiple arguments #633

Description

@adeftu

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

Image

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions