moogacs / nilaway-plugin

It's a custom build plugin to add nilaway to golangci-lint.
Apache License 2.0
6 stars 1 forks source link

Configuration of nilaway using golangci-lint.yml requires nilaway_analyzer to also be built. #5

Open taimoorgit opened 8 months ago

taimoorgit commented 8 months ago

Steps to reproduce

  1. build the plugin
  2. copy to your project folder
  3. setup a config, like this:
linters-settings:
  custom:
    nilaway:
      path: ./bin/nilaway.so
      description: self-built nilaway plugin
      original-url: github.com/nilaway-plugin
      settings:
        pretty-print: false
  1. run golangci-lint run --disable-all -E nilaway
  2. no errors will be thrown but pretty-print is not actually disabled. --include-pkgs won't work etiher.

Reason

This is happening because the config isn't read by the plugin.New()

func New(conf any) ([]*analysis.Analyzer, error) {
    return []*analysis.Analyzer{nilaway.Analyzer}, nil
}

Part of the solution is to take this conf any and turn it into a map[string]any, then read key value pairs from there and feed them into the analyzer flag sets. However, it's more complicated than that.

nilaway has a multi-tiered architecture, where the config needs to actually be read by a separate analyzer that all analyzers are dependent on, called 'nilaway_config'

To address this problem, we have defined a separate "nilaway_config" analyzer which is only responsible for defining the Flags field and expose the configs through its return value. All other sub-analyzers will depend on the config.Analyzer and use the values there to execute different logic.

Reference

Solution

We need to also build nilaway_analyzer.so somehow.

The YAML config will need to look more like:

run:
  timeout: 2m
linters-settings:
  custom:
    nilaway_config:
      path: ./bin/nilaway_config.so
      description: self-built nilaway plugin
      original-url: github.com/nilaway_config-plugin
      settings:
        pretty-print: false

    nilaway:
      path: ./bin/nilaway.so
      description: self-built nilaway plugin
      original-url: github.com/nilaway-plugin

# like the nilaway/golangci-custom linter docs mention, you apply the golangci-related stuff (like excludes) on nilaway, but the actual flag setting gets done on nilaway_analyzer
issues:
  exclude-rules:
    - linters:
        - nilaway
      path: "cmd/terraform.go"
taimoorgit commented 8 months ago

@moogacs I have a branch locally on my machine where I have mostly fixed this problem - I am at the point where I can set flags like --pretty-print or --include-pkgs and they will be obeyed properly.

The downside is that golangci-lint throws an error message and exits with a non-zero code despite running nilaway and other linters just fine.

Will report back in a couple days when I get that PR ready.