bazel-contrib / bazel-contrib/rules_go

gopackagesdriver does not work in pure GOWORK mode in a monorepo

Open
#4,248 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.5k
Forks
760
Avg merge
1d 11h
Merged PRs (30d)
12

Description

We're using a `go.work` layout in a monorepo, and setting gopackagesdriver in VSCode using it with `gopls`, results in a query to the gopackagesdriver that is only `./... builtin` - this does not resolve any modules from the `go.work` file, nor finds any `go.mod` files within the workspace.

It results in the very unintuitive Error message in VSCode:
```
[Error - 12:30:22] 2025/02/05 12:30:21 go/packages.Load #1: unexpected end of JSON input
view_id="1"
snapshot=0
directory=/home/mikn/src
query=[./... builtin]
packages=0
duration=688.341518ms

[Error - 12:30:22] 2025/02/05 12:30:21 initial workspace load failed: packages.Load error: unexpected end of JSON input
```
The error was figured out by looking at what the `query` field is set to when not using an external `GOPACKAGESDRIVER`, which, to `go list` is expanded to each module defined in the `go.work` file.

We fixed this for now, by doing this:

```bash
#!/usr/bin/env bash

if [[ -f "go.work" ]]; then
new_args=()
for arg in "${@}"; do
if [[ "$arg" == "./..." ]]; then
new_args+=($(go list -m -f '{{.Path}}' | xargs -I {} echo {}/...))
else
new_args+=("$arg")
fi
done
set -- "${new_args[@]}"
fi
exec bazel run -- @rules_go//go/tools/gopackagesdriver "${@}"
```

I am not sure this is a bug with the rules_go gopackagesdriver, or if it is gopls that is sending the wrong query in external mode, but we decided to fix it in the gopackagesdriver wrapper as it was the easiest place for us and I suspect that it may be the easiest to fix in `rules_go` also.

Contributor guide

Open the contributing guide

Research direction

Start with the rules_go gopackagesdriver entry point and reproduce the external-driver invocation in a monorepo containing go.work. Compare the ./... builtin query with the module-expanded query described in the issue, then verify that the driver resolves workspace modules and no longer returns unexpected end of JSON input.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
build-system, developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.