Commit Graph

6 Commits

Author SHA1 Message Date
Julien 68524af701
Enable goheader rule and add missing license headers ()
This commit adds the `goheader` rule to `golangci-lint` to enforce that
all our Go source code includes appropriate licence headers, fixing up
files that currently fail that check.

---------

Co-authored-by: Will Jones <will@sacharissa.co.uk>
2024-09-09 12:05:45 +00:00
Zaid Ajaj 2394f0de7a
[docs/go-program-gen] Fix generating constructor syntax examples for kubernetes ()
In this PR we fix generating constructor syntax examples for Kubernetes.
The problem initially was due to generating a PCL program with syntax
errors for the kubernetes schema. The syntax error was happening because
we emitted properties `$ref` and `$schema` which are not valid
identifiers in PCL. Fixing this was simple enough, we only needed to
quote these properties that start with dollar signs and we get a valid
PCL program.

Full PCL program for kubernetes:
https://gist.github.com/Zaid-Ajaj/abe899430a0b5f99a428934dcac75d52

However, the valid PCL program wouldn't convert to Go with program-gen
and it would hang without showing any errors or stack traces. I wrote a
script to split the programs for each resource and convert each
separately. This way I narrowed down the problem to this program:

```
resource "def" "kubernetes:apiextensions.k8s.io/v1:CustomResourceDefinition" {
  apiVersion = "string"
  kind = "string"
  spec = {
    versions = [{}]
  }
}
```

Debugging this in Go program-gen didn't show an obvious error and it
kept on hanging. However I noticed that we lowering expressions twice:
once for the entirety of resource inputs when generating resources and
again when generating the expressions separately. Lowering expressions
has been the source of many bugs and it seems to trip up program-gen a
lot and suspected something wrong was going on here, so I simplified it
to lower the expressions once when we generate resource inputs. This
fixed the hang issue as well as a few of our test programs (see diffs
for test programs and tests schemas)

Tested this against the full kubernetes schema and we can now generate
the full Go program:
https://gist.github.com/Zaid-Ajaj/3ac734536969a989edae92219968d51f

> The second time we lower expressions not only is redundant but also
can generate invalid code if the lowered expressions have temporary
variables because program-gen would just emit these variables inside
inline expressions like objects and lists which gives invalid Go code.

Resolves part of 
2024-07-05 12:42:41 +00:00
Abhinav Gupta 7aa5b77a0c
all: Reformat with gofumpt
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.
2023-03-03 09:00:24 -08:00
pulumi-bot 73a66f48ea [breaking] Changing the version of go.mod in sdk / pkg to be v3 2021-04-14 19:32:18 +01:00
Evan Boyle 94cd67b564
[Go Program Gen] Improved type handling and invokes () 2020-06-05 18:52:00 -07:00
Evan Boyle 9b2c6d726d
[Go Program Gen] binary, unary, and conditional expression support () 2020-05-28 13:47:49 -07:00