govet includes a nilness linter that detects a few nil issues.
This linter is not enabled by default in govet because the `go` tool
does not want a dependency on the libraries necessary to implement this
(golang/go#59714).
Enable the nilness linter and fix the following issues found by it
(commentary below each check mine):
pkg/cmd/pulumi/new.go:283:9: nilness: impossible condition: nil != nil (govet)
^- the error was very likely supposed to be the `os.Remove`
pkg/cmd/pulumi/new.go:633:10: nilness: impossible condition: nil != nil (govet)
^- same error checked on the previous line
pkg/cmd/pulumi/preview.go:190:11: nilness: impossible condition: nil != nil (govet)
^- same error checked a few blocks above
pkg/codegen/pcl/binder_component.go:101:64: nilness: nil dereference in dynamic method call (govet)
^- err is guaranteed nil
pkg/codegen/pcl/binder_component.go:133:10: nilness: impossible condition: nil != nil (govet)
^- err is guaranteed nil
sdk/go/auto/errors_test.go:374:34: nilness: nil dereference in index operation (govet)
^- this is intentional; I replaced it with a deliberate panic
tests/integration/construct_component_methods_resources/testcomponent-go/random.go:30:10: nilness: impossible condition: non-nil == nil (govet)
^- args is rejected if it's nil
Per team discussion, switching to gofumpt.
[gofumpt][1] is an alternative, stricter alternative to gofmt.
It addresses other stylistic concerns that gofmt doesn't yet cover.
[1]: https://github.com/mvdan/gofumpt
See the full list of [Added rules][2], but it includes:
- Dropping empty lines around function bodies
- Dropping unnecessary variable grouping when there's only one variable
- Ensuring an empty line between multi-line functions
- simplification (`-s` in gofmt) is always enabled
- Ensuring multi-line function signatures end with
`) {` on a separate line.
[2]: https://github.com/mvdan/gofumpt#Added-rules
gofumpt is stricter, but there's no lock-in.
All gofumpt output is valid gofmt output,
so if we decide we don't like it, it's easy to switch back
without any code changes.
gofumpt support is built into the tooling we use for development
so this won't change development workflows.
- golangci-lint includes a gofumpt check (enabled in this PR)
- gopls, the LSP for Go, includes a gofumpt option
(see [installation instrutions][3])
[3]: https://github.com/mvdan/gofumpt#installation
This change was generated by running:
```bash
gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error)
```
The following files were manually tweaked afterwards:
- pkg/cmd/pulumi/stack_change_secrets_provider.go:
one of the lines overflowed and had comments in an inconvenient place
- pkg/cmd/pulumi/destroy.go:
`var x T = y` where `T` wasn't necessary
- pkg/cmd/pulumi/policy_new.go:
long line because of error message
- pkg/backend/snapshot_test.go:
long line trying to assign three variables in the same assignment
I have included mention of gofumpt in the CONTRIBUTING.md.
This allows the pulumi-language-go plugin to start up providers directly
from .go source files.
The other language providers will be extended to support this as well in
time.