Subscribe

Running go vet and go test in GitHub Actions when go list fails on stray scripts

A scripts folder with two package names in one directory makes go list ./... fail, and with it go vet, go test and govulncheck. Here is how I list packages with go list -e, filter by path and pass the list through GITHUB_OUTPUT, and why I don't filter on .Error.

Running go vet and go test in GitHub Actions when go list fails on stray scripts

When I added go vet and go test as a blocking check to one of our backend repos at work, the plan was the usual two lines with ./.... It didn't work. go list ./... failed outright, and every Go command that takes ./... failed with it.

The cause was a scripts/ folder. Over time people had dropped one-off programs in there: a backfill, a data fix, a report. Some directories had two files declaring different package names, some imported packages that no longer existed. Nobody builds them as part of the service and nobody wanted to touch them in the same PR as the CI change itself.

In this blog we will look at how to run vet and test (and govulncheck) on the real packages only, without fixing or deleting the scripts first.

Reproducing it

I made a small module that looks like the problem. Two real packages, and a scripts/backfill directory with a main.go plus an old check_old.go that says package check and imports a package that is gone.

shop/
  go.mod
  cmd/api/main.go
  internal/orders/orders.go
  internal/orders/orders_test.go
  scripts/backfill/main.go
  scripts/backfill/check_old.go
  scripts/report/report.go

With Go 1.27.3:

go list ./...
found packages check (check_old.go) and main (main.go) in /home/me/shop/scripts/backfill

Exit code 1, and nothing else listed. go vet ./... fails the same way and doesn't vet anything. go test ./... is a bit kinder: it runs the tests it can and still exits 1 with FAIL example.com/shop/scripts/backfill [setup failed]. So a red build every time, for code that never ships.

go list -e

The -e flag tells go list to report broken packages instead of stopping on them. It lists every package, broken ones included, and exits 0:

go list -e ./...
example.com/shop/cmd/api
example.com/shop/internal/orders
example.com/shop/scripts/backfill
example.com/shop/scripts/report

Then drop scripts/ by the path:

go list -e ./... | grep -v '/scripts/'
example.com/shop/cmd/api
example.com/shop/internal/orders

That list goes to go vet and go test as arguments. Both passed.

Don't filter on .Error

My first idea was smarter, or so I thought. go list can print a template per package, and broken packages have .Error set. So skip anything broken, wherever it lives:

go list -e -f '{{if not .Error}}{{if not .DepsErrors}}{{.ImportPath}}{{end}}{{end}}' ./...

On the sample module it gave the right list. Then I tried the case that matters, a real package that's broken. I added a file to internal/orders that imports a package that doesn't exist, and ran the template again:

example.com/shop/scripts/report

internal/orders was gone, and so was cmd/api because it depends on orders. Then go test on that list:

?       example.com/shop/scripts/report [no test files]

Exit code 0. A broken import in the main code, and CI is green. That's the exact thing the check exists to catch, so I dropped the template idea fast.

See, the path filter is dumb, and that's why it's right here. It only ever skips scripts/. If internal/orders breaks, it's still in the list and go vet fails loudly. (A plain type error doesn't set .Error at all, go list doesn't type check. Only load errors like a missing import or mixed package names do. But that's still enough to hide a real break.)

The workflow

GitHub Actions doesn't let one step's shell variables reach the next step, so the list goes through GITHUB_OUTPUT. If you're new to steps and outputs, I wrote about them in GitHub Actions from zero.

name: go-checks

on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  vet-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7

      - uses: actions/setup-go@v7
        with:
          go-version-file: go.mod

      - name: List packages
        id: pkgs
        run: |
          set -euo pipefail
          # scripts/ holds one-off programs that don't build as packages.
          # -e lists everything without failing; then drop scripts/ by path.
          list=$(go list -e ./... | grep -v '/scripts/' | tr '\n' ' ')
          if [ -z "${list// /}" ]; then
            echo "no packages found, refusing to pass an empty list" >&2
            exit 1
          fi
          echo "Packages: $list"
          echo "list=$list" >> "$GITHUB_OUTPUT"

      - name: go vet
        run: go vet ${{ steps.pkgs.outputs.list }}

      - name: go test
        run: go test -race -count=1 ${{ steps.pkgs.outputs.list }}

