Skip to content

Commit 0865512

Browse files
committed
fix: Avoid losing diagnostics raised when we parse config in (c *InitCommand) Run, instead pass config and diags into downstream code.
1 parent 77cd52f commit 0865512

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

internal/command/init.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,21 @@ func (c *InitCommand) Run(args []string) int {
6262
view.Diagnostics(diags)
6363
return 1
6464
}
65-
rootMod, _ := c.loadSingleModuleWithTests(path, initArgs.TestsDirectory)
66-
// We purposefully ignore any diagnostics returned here. They will be encountered downstream,
67-
// when the 'run' logic below is executed. If we return early due to error diagnostics here we
68-
// will break the order that errors are expected to be raised in.
65+
rootMod, rootModDiags := c.loadSingleModuleWithTests(path, initArgs.TestsDirectory)
66+
// We purposefully don't exit early if there are error diagnostics returned here; there are errors related to the Terraform version
67+
// that have precedence and are detected downstream.
68+
// We pass the configuration and diagnostic values from here into downstream code, replacing where the files are parsed there.
69+
// This prevents the diagnostics being lost, as re-parsing the same config results in lost diagnostics.
6970

7071
// The else condition below invokes the original logic of the init command.
7172
// An experimental version of the init code will be used if:
7273
// > The user uses an experimental version of TF (alpha or built from source).
7374
// > The terraform block in the configuration lists the `pluggable_state_stores` experiment.
7475
if c.Meta.AllowExperimentalFeatures && rootMod.ActiveExperiments.Has(experiments.PluggableStateStores) {
7576
// TODO(SarahFrench/radeksimko): Remove forked init logic once feature is no longer experimental
76-
return c.runPssInit(initArgs, view)
77+
return c.runPssInit(initArgs, view, rootMod, rootModDiags)
7778
} else {
78-
return c.run(initArgs, view)
79+
return c.run(initArgs, view, rootMod, rootModDiags)
7980
}
8081
}
8182

@@ -1488,10 +1489,11 @@ Options:
14881489
-test-directory=path Set the Terraform test directory, defaults to "tests".
14891490
14901491
-create-default-workspace [EXPERIMENTAL]
1491-
This flag must be used alongside the -enable-pluggable-state-storage-
1492-
experiment flag with experiments enabled. This flag's value defaults
1493-
to true, which allows the default workspace to be created if it does
1494-
not exist. Use -create-default-workspace=false to disable this behavior.
1492+
This flag must be used alongside naming the pluggable_state_stores
1493+
experiment in your configuration and using an experimental build of
1494+
Terraform. This flag's value defaults to true, which allows the default
1495+
workspace to be created if it does not exist.
1496+
Use -create-default-workspace=false to disable this behavior.
14951497
14961498
`
14971499
return strings.TrimSpace(helpText)

internal/command/init_run.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"go.opentelemetry.io/otel/trace"
2424
)
2525

26-
func (c *InitCommand) run(initArgs *arguments.Init, view views.Init) int {
26+
func (c *InitCommand) run(initArgs *arguments.Init, view views.Init, rootModEarly *configs.Module, earlyConfDiags tfdiags.Diagnostics) int {
2727
var diags tfdiags.Diagnostics
2828

2929
c.forceInitCopy = initArgs.ForceInitCopy
@@ -130,8 +130,14 @@ func (c *InitCommand) run(initArgs *arguments.Init, view views.Init) int {
130130
return 0
131131
}
132132

133-
// Load just the root module to begin backend and module initialization
134-
rootModEarly, earlyConfDiags := c.loadSingleModuleWithTests(path, initArgs.TestsDirectory)
133+
// If the passed root module is nil, attempt to parse it.
134+
// At this point we load just the root module to begin backend and module initialization
135+
//
136+
// TODO(SarahFrench/radeksimko): Once PSS's experiment is over, remove use of arguments and
137+
// restore parsing of config to always happen here in this code instead of calling code.
138+
if rootModEarly == nil {
139+
rootModEarly, earlyConfDiags = c.loadSingleModuleWithTests(path, initArgs.TestsDirectory)
140+
}
135141

136142
// There may be parsing errors in config loading but these will be shown later _after_
137143
// checking for core version requirement errors. Not meeting the version requirement should

internal/command/init_run_experiment.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
// `runPssInit` is an altered version of the logic in `run` that contains changes
3232
// related to the PSS project. This is used by the (InitCommand.Run method only if Terraform has
3333
// experimental features enabled.
34-
func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init) int {
34+
func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init, rootModEarly *configs.Module, earlyConfDiags tfdiags.Diagnostics) int {
3535
var diags tfdiags.Diagnostics
3636

3737
c.forceInitCopy = initArgs.ForceInitCopy
@@ -138,8 +138,14 @@ func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init) int
138138
return 0
139139
}
140140

141-
// Load just the root module to begin backend and module initialization
142-
rootModEarly, earlyConfDiags := c.loadSingleModuleWithTests(path, initArgs.TestsDirectory)
141+
// If the passed root module is nil, attempt to parse it.
142+
// At this point we load just the root module to begin backend and module initialization
143+
//
144+
// TODO(SarahFrench/radeksimko): Once PSS's experiment is over, remove use of arguments and
145+
// restore parsing of config to always happen here in this code instead of calling code.
146+
if rootModEarly == nil {
147+
rootModEarly, earlyConfDiags = c.loadSingleModuleWithTests(path, initArgs.TestsDirectory)
148+
}
143149

144150
// There may be parsing errors in config loading but these will be shown later _after_
145151
// checking for core version requirement errors. Not meeting the version requirement should

0 commit comments

Comments
 (0)