uber-go / nilaway

Static analysis tool to detect potential nil panics in Go code
Apache License 2.0
3.04k stars 59 forks source link

How to use nilaway for a Golang project involved conditional compilation? #154

Open hxiaodon opened 9 months ago

hxiaodon commented 9 months ago

I tried running nilaway cmd for our internal project, but it failed with the following error

internal/app/xxx/domain/task.go:244:18: undefined: settings.XXXXX

settings.XXXXX is indeed defined in a source file with "//go:build $flagname" statement declared at the first line

The golangci-lint has the build-flag option while nilaway's analyzer deprecated it

user% ./nilaway --help
nilaway: Run NilAway on this package to report any possible flows of nil values to erroneous sites that our system can detect

Usage: nilaway [-flag] [package]

Flags:
  -V    print version and exit
  -all
        no effect (deprecated)
  -c int
        display offending line with this many lines of context (default -1)
  -cpuprofile string
        write CPU profile to this file
  -debug string
        debug flags, any subset of "fpstv"
  -exclude-errors-in-files string
        A comma-separated list of file prefixes to exclude from error reporting. This takes precedence over include-errors-in-files.
  -exclude-file-docstrings value
        Comma-separated list of docstrings to exclude from analysis
  -exclude-pkgs value
        Comma-separated list of packages to exclude from analysis
  -fix
        apply all suggested fixes
  -flags
        print analyzer flags in JSON
  -include-errors-in-files string
        A comma-separated list of file prefixes to report errors, default is current working directory. (default "/Users/tiger/Downloads/working/nilaway")
  -include-pkgs value
        Comma-separated list of packages to analyze
  -json
        emit JSON output
  -memprofile string
        write memory profile to this file
  -pretty-print value
        Pretty print the error messages
  -source
        no effect (deprecated)
  -tags string
        no effect (deprecated)
  -test
        indicates whether test files should be analyzed, too (default true)
  -trace string
        write trace log to this file
  -v    no effect (deprecated)

Any suggestion for this issue? Thanks~

yuxincs commented 8 months ago

We are using the singlechecker package from x/tools package: https://pkg.go.dev/golang.org/x/tools/go/analysis/singlechecker, and the flags you see there are not directly related to NilAway (populated by the singlechecker linter driver).

Here it seems that the singlechecker does not have proper support for build tags and we have very little control over that since singlechecker takes the entire main function entry away and handles the package loading logic (other than writing our own linter driver of course). There has been https://github.com/golang/go/issues/61324 that tries to implement an importable checker driver API, which would provide some flexibilities to configure the package loader. We are waiting for that to be available.

In the meantime, we can also hack around to see if it's possible to fix this (feel free to do so yourself and send a PR 😃 ). I'm keeping this issue open to track this.