A few notes on it.

  • tr '\n' ' ' puts the list on one line. A plain name=value line in GITHUB_OUTPUT can't hold newlines, you'd need the heredoc delimiter syntax for that.
  • The empty check matters. go vet with no arguments vets the package in the current directory, which in most repos is the root and might have no Go files at all. An empty list should fail, not quietly vet nothing.
  • ${{ steps.pkgs.outputs.list }} is pasted into the script as text before bash runs, so the spaces split into separate arguments. When I tried to imitate this locally with go vet $L in zsh, I got malformed import path "example.com/shop/cmd/api example.com/shop/internal/orders ", because zsh doesn't split unquoted variables. Ran it in bash, it was fine. Just a local testing trap, Actions uses bash.

I checked the file with actionlint 1.7.12 and it was clean.

Make the job a required status check in branch protection, otherwise it reports red and people basically merge anyway.

Passing the same list to govulncheck

govulncheck loads packages the same way, so govulncheck ./... hits the same wall:

govulncheck: loading packages:
There are errors with the provided package patterns:
...found packages check (check_old.go) and main (main.go) in .../scripts/backfill

I won't repeat the basics here, they're in finding vulnerable dependencies in golang using govulncheck. The short version: give it the same filtered list and it works, with full reachability analysis.

  vulns:
    runs-on: ubuntu-latest
    needs: vet-test
    continue-on-error: true
    steps:
      - uses: actions/checkout@v7

      - uses: actions/setup-go@v7
        with:
          go-version-file: go.mod

      - name: govulncheck on the same package list
        run: |
          set -uo pipefail
          go install golang.org/x/vuln/cmd/govulncheck@v1.10.0
          pkgs=$(go list -e ./... | grep -v '/scripts/')
          # shellcheck disable=SC2086
          govulncheck -format json $pkgs > govulncheck.json
          python3 ci/vulnsummary.py govulncheck.json >> "$GITHUB_STEP_SUMMARY"

At work we went another way and used -scan module, which doesn't load packages at all. We had a second reason also: symbol mode crashed inside the type checker on one transitive dependency. Module mode has two catches though. It accepts no package patterns (patterns are not accepted for module only scanning), and it reports every advisory for a module version without checking you call the bad function. On my sample module it also failed with no Go files in the root, because the root directory had no Go file. Adding a doc.go there fixed it. Use it only if symbol mode really can't run.

Reading the JSON in Python

One more trap. The -format json output is a stream of pretty-printed objects one after another. jq reads that fine, Python's json.load doesn't:

json.decoder.JSONDecodeError: Extra data: line 13 column 1 (char 290)

You can't split it by lines either, since each object spans many lines. JSONDecoder.raw_decode reads one object and tells you where it stopped:

import json


def read_stream(text):
    dec = json.JSONDecoder()
    i, n = 0, len(text)
    while i < n:
        while i < n and text[i].isspace():
            i += 1
        if i >= n:
            break
        obj, i = dec.raw_decode(text, i)
        yield obj

The rest of ci/vulnsummary.py collects osv summaries, keeps findings whose first trace frame has a function (the reachable ones) and prints a Markdown table into the job summary. On my sample it printed "No reachable vulnerabilities.", which matched the text output.

The proper fix, later

The filter is a workaround. When someone has time, there are cleaner options, and Go ignores some directories on its own:

  • Rename the folder to _scripts. Directories starting with _ or ., and any called testdata, are skipped by ./....
  • Put each script in its own directory with one package name.
  • Since Go 1.25, go.mod takes an ignore directive.
go mod edit -ignore=./scripts
go list ./...
example.com/shop/cmd/api
example.com/shop/internal/orders

I tried that on a copy of the sample and it worked with plain ./..., no -e and no grep. I like it the most of the three, since it keeps the folder where people expect it. Until then the grep does the job, and the CI file says why in a comment so nobody "cleans it up" later.