Skip to content

Typed rules don't work as expected #1277

@ldez

Description

@ldez

Describe the bug

The following rules don't work as expected:

  • context-keys-type
  • errorf
  • modifies-value-receiver
  • range-val-address
  • string-of-int
  • time-equal
  • time-naming
  • unexported-return
  • unhandled-error
  • var-declaration

To Reproduce

cd /tmp
git clone --branch revive https://github.com/ldez/fiber.git
cd fiber

# version 1.7.0 (I used the official binary)
revive --version

# Output: 24
revive -config=revive.toml ./... | wc -l

# Output: 3
GOROOT= revive -config=revive.toml ./... | wc -l
[rule.unhandled-error]

Expected behavior

I expect the rules to be less dependent on the environment and the compilation flags.

Logs

NA

Desktop (please complete the following information):

  • OS: Linux
  • go 1.24

Additional context

The problem relates to file.Pkg.TypeCheck(): the default importer uses GOROOT and GOPATH.

If you log the error returns by file.Pkg.TypeCheck(), you can see what happened.

Note, go env GOROOT always returns a value even if the env var is not set.

For example, GitHub Action runners don't have an explicit env var GOROOT.

The problem is increased if the binary is built with -trimpath.

For now, I haven't found a way to reproduce the same environment locally as GitHub Action runners, but I'm using an SSH connection to a runner to test it.

Details
runner@fv-az1337-715:~/work/fiber/fiber$ go version
go version go1.24.1 linux/amd64
runner@fv-az1337-715:~/work/fiber/fiber$
runner@fv-az1337-715:~/work/fiber/fiber$ env | grep GOROOT
GOROOT_1_21_X64=/opt/hostedtoolcache/go/1.21.13/x64
GOROOT_1_23_X64=/opt/hostedtoolcache/go/1.23.7/x64
GOROOT_1_22_X64=/opt/hostedtoolcache/go/1.22.12/x64
runner@fv-az1337-715:~/work/fiber/fiber$
runner@fv-az1337-715:~/work/fiber/fiber$ go env GOROOT
/opt/hostedtoolcache/go/1.24.1/x64
runner@fv-az1337-715:~/work/fiber/fiber$
runner@fv-az1337-715:~/work/fiber/fiber$ cd /tmp
runner@fv-az1337-715:~/work/fiber/fiber$ git clone -q --branch v1.7.0 https://github.com/mgechev/revive.git && cd revive
runner@fv-az1337-715:~/work/fiber/fiber$
runner@fv-az1337-715:/tmp/revive$ go build -trimpath -o revive-trim .; go build -o revive-notrim .
...
runner@fv-az1337-715:/tmp/revive$ cd ~/work/fiber/fiber
runner@fv-az1337-715:~/work/fiber/fiber$ /tmp/revive/revive-notrim -config=revive.toml ./... | wc -l
24
runner@fv-az1337-715:~/work/fiber/fiber$ /tmp/revive/revive-trim -config=revive.toml ./... | wc -l
3

https://github.com/ldez/fiber/actions/runs/13937713921/job/39008703769

You can fork my branch https://github.com/ldez/fiber/tree/revive if you need it, the environment is already prepared (with SSH debug).

EDIT: I found a way to reproduce outside GitHub Action runners.

Details
$ docker run --rm -it golang:1.24-alpine sh
/go # apk add -q git
/go # go version
go version go1.24.1 linux/amd64
/go # env | grep GOROOT
/go # env | grep GOPATH
GOPATH=/go
/go # go env GOROOT
/usr/local/go
/go # go env GOPATH
/go
/go # cd /tmp
/tmp # git clone -q --branch revive https://github.com/ldez/fiber.git
/tmp # git clone -q --branch v1.7.0 https://github.com/mgechev/revive.git
...
/tmp # cd revive
/tmp/revive # go build -trimpath -o revive-trim .; go build -o revive-notrim .
...
/tmp/revive # cd /tmp/fiber
/tmp/fiber # /tmp/revive/revive-notrim -config=revive.toml ./... | wc -l
24
/tmp/fiber # /tmp/revive/revive-trim -config=revive.toml ./... | wc -l
3
/tmp/fiber # 

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions