Skip to content

Commit

Permalink
backend/local: Allow experiments when loading config from snapshot
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed May 7, 2024
1 parent cd5191b commit 06a3324
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/backend/local/backend_local.go
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions internal/configs/configload/loader.go
Expand Up @@ -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()
}
7 changes: 7 additions & 0 deletions internal/configs/parser.go
Expand Up @@ -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
}

0 comments on commit 06a3324

Please sign in to comment.