Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gopackagesdriver does not work in pure GOWORK mode in a monorepo #4248

Open
mikn opened this issue Feb 5, 2025 · 0 comments
Open

gopackagesdriver does not work in pure GOWORK mode in a monorepo #4248

mikn opened this issue Feb 5, 2025 · 0 comments

Comments

@mikn
Copy link

mikn commented Feb 5, 2025

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:

#!/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.

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

No branches or pull requests

1 participant