Skip to content

Commit

Permalink
Convert path.root and path.cwd to rootDirectory and cwd
Browse files Browse the repository at this point in the history
cwd has a warning emitted letting the user know that it will not behave
the same as it does in terraform, but as per the discussion on
pulumi/pulumi#18565 this should be ok and we can fix it later if it is
still blocking.
  • Loading branch information
brandonpollack23 committed Feb 28, 2025
1 parent b3d914d commit 3f9a7d8
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- Implement `toset` through the `pulumi-std` invoke of the same name
- Implement `cidrsubnets` through the `pulumi-std` invoke of the same name
- Implement `formatlist` through the `pulumi-std` invoke of the same name
- Add path.root and path.cwd to the converter to pcl intrinsics projectRoot and cwd respectively.

### Bug Fixes

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/pulumi/pulumi-converter-terraform

go 1.22.3

toolchain go1.23.3
toolchain go1.24.0

replace github.com/opentofu/opentofu/shim => ./pkg/internal/shim

Expand All @@ -14,8 +14,8 @@ require (
github.com/hashicorp/terraform-svchost v0.1.1
github.com/opentofu/opentofu/shim v0.0.0-00010101000000-000000000000
github.com/pulumi/pulumi-terraform-bridge/v3 v3.103.1-0.20250225074029-c535e789aacb
github.com/pulumi/pulumi/pkg/v3 v3.152.0
github.com/pulumi/pulumi/sdk/v3 v3.152.0
github.com/pulumi/pulumi/pkg/v3 v3.152.1-0.20250227003515-e15af80ca210
github.com/pulumi/pulumi/sdk/v3 v3.152.1-0.20250227083209-2cc7e08f9eee
github.com/pulumi/terraform v0.12.1-0.20230322133416-a268cd0892c9
github.com/spf13/afero v1.11.0
github.com/spf13/pflag v1.0.5
Expand Down
20 changes: 8 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2 h1:l+bLFvHjqtgNQwWxwrFX9PemGAAO2P1AGZM7zlMNvCs=
github.com/hashicorp/terraform-config-inspect v0.0.0-20210209133302-4fd17a0faac2/go.mod h1:Z0Nnk4+3Cy89smEbrq+sl1bxc9198gIP4I7wcQF6Kqs=
github.com/hashicorp/terraform-plugin-go v0.24.0 h1:2WpHhginCdVhFIrWHxDEg6RBn3YaWzR2o6qUeIEat2U=
github.com/hashicorp/terraform-plugin-go v0.24.0/go.mod h1:tUQ53lAsOyYSckFGEefGC5C8BAaO0ENqzFd3bQeuYQg=
github.com/hashicorp/terraform-plugin-go v0.26.0 h1:cuIzCv4qwigug3OS7iKhpGAbZTiypAfFQmw8aE65O2M=
github.com/hashicorp/terraform-plugin-go v0.26.0/go.mod h1:+CXjuLDiFgqR+GcrM5a2E2Kal5t5q2jb0E3D57tTdNY=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
github.com/hashicorp/terraform-plugin-sdk v1.7.0 h1:B//oq0ZORG+EkVrIJy0uPGSonvmXqxSzXe8+GhknoW0=
Expand Down Expand Up @@ -713,18 +713,14 @@ github.com/pulumi/providertest v0.1.3 h1:GpNKRy/haNjRHiUA9bi4diU4Op2zf3axYXbga5A
github.com/pulumi/providertest v0.1.3/go.mod h1:GcsqEGgSngwaNOD+kICJPIUQlnA911fGBU8HDlJvVL0=
github.com/pulumi/pulumi-java/pkg v0.19.0 h1:T9kkGUQJV7UTxenw08m3txsgQkNVnZZxvn1zCcNjaE8=
github.com/pulumi/pulumi-java/pkg v0.19.0/go.mod h1:YKYYFEb3Jvzf/dDJo0xOeEkIfBAMkkkdhXulauvEjmc=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.100.0 h1:L03nXHLprXuF0wIihoKFIjpE8oSPomiMayrRVDv1VgY=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.100.0/go.mod h1:r+cxSrRxwCRbHMdKMKWU3NKDcUTm9xa9PcBDuHQnRkI=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.103.0 h1:kHY3wnVOutj1G3XcjA4ZNSlHbrUKHfMTZsJRykTYc48=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.103.0/go.mod h1:2+e4IIfbop+OBZYcMvlgx9KPZ9J6VrweKHadjnYyI6M=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.103.1-0.20250225074029-c535e789aacb h1:ym1QhTPKtp5BqU/rekd8OmvtPoxeSD1uZE2lswF9rEU=
github.com/pulumi/pulumi-terraform-bridge/v3 v3.103.1-0.20250225074029-c535e789aacb/go.mod h1:UjexyX2uPdBlLJKZF2v8JsJb7z7mLiVfcwgxwt9fi6Q=
github.com/pulumi/pulumi-yaml v1.12.0 h1:ThJP+EBqeJyCnS6w6/PwcEFOT5o112qv0lObhefmFCk=
github.com/pulumi/pulumi-yaml v1.12.0/go.mod h1:EhZd1XDfuLa15O51qVVE16U6r8ldK9mLIBclqWCX27Y=
github.com/pulumi/pulumi/pkg/v3 v3.152.0 h1:SDhBD12bD5UoxwyLBHVBbfBC+iul1GoIgoHwtSfciI4=
github.com/pulumi/pulumi/pkg/v3 v3.152.0/go.mod h1:WzIlIXzgRvu7pD1bS/5/5Z9Zs4vdHRs5gyjE7GcaPbo=
github.com/pulumi/pulumi/sdk/v3 v3.152.0 h1:sPstmYhbf/ArkREjkPYRgFUf+iv1llskLY2/4aASdxc=
github.com/pulumi/pulumi/sdk/v3 v3.152.0/go.mod h1:+WC9aIDo8fMgd2g0jCHuZU2S/VYNLRAZ3QXt6YVgwaA=
github.com/pulumi/pulumi/pkg/v3 v3.152.1-0.20250227003515-e15af80ca210 h1:4YjNkqQXF8b0Hh7h21WbmKKK/3aCxyt7kLgeKaAApBs=
github.com/pulumi/pulumi/pkg/v3 v3.152.1-0.20250227003515-e15af80ca210/go.mod h1:WzIlIXzgRvu7pD1bS/5/5Z9Zs4vdHRs5gyjE7GcaPbo=
github.com/pulumi/pulumi/sdk/v3 v3.152.1-0.20250227083209-2cc7e08f9eee h1:o3rti7wG/IrQTGG4LaukYFsVZ+VfS7P235GsWke73TU=
github.com/pulumi/pulumi/sdk/v3 v3.152.1-0.20250227083209-2cc7e08f9eee/go.mod h1:+WC9aIDo8fMgd2g0jCHuZU2S/VYNLRAZ3QXt6YVgwaA=
github.com/pulumi/schema-tools v0.1.2 h1:Fd9xvUjgck4NA+7/jSk7InqCUT4Kj940+EcnbQKpfZo=
github.com/pulumi/schema-tools v0.1.2/go.mod h1:62lgj52Tzq11eqWTIaKd+EVyYAu5dEcDJxMhTjvMO/k=
github.com/pulumi/terraform v0.12.1-0.20230322133416-a268cd0892c9 h1:VHeasqoSdMgpxw8Rx46TybHixfnBlCk0i0JLDusNn4Y=
Expand Down Expand Up @@ -1414,8 +1410,8 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
5 changes: 2 additions & 3 deletions pkg/convert/testdata/programs/builtin_functions/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ locals {
# the current module, but tf2pulumi doesn't support that so we replace it with local.path_module.
path_module = "some/path"

# Some of the examples in the docs use `path.root` which _should_ resolve to the file system path of the
# root module of the configuration, but tf2pulumi doesn't support that so we replace it with local.path_root.
# Instead of using path.root use a local to not conflate testing of
# path.root conversion with uses of a path.
path_root = "root/path"

# The `can` examples make use of a local `foo`.
Expand Down Expand Up @@ -1227,4 +1227,3 @@ output "funcYamlencode2" {
output "funcZipmap" {
value = zipmap(["a", "b"], [1, 2])
}

4 changes: 2 additions & 2 deletions pkg/convert/testdata/programs/builtin_functions/pcl/main.pp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# the current module, but tf2pulumi doesn't support that so we replace it with local.path_module.
pathModule = "some/path"

# Some of the examples in the docs use `path.root` which _should_ resolve to the file system path of the
# root module of the configuration, but tf2pulumi doesn't support that so we replace it with local.path_root.
# Instead of using path.root use a local to not conflate testing of
# path.root conversion with uses of a path.
pathRoot = "root/path"

# The `can` examples make use of a local `foo`.
Expand Down
10 changes: 5 additions & 5 deletions pkg/convert/testdata/programs/builtin_inputs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ module "some_module" {
source = "./mod"
}

output "module_path" {
output "module_path_output" {
value = module.some_module.output
}

output "root" {
output "root_output" {
value = path.root
}

output "cwd" {
output "cwd_output" {
value = path.cwd
}

output "workspace" {
output "workspace_output" {
value = terraform.workspace
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[
"warning:builtin_inputs/mod/main.tf:2,17-24:builtin_inputs/mod/main.tf:2,13-24:Terraform input not yet implemented:path",
"warning:builtin_inputs/main.tf:10,17-22:builtin_inputs/main.tf:10,13-22:Terraform input not yet implemented:path",
"warning:builtin_inputs/main.tf:14,17-21:builtin_inputs/main.tf:14,13-21:Terraform input not yet implemented:path",
"warning:builtin_inputs/main.tf:14,13-21:builtin_inputs/main.tf:14,17-21:Converting the builtin variable path.cwd with differing behavior:The builtin Terraform variable path.cwd is being converted, but in Pulumi cwd will start at the \nproject program directory, not the execution directory",
"warning:builtin_inputs/main.tf:18,22-32:builtin_inputs/main.tf:18,13-32:Terraform input not yet implemented:terraform"
]
12 changes: 6 additions & 6 deletions pkg/convert/testdata/programs/builtin_inputs/pcl/main.pp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
component "someModule" "./mod" {
}

output "modulePath" {
output "modulePathOutput" {
value = someModule.output
}

output "root" {
value = notImplemented("path.root")
output "rootOutput" {
value = rootDirectory()
}

output "cwd" {
value = notImplemented("path.cwd")
output "cwdOutput" {
value = cwd()
}

output "workspace" {
output "workspaceOutput" {
value = notImplemented("terraform.workspace")
}
16 changes: 13 additions & 3 deletions pkg/convert/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,9 +1517,7 @@ func rewriteTraversal(
}

if matches("terraform", "workspace") ||
matches("path", "cwd") ||
matches("path", "module") ||
matches("path", "root") {
matches("path", "module") {
// If this is one of the builtin terraform inputs we just rewrite it to notImplemented.
state.appendDiagnostic(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Expand All @@ -1529,6 +1527,18 @@ func rewriteTraversal(
Subject: &contextRange,
})
return notImplemented(state, getTraversalRange(traversal))
} else if matches("path", "root") {
return hclwrite.TokensForFunctionCall("rootDirectory")
} else if matches("path", "cwd") {
state.diagnostics = append(state.diagnostics, &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Converting the builtin variable path.cwd with differing behavior",
Detail: `The builtin Terraform variable path.cwd is being converted, but in Pulumi cwd will start at the
project program directory, not the execution directory`,
Subject: &subjectRange,
Context: &contextRange,
})
return hclwrite.TokensForFunctionCall("cwd")
} else if root.Name == "var" && maybeFirstAttr != nil {
// This is a lookup of a var etc, we need to rewrite this traversal such that the root is now the
// pulumi config value instead.
Expand Down

0 comments on commit 3f9a7d8

Please sign in to comment.