Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[codegen/go] Improve optional params in invoke #8839

Merged
merged 4 commits into from
Feb 1, 2022

Conversation

AaronFriel
Copy link
Member

@AaronFriel AaronFriel commented Jan 26, 2022

As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.

Previously, code would be generated like so:

    policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
      Statements: []iam.GetPolicyDocumentStatement{
        iam.GetPolicyDocumentStatement{
          Sid: "1",
          //...

However "Sid" is of type *string.

This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.

In the new code generation, the above is instead rendered as:

    policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
      Statements: []iam.GetPolicyDocumentStatement{
        iam.GetPolicyDocumentStatement{
          Sid: pulumi.StringRef("1"),
          //...

@github-actions
Copy link

Diff for pulumi-random with merge commit 4352872

@github-actions
Copy link

Diff for pulumi-azuread with merge commit 4352872

@github-actions
Copy link

Diff for pulumi-kubernetes with merge commit 4352872

@github-actions
Copy link

Diff for pulumi-gcp with merge commit 4352872

@github-actions
Copy link

Diff for pulumi-azure with merge commit 4352872

@github-actions
Copy link

Diff for pulumi-aws with merge commit 4352872

@codecov
Copy link

codecov bot commented Jan 26, 2022

Codecov Report

Merging #8839 (ef6e697) into master (eb97581) will decrease coverage by 15.09%.
The diff coverage is 0.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master    #8839       +/-   ##
===========================================
- Coverage   59.35%   44.26%   -15.10%     
===========================================
  Files         641      635        -6     
  Lines       99407    97214     -2193     
  Branches     1389     1389               
===========================================
- Hits        59006    43033    -15973     
- Misses      37038    51198    +14160     
+ Partials     3363     2983      -380     
Impacted Files Coverage Δ
pkg/codegen/go/gen_program_expressions.go 0.00% <0.00%> (-78.00%) ⬇️
pkg/codegen/go/gen_program_optionals.go 0.00% <0.00%> (-94.21%) ⬇️
pkg/codegen/internal/test/program_driver.go 0.00% <ø> (-52.71%) ⬇️
sdk/go/pulumi/type_conversions.go 0.00% <0.00%> (ø)
pkg/codegen/docs.go 0.00% <0.00%> (-100.00%) ⬇️
pkg/codegen/go/gen_spill.go 0.00% <0.00%> (-100.00%) ⬇️
pkg/codegen/dotnet/templates.go 0.00% <0.00%> (-100.00%) ⬇️
pkg/codegen/hcl2/model/format/func.go 0.00% <0.00%> (-100.00%) ⬇️
pkg/codegen/go/utilities.go 0.00% <0.00%> (-90.00%) ⬇️
pkg/codegen/go/gen.go 0.00% <0.00%> (-89.71%) ⬇️
... and 126 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eb97581...ef6e697. Read the comment docs.

@github-actions
Copy link

Diff for pulumi-azure-native with merge commit 4352872

@t0yv0 t0yv0 self-requested a review January 27, 2022 16:25
@AaronFriel
Copy link
Member Author

@t0yv0 or @pgavlin would appreciate your insight on why this panics

@t0yv0
Copy link
Member

t0yv0 commented Jan 27, 2022

Let me return to this tomorrow

}


resource "aws_iam_policy" "example" {
Copy link
Member

@mikhailshilkov mikhailshilkov Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it panics because "example" is not a valid resource type. It tries to lookup a provider which is supposed to come from that resource type.

Suggested change
resource "aws_iam_policy" "example" {
resource aws_iam_policy "aws:iam:Policy" {

@@ -0,0 +1,53 @@
data "aws_iam_policy_document" "example" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This likely needs to be an invoke, see

subnets = invoke("aws:ec2:getSubnetIds", {
for example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, for my understanding this is because the .pp files are in PCL?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think pp = pulumi program.

@t0yv0
Copy link
Member

t0yv0 commented Jan 28, 2022

Apologies, slipping again, will look on Monday

@mikhailshilkov
Copy link
Member

@t0yv0 I think I responded to the initial question, I'm happy to help @AaronFriel here

@AaronFriel AaronFriel changed the title wip: panic on running tests? [codegen/go] Improve optional params in invoke Feb 1, 2022
@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-random with merge commit a3209b4

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azuread with merge commit a3209b4

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-kubernetes with merge commit a3209b4

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-gcp with merge commit a3209b4

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure with merge commit a3209b4

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-aws with merge commit a3209b4

@AaronFriel
Copy link
Member Author

AaronFriel commented Feb 1, 2022

@t0yv0 I've rewritten the optional spiller to track state as it recurses into an invoke, then an intrinsic __convert, then an object constructor. I attempted to use replace clauses in go.mod to regenerate the AWS SDK to validate, and it appears that (unrelated to my change) I can't seem to build exactly what's in pulumi-aws/sdk/go. Even with a clean version of pulumi SDK, I get many thousands of lines of diffs.

That said, I did run it and verified by hand that positive changes were visible in doc comments to remove hoisted variables:

  pulumi.Run(func(ctx *pulumi.Context) error {
    _, err := aws.GetRegions(ctx, &GetRegionsArgs{
      AllRegions: pulumi.BoolRef(true),
    }, nil)
     _, err = lb.LookupListener(ctx, &lb.LookupListenerArgs{
       LoadBalancerArn: pulumi.StringRef(selected.Arn),
       Port:            pulumi.IntRef(443),
     }, nil)

This looks to be a positive change everywhere I can see, but again, having difficulty even locally rebuilding the AWS provider SDK reproducibly, so I'd be hesitant to merge this without being able to do that.

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure-native with merge commit a3209b4

CHANGELOG_PENDING.md Show resolved Hide resolved
"github.com/pulumi/pulumi/sdk/v3/go/pulumi",
"../../../../../../sdk")},
"github.com/pulumi/pulumi/sdk/v3",
"../../../../../../../sdk")},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why has this changed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Go test were running via fluke before, resolving a different "github.com/pulumi/pulumi/sdk/v3" than the local copy. Until I changed this, tests were running using the published version of our SDK, I believe? I don't know Go module resolution well enough to say, but I do know that until I changed this, tests weren't running using my local changes.

Digging into the tests, they temporarily create a go.mod in a subdirectory like below, then run go mod tidy, then add a replacement for the Pulumi SDK. But I think that last step was a no-op.

testdata
├── aws-optionals-pp
│   ├── go
│   │   ├── aws-optionals.go 
│   │   └── go.mod # generated at test runtime

From the directory containing go.mod up to sdk is 7 levels:

testdata/aws-optionals-pp/go on  AaronFriel/issue8821 [$?⇕] via  v1.17.6 
❯ ls ../../../../../../../sdk
README.md  dotnet  go  go.mod  go.sum  nodejs  proto  python

@AaronFriel
Copy link
Member Author

Todo:

  • Split changelog codegen & new Go SDK APIs into two parts.

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-random with merge commit fb98f44

@AaronFriel AaronFriel marked this pull request as ready for review February 1, 2022 19:41
@AaronFriel
Copy link
Member Author

@t0yv0 removing your draft and I believe with this nit commit, all tests should be green again.

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-random with merge commit 95fdae9

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azuread with merge commit 95fdae9

As described in #8821, docs generated for helper functions can be incorrect because optional arguments to parameter objects are not correctly handled.

Previously, code would be generated like so:

```go
    policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
      Statements: []iam.GetPolicyDocumentStatement{
        iam.GetPolicyDocumentStatement{
          Sid: "1",
          //...
```

However "Sid" is of type `*string`.

This four helper conversion functions, to handle the primitive types we lower from, and modifies codegen to recursively apply these functions inside of an invoke call.

In the new code generation, the above is instead rendered as:

```go
    policyDocument, err := iam.GetPolicyDocument(ctx, &iam.GetPolicyDocumentArgs{
      Statements: []iam.GetPolicyDocumentStatement{
        iam.GetPolicyDocumentStatement{
          Sid: pulumi.StringRef("1"),
          //...
```
@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-kubernetes with merge commit 95fdae9

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-random with merge commit bd241b3

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-gcp with merge commit 95fdae9

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azuread with merge commit bd241b3

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-kubernetes with merge commit bd241b3

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-gcp with merge commit bd241b3

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure with merge commit 95fdae9

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure with merge commit bd241b3

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-aws with merge commit 95fdae9

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-aws with merge commit bd241b3

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure-native with merge commit 95fdae9

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure-native with merge commit bd241b3

@AaronFriel AaronFriel merged commit 0295ce9 into master Feb 1, 2022
@pulumi-bot pulumi-bot deleted the AaronFriel/issue8821 branch February 1, 2022 20:58
@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-random with merge commit 13fd9de

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azuread with merge commit 13fd9de

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-kubernetes with merge commit 13fd9de

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-gcp with merge commit 13fd9de

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure with merge commit 13fd9de

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-aws with merge commit 13fd9de

@github-actions
Copy link

github-actions bot commented Feb 1, 2022

Diff for pulumi-azure-native with merge commit 13fd9de

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants