diff --git a/CHANGELOG.md b/CHANGELOG.md index fcdbae6871..1b78fd2ddc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ BUG FIXES: * Fix crash in tofu test when using deprecated outputs ([#3249](https://github.com/opentofu/opentofu/pull/3249)) +* Fix missing provider functions when parentheses are used ([#3402](https://github.com/opentofu/opentofu/pull/3402)) ## 1.10.6 diff --git a/go.mod b/go.mod index ab6ea982d0..566c506f8b 100644 --- a/go.mod +++ b/go.mod @@ -277,4 +277,4 @@ require ( go 1.24.6 -replace github.com/hashicorp/hcl/v2 v2.20.1 => github.com/opentofu/hcl/v2 v2.20.2-0.20250121132637-504036cd70e7 +replace github.com/hashicorp/hcl/v2 v2.20.1 => github.com/opentofu/hcl/v2 v2.20.2-0.20251021132045-587d123c2828 diff --git a/go.sum b/go.sum index e436f18d40..4a64456936 100644 --- a/go.sum +++ b/go.sum @@ -926,8 +926,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= -github.com/opentofu/hcl/v2 v2.20.2-0.20250121132637-504036cd70e7 h1:QHUIrylb/q3pQdQcnAr74cGWsXS1lmA8GqP+RWFMK6U= -github.com/opentofu/hcl/v2 v2.20.2-0.20250121132637-504036cd70e7/go.mod h1:k+HgkLpoWu9OS81sy4j1XKDXaWm/rLysG33v5ibdDnc= +github.com/opentofu/hcl/v2 v2.20.2-0.20251021132045-587d123c2828 h1:r2WiJxljn/Cxwpq9zMZ5HomJcNwmGNjKmeZnFsxkpBg= +github.com/opentofu/hcl/v2 v2.20.2-0.20251021132045-587d123c2828/go.mod h1:k+HgkLpoWu9OS81sy4j1XKDXaWm/rLysG33v5ibdDnc= github.com/opentofu/registry-address v0.0.0-20230920144404-f1e51167f633 h1:81TBkM/XGIFlVvyabp0CJl00UHeVUiQjz0fddLMi848= github.com/opentofu/registry-address v0.0.0-20230920144404-f1e51167f633/go.mod h1:HzQhpVo/NJnGmN+7FPECCVCA5ijU7AUcvf39enBKYOc= github.com/packer-community/winrmcp v0.0.0-20180921211025-c76d91c1e7db h1:9uViuKtx1jrlXLBW/pMnhOfzn3iSEdLase/But/IZRU= diff --git a/internal/lang/functions.go b/internal/lang/functions.go index 7ad69d0078..57f500c228 100644 --- a/internal/lang/functions.go +++ b/internal/lang/functions.go @@ -25,7 +25,8 @@ var impureFunctions = []string{ "uuid", } -// This should probably be replaced with addrs.Function everywhere +// CoreNamespace defines the string prefix used for all core namespaced functions +// TODO: This should probably be replaced with addrs.Function everywhere const CoreNamespace = addrs.FunctionNamespaceCore + "::" // Functions returns the set of functions that should be used to when evaluating diff --git a/internal/lang/references_test.go b/internal/lang/references_test.go new file mode 100644 index 0000000000..8b87cdf97a --- /dev/null +++ b/internal/lang/references_test.go @@ -0,0 +1,102 @@ +// Copyright (c) The OpenTofu Authors +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2023 HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package lang + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" + + "github.com/opentofu/opentofu/internal/addrs" +) + +func TestReferencesInExpr(t *testing.T) { + // Note for developers, this list is non-exhaustive right now + // and there are many more expression types. This test is mainly to ensure that provider + // defined function references are returned from references with parentheses + // to resolve issue opentofu/opentofu#3401 + tests := map[string]struct { + exprSrc string + wantFuncRefs []string // List of expected provider function references + }{ + "no functions": { + exprSrc: `"literal"`, + wantFuncRefs: nil, + }, + "provider function without parentheses": { + exprSrc: `provider::testing::echo("hello")`, + wantFuncRefs: []string{"provider::testing::echo"}, + }, + "provider function with parentheses": { + exprSrc: `(provider::testing::echo("hello"))`, + wantFuncRefs: []string{"provider::testing::echo"}, + }, + "provider function in binary expression": { + exprSrc: `(provider::testing::add(1, 2)) + 3`, + wantFuncRefs: []string{"provider::testing::add"}, + }, + "nested parentheses": { + exprSrc: `((provider::testing::echo("hello")))`, + wantFuncRefs: []string{"provider::testing::echo"}, + }, + "provider function in conditional true": { + exprSrc: `true ? provider::testing::echo("yes") : "no"`, + wantFuncRefs: []string{"provider::testing::echo"}, + }, + "provider function in conditional false": { + exprSrc: `false ? "yes" : provider::testing::echo("no")`, + wantFuncRefs: []string{"provider::testing::echo"}, + }, + "provider function in conditional condition": { + exprSrc: `provider::testing::is_true() ? "yes" : "no"`, + wantFuncRefs: []string{"provider::testing::is_true"}, + }, + "multiple provider functions": { + exprSrc: `(provider::testing::add(1, 2)) + (provider::testing::mul(3, 4))`, + wantFuncRefs: []string{"provider::testing::add", "provider::testing::mul"}, + }, + "provider function with alias": { + exprSrc: `(provider::testing::custom::echo("hello"))`, + wantFuncRefs: []string{"provider::testing::custom::echo"}, + }, + "core function not included": { + exprSrc: `(core::max(1, 2))`, + wantFuncRefs: nil, // core functions are not provider functions + }, + "builtin function not included": { + exprSrc: `(length([1, 2, 3]))`, + wantFuncRefs: nil, // builtin functions are not provider functions + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + expr, diags := hclsyntax.ParseExpression([]byte(test.exprSrc), "test.tf", hcl.Pos{Line: 1, Column: 1}) + if diags.HasErrors() { + t.Fatalf("Failed to parse expression: %s", diags.Error()) + } + + refs, refDiags := ReferencesInExpr(addrs.ParseRef, expr) + if refDiags.HasErrors() { + t.Errorf("Unexpected diagnostics: %s", refDiags.Err()) + } + + // Extract provider function references + var gotFuncRefs []string + for _, ref := range refs { + if provFunc, ok := ref.Subject.(addrs.ProviderFunction); ok { + gotFuncRefs = append(gotFuncRefs, provFunc.String()) + } + } + + if diff := cmp.Diff(test.wantFuncRefs, gotFuncRefs); diff != "" { + t.Errorf("Wrong provider function references (-want +got):\n%s", diff) + } + }) + } +}