From dca6f890a4f7675c4d51c3a84611c822c79bd617 Mon Sep 17 00:00:00 2001 From: Zaid Ajaj Date: Tue, 24 Dec 2024 19:28:33 +0100 Subject: [PATCH] [program-gen] Emit the Deployment class when using PCL functions stack() and projectName() (#1518) Fixes https://github.com/pulumi/pulumi/issues/16385 --- CHANGELOG_PENDING.md | 2 ++ pkg/codegen/java/gen_program.go | 33 +++++++++++++------ .../java/pulumi-builtins.java | 23 +++++++++++++ .../pulumi-builtins-pp/pulumi-builtins.pp | 7 ++++ 4 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 pkg/codegen/testing/test/testdata/pulumi-builtins-pp/java/pulumi-builtins.java create mode 100644 pkg/codegen/testing/test/testdata/pulumi-builtins-pp/pulumi-builtins.pp diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index e10c0aeb935..9851cac33d0 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -3,3 +3,5 @@ - Update to Pulumi 3.143.0 ### Bug Fixes + +- [Convert] Emit the `Deployment` class when using Pulumi built-in functions in PCL `stack()` and `projectName()` \ No newline at end of file diff --git a/pkg/codegen/java/gen_program.go b/pkg/codegen/java/gen_program.go index edbb7ab8aba..e95a8106688 100644 --- a/pkg/codegen/java/gen_program.go +++ b/pkg/codegen/java/gen_program.go @@ -654,6 +654,10 @@ func (g *generator) collectFunctionCallImports(functionCall *model.FunctionCallE imports = append(imports, collectObjectImports(argumentsExpr, argumentExprType)...) } } + case "stack", "projectName": + // stack() and projectName() functions are pulumi built-ins + // they require the Deployment class + imports = append(imports, "com.pulumi.deployment.Deployment") } return imports @@ -678,24 +682,33 @@ func removeDuplicates(inputs []string) []string { // configuration or used in function calls func (g *generator) collectImports(nodes []pcl.Node) []string { imports := make([]string, 0) + + visitFunctionCalls := func(expr model.Expression) (model.Expression, hcl.Diagnostics) { + switch expr := expr.(type) { + case *model.FunctionCallExpression: + imports = append(imports, g.collectFunctionCallImports(expr)...) + } + return expr, nil + } + for _, node := range nodes { switch node := node.(type) { case *pcl.Resource: // collect resource imports resource := node imports = append(imports, collectResourceImports(resource)...) + for _, prop := range resource.Inputs { + _, diags := model.VisitExpression(prop.Value, model.IdentityVisitor, visitFunctionCalls) + g.diagnostics = append(g.diagnostics, diags...) + } case *pcl.LocalVariable: localVariable := node - switch localVariable.Definition.Value.(type) { - case *model.FunctionCallExpression: - // collect function invoke imports - // traverse the args and inner objects recursively - functionCall := localVariable.Definition.Value.(*model.FunctionCallExpression) - _, isInvokeCall := g.isFunctionInvoke(localVariable) - if isInvokeCall { - imports = append(imports, g.collectFunctionCallImports(functionCall)...) - } - } + _, diags := model.VisitExpression(localVariable.Definition.Value, model.IdentityVisitor, visitFunctionCalls) + g.diagnostics = append(g.diagnostics, diags...) + case *pcl.OutputVariable: + outputVariable := node + _, diags := model.VisitExpression(outputVariable.Value, model.IdentityVisitor, visitFunctionCalls) + g.diagnostics = append(g.diagnostics, diags...) } } diff --git a/pkg/codegen/testing/test/testdata/pulumi-builtins-pp/java/pulumi-builtins.java b/pkg/codegen/testing/test/testdata/pulumi-builtins-pp/java/pulumi-builtins.java new file mode 100644 index 00000000000..3e95d342db2 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/pulumi-builtins-pp/java/pulumi-builtins.java @@ -0,0 +1,23 @@ +package generated_program; + +import com.pulumi.Context; +import com.pulumi.Pulumi; +import com.pulumi.core.Output; +import com.pulumi.deployment.Deployment; +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class App { + public static void main(String[] args) { + Pulumi.run(App::stack); + } + + public static void stack(Context ctx) { + ctx.export("stackName", Deployment.getInstance().getStackName()); + ctx.export("projectName", Deployment.getInstance().getProjectName()); + } +} diff --git a/pkg/codegen/testing/test/testdata/pulumi-builtins-pp/pulumi-builtins.pp b/pkg/codegen/testing/test/testdata/pulumi-builtins-pp/pulumi-builtins.pp new file mode 100644 index 00000000000..5aa237cca80 --- /dev/null +++ b/pkg/codegen/testing/test/testdata/pulumi-builtins-pp/pulumi-builtins.pp @@ -0,0 +1,7 @@ +output "stackName" { + value = stack() +} + +output "projectName" { + value = project() +} \ No newline at end of file