Skip to content

Commit 7355485

Browse files
Backport of stacks: Make path.module and path.root relative, to match documentation into v1.14 (#38008)
* backport of commit 116646a --------- Co-authored-by: Nick Fagerlund <[email protected]>
1 parent c716e32 commit 7355485

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: 'stacks: change absolute paths in path.module/path.root to be relative, as documented'
3+
time: 2025-12-09T23:00:00.316597+00:00
4+
custom:
5+
Issue: "37982"

internal/configs/source_bundle_parser.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,31 @@ func (p *SourceBundleParser) LoadConfigDir(source sourceaddrs.FinalSource) (*Mod
6767
})
6868
return nil, diags
6969
}
70-
mod.SourceDir = sourceDir
70+
71+
// The result of sources.LocalPathForSource is an absolute path, but we
72+
// don't actually want to pass an absolute path for a module's SourceDir;
73+
// doing so will cause the value of `path.module` in Terraform configs to
74+
// differ across plans and applies, since tfc-agent performs plans and
75+
// applies in temporary directories. Instead, we try to resolve a relative
76+
// path from Terraform's working directory, which should always be a
77+
// reasonable SourceDir value.
78+
workDir, err := os.Getwd()
79+
if err != nil {
80+
diags = diags.Append(&hcl.Diagnostic{
81+
Severity: hcl.DiagError,
82+
Summary: "Cannot resolve working directory",
83+
Detail: fmt.Sprintf("Failed to resolve current working directory: %s. This is a bug in Terraform - please report it.", err),
84+
})
85+
}
86+
relativeSourceDir, err := filepath.Rel(workDir, sourceDir)
87+
if err != nil {
88+
diags = diags.Append(&hcl.Diagnostic{
89+
Severity: hcl.DiagError,
90+
Summary: "Cannot resolve relative path",
91+
Detail: fmt.Sprintf("Failed to resolve relative path to module directory: %s. This is a bug in Terraform - please report it.", err),
92+
})
93+
}
94+
mod.SourceDir = relativeSourceDir
7195

7296
return mod, diags
7397
}

internal/stacks/stackruntime/internal/stackeval/planning_test.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"os"
10-
"path/filepath"
1110
"strings"
1211
"testing"
1312
"time"
@@ -852,29 +851,21 @@ func TestPlanning_PathValues(t *testing.T) {
852851
t.Fatalf("failed to get current working directory: %s", err)
853852
}
854853

855-
normalizePath := func(path string) string {
856-
rel, err := filepath.Rel(cwd, path)
857-
if err != nil {
858-
t.Errorf("rel(%s,%s): %s", cwd, path, err)
859-
return path
860-
}
861-
return rel
862-
}
863-
854+
// path.cwd should be absolute and all others should be relative, as per documentation.
864855
expected := map[string]string{
865-
"cwd": ".",
856+
"cwd": cwd,
866857
"root": "testdata/sourcebundle/planning/path_values/module", // this is the root module of the component
867858
"module": "testdata/sourcebundle/planning/path_values/module", // this is the root module
868859
"child_root": "testdata/sourcebundle/planning/path_values/module", // should be the same for all modules
869860
"child_module": "testdata/sourcebundle/planning/path_values/module/child", // this is the child module
870861
}
871862

872863
actual := map[string]string{
873-
"cwd": normalizePath(component.PlannedOutputValues[addrs.OutputValue{Name: "cwd"}].AsString()),
874-
"root": normalizePath(component.PlannedOutputValues[addrs.OutputValue{Name: "root"}].AsString()),
875-
"module": normalizePath(component.PlannedOutputValues[addrs.OutputValue{Name: "module"}].AsString()),
876-
"child_root": normalizePath(component.PlannedOutputValues[addrs.OutputValue{Name: "child_root"}].AsString()),
877-
"child_module": normalizePath(component.PlannedOutputValues[addrs.OutputValue{Name: "child_module"}].AsString()),
864+
"cwd": component.PlannedOutputValues[addrs.OutputValue{Name: "cwd"}].AsString(),
865+
"root": component.PlannedOutputValues[addrs.OutputValue{Name: "root"}].AsString(),
866+
"module": component.PlannedOutputValues[addrs.OutputValue{Name: "module"}].AsString(),
867+
"child_root": component.PlannedOutputValues[addrs.OutputValue{Name: "child_root"}].AsString(),
868+
"child_module": component.PlannedOutputValues[addrs.OutputValue{Name: "child_module"}].AsString(),
878869
}
879870

880871
if cmp.Diff(expected, actual) != "" {

0 commit comments

Comments
 (0)