From 1df01fdd2b95db534b2693c8e2f58bd80e5e917a Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 7 May 2024 11:09:52 -0700 Subject: [PATCH] backend/local: Allow experiments when loading config from snapshot If a saved plan is being applied using a Terraform CLI/Core build that allows use of language experiments then that should be allowed when the configuration is being loaded from the snapshot saved in a plan file, in the same way as it would've been handled when preparing to generate that plan file. The local backend doesn't get told directly whether it should allow experiments, and instead its operation comes with a pre-configured configuration loader that may have the experiment flag set internally. When we load from snapshot we can't use the operation's own config loader because that loads from the real filesystem, and so we'll copy over the experiments-allowed flag from the operation's config loader to achieve consistency while still loading the configuration from the snapshot in the plan file. Without this a plan successfully created with experiments would fail with confusing errors during apply, because the snapshot config loader would not accept the same experiments that the plan-time config loader did. --- internal/backend/local/backend_local.go | 1 + internal/configs/configload/loader.go | 7 +++++++ internal/configs/parser.go | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/internal/backend/local/backend_local.go b/internal/backend/local/backend_local.go index d34d76f769..7ca85f1c05 100644 --- a/internal/backend/local/backend_local.go +++ b/internal/backend/local/backend_local.go @@ -239,6 +239,7 @@ func (b *Local) localRunForPlanFile(op *backendrun.Operation, pf *planfile.Reade return nil, snap, diags } loader := configload.NewLoaderFromSnapshot(snap) + loader.AllowLanguageExperiments(op.ConfigLoader.AllowsLanguageExperiments()) config, configDiags := loader.LoadConfig(snap.Modules[""].Dir) diags = diags.Append(configDiags) if configDiags.HasErrors() { diff --git a/internal/configs/configload/loader.go b/internal/configs/configload/loader.go index 06d92db55e..6ad987b7d1 100644 --- a/internal/configs/configload/loader.go +++ b/internal/configs/configload/loader.go @@ -164,3 +164,10 @@ func (l *Loader) ImportSourcesFromSnapshot(snap *Snapshot) { func (l *Loader) AllowLanguageExperiments(allowed bool) { l.parser.AllowLanguageExperiments(allowed) } + +// AllowsLanguageExperiments returns the value most recently passed to +// [Loader.AllowLanguageExperiments], or false if that method has not been +// called on this object. +func (l *Loader) AllowsLanguageExperiments() bool { + return l.parser.AllowsLanguageExperiments() +} diff --git a/internal/configs/parser.go b/internal/configs/parser.go index 26b9001618..91e1325df8 100644 --- a/internal/configs/parser.go +++ b/internal/configs/parser.go @@ -121,3 +121,10 @@ func (p *Parser) ForceFileSource(filename string, src []byte) { func (p *Parser) AllowLanguageExperiments(allowed bool) { p.allowExperiments = allowed } + +// AllowsLanguageExperiments returns the value most recently passed to +// [Parser.AllowLanguageExperiments], or false if that method has not been +// called on this object. +func (p *Parser) AllowsLanguageExperiments() bool { + return p.allowExperiments +}