diff --git a/commands.go b/commands.go index 41c39066dd..ea1f320030 100644 --- a/commands.go +++ b/commands.go @@ -100,6 +100,8 @@ func initCommands( ProviderSource: providerSrc, ProviderDevOverrides: providerDevOverrides, UnmanagedProviders: unmanagedProviders, + + AllowExperimentalFeatures: ExperimentsAllowed(), } // The command list is included in the terraform -help diff --git a/experiments.go b/experiments.go new file mode 100644 index 0000000000..eb51dd2524 --- /dev/null +++ b/experiments.go @@ -0,0 +1,23 @@ +package main + +// experimentsAllowed can be set to any non-empty string using Go linker +// arguments in order to enable the use of experimental features for a +// particular Terraform build: +// go install -ldflags="-X 'main.experimentsAllowed=yes'" +// +// By default this variable is initialized as empty, in which case +// experimental features are not available. +// +// The Terraform release process should arrange for this variable to be +// set for alpha releases and development snapshots, but _not_ for +// betas, release candidates, or final releases. +// +// (NOTE: Some experimental features predate the rule that experiments +// are available only for alpha/dev builds, and so intentionally do not +// make use of this setting to avoid retracting a previously-documented +// open experiment.) +var experimentsAllowed string + +func ExperimentsAllowed() bool { + return experimentsAllowed != "" +} diff --git a/internal/command/meta.go b/internal/command/meta.go index 459427a0df..37bc7c2a8d 100644 --- a/internal/command/meta.go +++ b/internal/command/meta.go @@ -130,6 +130,24 @@ type Meta struct { // just trusting that someone else did it before running Terraform. UnmanagedProviders map[addrs.Provider]*plugin.ReattachConfig + // AllowExperimentalFeatures controls whether a command that embeds this + // Meta is permitted to make use of experimental Terraform features. + // + // Set this field only during the initial creation of Meta. If you change + // this field after calling methods of type Meta then the resulting + // behavior is undefined. + // + // In normal code this would be set by package main only in builds + // explicitly marked as being alpha releases or development snapshots, + // making experimental features unavailable otherwise. Test code may + // choose to set this if it needs to exercise experimental features. + // + // Some experiments predated the addition of this setting, and may + // therefore still be available even if this flag is false. Our intent + // is that all/most _future_ experiments will be unavailable unless this + // flag is set, to reinforce that experiments are not for production use. + AllowExperimentalFeatures bool + //---------------------------------------------------------- // Protected: commands can set these //---------------------------------------------------------- diff --git a/internal/command/meta_config.go b/internal/command/meta_config.go index d193892839..1df46d0493 100644 --- a/internal/command/meta_config.go +++ b/internal/command/meta_config.go @@ -334,6 +334,7 @@ func (m *Meta) initConfigLoader() (*configload.Loader, error) { if err != nil { return nil, err } + loader.AllowLanguageExperiments(m.AllowExperimentalFeatures) m.configLoader = loader if m.View != nil { m.View.SetConfigSources(loader.Sources) diff --git a/internal/configs/configload/loader.go b/internal/configs/configload/loader.go index d861a8d1e6..b9c3f4489c 100644 --- a/internal/configs/configload/loader.go +++ b/internal/configs/configload/loader.go @@ -148,3 +148,16 @@ func (l *Loader) ImportSourcesFromSnapshot(snap *Snapshot) { } } } + +// AllowLanguageExperiments specifies whether subsequent LoadConfig (and +// similar) calls will allow opting in to experimental language features. +// +// If this method is never called for a particular loader, the default behavior +// is to disallow language experiments. +// +// Main code should set this only for alpha or development builds. Test code +// is responsible for deciding for itself whether and how to call this +// method. +func (l *Loader) AllowLanguageExperiments(allowed bool) { + l.parser.AllowLanguageExperiments(allowed) +} diff --git a/internal/configs/experiments.go b/internal/configs/experiments.go index 8f330b6d9b..0bc87ad2b0 100644 --- a/internal/configs/experiments.go +++ b/internal/configs/experiments.go @@ -26,7 +26,7 @@ var disableExperimentWarnings = "" // the experiments are known before we process the result of the module config, // and thus we can take into account which experiments are active when deciding // how to decode. -func sniffActiveExperiments(body hcl.Body) (experiments.Set, hcl.Diagnostics) { +func sniffActiveExperiments(body hcl.Body, allowed bool) (experiments.Set, hcl.Diagnostics) { rootContent, _, diags := body.PartialContent(configFileTerraformBlockSniffRootSchema) ret := experiments.NewSet() @@ -84,9 +84,18 @@ func sniffActiveExperiments(body hcl.Body) (experiments.Set, hcl.Diagnostics) { } exps, expDiags := decodeExperimentsAttr(attr) - diags = append(diags, expDiags...) - if !expDiags.HasErrors() { - ret = experiments.SetUnion(ret, exps) + if allowed { + diags = append(diags, expDiags...) + if !expDiags.HasErrors() { + ret = experiments.SetUnion(ret, exps) + } + } else { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Module uses experimental features", + Detail: "Experimental features are intended only for gathering early feedback on new language designs, and so are available only in alpha releases of Terraform.", + Subject: attr.NameRange.Ptr(), + }) } } @@ -144,7 +153,7 @@ func decodeExperimentsAttr(attr *hcl.Attribute) (experiments.Set, hcl.Diagnostic diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: fmt.Sprintf("Experimental feature %q is active", exp.Keyword()), - Detail: "Experimental features are subject to breaking changes in future minor or patch releases, based on feedback.\n\nIf you have feedback on the design of this feature, please open a GitHub issue to discuss it.", + Detail: "Experimental features are available only in alpha releases of Terraform and are subject to breaking changes or total removal in later versions, based on feedback. We recommend against using experimental features in production.\n\nIf you have feedback on the design of this feature, please open a GitHub issue to discuss it.", Subject: expr.Range().Ptr(), }) } diff --git a/internal/configs/experiments_test.go b/internal/configs/experiments_test.go index 36e84f140e..7d1b9dc4e3 100644 --- a/internal/configs/experiments_test.go +++ b/internal/configs/experiments_test.go @@ -22,6 +22,7 @@ func TestExperimentsConfig(t *testing.T) { t.Run("current", func(t *testing.T) { parser := NewParser(nil) + parser.AllowLanguageExperiments(true) mod, diags := parser.LoadConfigDir("testdata/experiments/current") if got, want := len(diags), 1; got != want { t.Fatalf("wrong number of diagnostics %d; want %d", got, want) @@ -30,7 +31,7 @@ func TestExperimentsConfig(t *testing.T) { want := &hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: `Experimental feature "current" is active`, - Detail: "Experimental features are subject to breaking changes in future minor or patch releases, based on feedback.\n\nIf you have feedback on the design of this feature, please open a GitHub issue to discuss it.", + Detail: "Experimental features are available only in alpha releases of Terraform and are subject to breaking changes or total removal in later versions, based on feedback. We recommend against using experimental features in production.\n\nIf you have feedback on the design of this feature, please open a GitHub issue to discuss it.", Subject: &hcl.Range{ Filename: "testdata/experiments/current/current_experiment.tf", Start: hcl.Pos{Line: 2, Column: 18, Byte: 29}, @@ -49,6 +50,7 @@ func TestExperimentsConfig(t *testing.T) { }) t.Run("concluded", func(t *testing.T) { parser := NewParser(nil) + parser.AllowLanguageExperiments(true) _, diags := parser.LoadConfigDir("testdata/experiments/concluded") if got, want := len(diags), 1; got != want { t.Fatalf("wrong number of diagnostics %d; want %d", got, want) @@ -70,6 +72,7 @@ func TestExperimentsConfig(t *testing.T) { }) t.Run("concluded", func(t *testing.T) { parser := NewParser(nil) + parser.AllowLanguageExperiments(true) _, diags := parser.LoadConfigDir("testdata/experiments/unknown") if got, want := len(diags), 1; got != want { t.Fatalf("wrong number of diagnostics %d; want %d", got, want) @@ -91,6 +94,7 @@ func TestExperimentsConfig(t *testing.T) { }) t.Run("invalid", func(t *testing.T) { parser := NewParser(nil) + parser.AllowLanguageExperiments(true) _, diags := parser.LoadConfigDir("testdata/experiments/invalid") if got, want := len(diags), 1; got != want { t.Fatalf("wrong number of diagnostics %d; want %d", got, want) @@ -110,4 +114,26 @@ func TestExperimentsConfig(t *testing.T) { t.Errorf("wrong error\n%s", diff) } }) + t.Run("disallowed", func(t *testing.T) { + parser := NewParser(nil) + parser.AllowLanguageExperiments(false) // The default situation for release builds + _, diags := parser.LoadConfigDir("testdata/experiments/current") + if got, want := len(diags), 1; got != want { + t.Fatalf("wrong number of diagnostics %d; want %d", got, want) + } + got := diags[0] + want := &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: `Module uses experimental features`, + Detail: `Experimental features are intended only for gathering early feedback on new language designs, and so are available only in alpha releases of Terraform.`, + Subject: &hcl.Range{ + Filename: "testdata/experiments/current/current_experiment.tf", + Start: hcl.Pos{Line: 2, Column: 3, Byte: 14}, + End: hcl.Pos{Line: 2, Column: 14, Byte: 25}, + }, + } + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("wrong error\n%s", diff) + } + }) } diff --git a/internal/configs/parser.go b/internal/configs/parser.go index 2a621b5772..5a4b81078a 100644 --- a/internal/configs/parser.go +++ b/internal/configs/parser.go @@ -17,6 +17,13 @@ import ( type Parser struct { fs afero.Afero p *hclparse.Parser + + // allowExperiments controls whether we will allow modules to opt in to + // experimental language features. In main code this will be set only + // for alpha releases and some development builds. Test code must decide + // for itself whether to enable it so that tests can cover both the + // allowed and not-allowed situations. + allowExperiments bool } // NewParser creates and returns a new Parser that reads files from the given @@ -98,3 +105,16 @@ func (p *Parser) ForceFileSource(filename string, src []byte) { Bytes: src, }) } + +// AllowLanguageExperiments specifies whether subsequent LoadConfigFile (and +// similar) calls will allow opting in to experimental language features. +// +// If this method is never called for a particular parser, the default behavior +// is to disallow language experiments. +// +// Main code should set this only for alpha or development builds. Test code +// is responsible for deciding for itself whether and how to call this +// method. +func (p *Parser) AllowLanguageExperiments(allowed bool) { + p.allowExperiments = allowed +} diff --git a/internal/configs/parser_config.go b/internal/configs/parser_config.go index 0281339c6e..2e08580b5b 100644 --- a/internal/configs/parser_config.go +++ b/internal/configs/parser_config.go @@ -45,7 +45,7 @@ func (p *Parser) loadConfigFile(path string, override bool) (*File, hcl.Diagnost // We'll load the experiments first because other decoding logic in the // loop below might depend on these experiments. var expDiags hcl.Diagnostics - file.ActiveExperiments, expDiags = sniffActiveExperiments(body) + file.ActiveExperiments, expDiags = sniffActiveExperiments(body, p.allowExperiments) diags = append(diags, expDiags...) content, contentDiags := body.Content(configFileSchema) diff --git a/internal/configs/testdata/valid-files/object-optional-attrs-experiment.tf b/internal/configs/testdata/valid-files/object-optional-attrs-experiment.tf deleted file mode 100644 index f0ff5cd07f..0000000000 --- a/internal/configs/testdata/valid-files/object-optional-attrs-experiment.tf +++ /dev/null @@ -1,30 +0,0 @@ -variable "a" { - type = object({ - foo = optional(string) - bar = optional(bool, true) - }) -} - -variable "b" { - type = list( - object({ - foo = optional(string) - }) - ) -} - -variable "c" { - type = set( - object({ - foo = optional(string) - }) - ) -} - -variable "d" { - type = map( - object({ - foo = optional(string) - }) - ) -} diff --git a/internal/terraform/terraform_test.go b/internal/terraform/terraform_test.go index a3d81b7b6b..4192459583 100644 --- a/internal/terraform/terraform_test.go +++ b/internal/terraform/terraform_test.go @@ -57,6 +57,9 @@ func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *config // change its interface at this late stage. loader, _ := configload.NewLoaderForTests(t) + // We need to be able to exercise experimental features in our integration tests. + loader.AllowLanguageExperiments(true) + // Test modules usually do not refer to remote sources, and for local // sources only this ultimately just records all of the module paths // in a JSON file so that we can load them below. @@ -111,6 +114,9 @@ func testModuleInline(t *testing.T, sources map[string]string) *configs.Config { loader, cleanup := configload.NewLoaderForTests(t) defer cleanup() + // We need to be able to exercise experimental features in our integration tests. + loader.AllowLanguageExperiments(true) + // Test modules usually do not refer to remote sources, and for local // sources only this ultimately just records all of the module paths // in a JSON file so that we can load them below. diff --git a/main.go b/main.go index 1ffd8a343a..c807bb5a40 100644 --- a/main.go +++ b/main.go @@ -84,6 +84,9 @@ func realMain() int { } log.Printf("[INFO] Go runtime version: %s", runtime.Version()) log.Printf("[INFO] CLI args: %#v", os.Args) + if ExperimentsAllowed() { + log.Printf("[INFO] This build of Terraform allows using experimental features") + } streams, err := terminal.Init() if err != nil {