bazel-contrib / bazel-contrib/vscode-bazel

Go to Definition resolves package-relative labels from workspace root

Open
#696 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
296
Forks
108
Avg merge
3d 22h
Merged PRs (30d)
5

Description

## Bug

Go to Definition fails for package-relative source labels such as `srcs = ["client.py"]`.

The provider extracts `client.py` correctly, but runs the Bazel query from the Bazel workspace root. Bazel therefore resolves the label as `//:client.py` instead of relative to the BUILD file's package (`//pkg:client.py`).

This appears to be a regression introduced by #687. Before commit `bfb8a70`, `BazelGotoDefinitionProvider` passed `Utils.dirname(document.uri)` to `targetToUri()`. Commit `bfb8a70` changed the working directory to `workspaceInfo.bazelWorkspacePath`.

## Minimal reproduction

Download [vscode-bazel-relative-label-repro.zip](https://github.com/vinayan3/vscode-bazel-relative-label-repro/releases/download/repro-v1/vscode-bazel-relative-label-repro.zip), open the extracted contents as a folder in VS Code, and follow the included README.

The complete source is also available at [vinayan3/vscode-bazel-relative-label-repro](https://github.com/vinayan3/vscode-bazel-relative-label-repro).

The relevant structure is:

```text
.
├── MODULE.bazel
├── BUILD.bazel
└── pkg
├── BUILD.bazel
└── client.py
```

`pkg/BUILD.bazel` contains:

```starlark
filegroup(
name = "client",
srcs = ["client.py"],
)
```

## Steps to reproduce

1. Open the extracted reproduction folder in VS Code.
2. Open `pkg/BUILD.bazel`.
3. Invoke **Go to Definition** on `client.py` in `srcs = ["client.py"]`.

## Actual behavior

The extension reports:

```text
Bazel query failed with code 7.
ERROR: no such target '//:client.py': target 'client.py' not declared in package ''
```

This reproduces directly from the workspace root:

```console
$ bazel query 'kind(file, "client.py")'
ERROR: no such target '//:client.py': target 'client.py' not declared in package ''
```

## Expected behavior

Go to Definition opens `pkg/client.py`.

The same query succeeds when evaluated from the BUILD file's package:

```console
$ cd pkg
$ bazel query 'kind(file, "client.py")'
//pkg:client.py
```

Canonicalizing the label before querying from the workspace root also works:

```console
$ bazel query 'kind(file, "//pkg:client.py")'
//pkg:client.py
```

## Cause

In `src/definition/bazel_goto_definition_provider.ts`, commit `bfb8a70` changed:

```ts
targetToUri(targetText, Utils.dirname(document.uri));
```

to:

```ts
targetToUri(targetText, Uri.file(workspaceInfo.bazelWorkspacePath));
```

The regression test added with that change covers only an absolute label (`//pkg:target`), whose meaning does not depend on the query working directory. It does not cover package-relative rule or source-file labels.

## Suggested fix

Preserve package-relative label semantics by either:

- running queries for relative labels from the BUILD file's directory, or
- canonicalizing relative labels against the BUILD file's package before querying from the workspace root.

Regression coverage should include:

- `"client.py"`
- `"subdir/client.py"`
- `":target"`
- a package-relative Starlark file
- `"//pkg:target"`
- `"@repo//pkg:target"`

## Environment

- vscode-bazel commit: `bfb8a70`
- vscode-bazel extension version: `0.14.0`
- Bazel: `9.2.0`
- OS: macOS

Related: #687, #684

Contributor guide

Open the contributing guide

Research direction

Start in src/definition/bazel_goto_definition_provider.ts and inspect the regression coverage added with commit bfb8a70, which currently exercises an absolute label. Reproduce the failure with the provided archive or workspace structure, then add coverage for relative labels and verify that Go to Definition opens pkg/client.py while the existing absolute-label behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, vscode
Domain
developer-experience, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.