From fe24feb4ada29f806f0eac5707fe3471b904aec3 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 30 Aug 2023 12:59:15 +0200 Subject: [PATCH 1/3] Only compare the address for missing schemas on preload --- internal/state/provider_schema.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/internal/state/provider_schema.go b/internal/state/provider_schema.go index 1a9c980e8..8cbca1424 100644 --- a/internal/state/provider_schema.go +++ b/internal/state/provider_schema.go @@ -209,10 +209,35 @@ func (s *ProviderSchemaStore) AllSchemasExist(pvm map[tfaddr.Provider]version.Co return true, nil } +// MissingSchemas checks which schemas are missing in order to preload them from the bundled schemas. +// Since we don't know the version of a schema on disk before loading it, we assume it's +// good to just load it by address and ignore the version constraints. func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Constraints) (map[tfaddr.Provider]version.Constraints, error) { missingSchemas := make(map[tfaddr.Provider]version.Constraints, 0) + fakeCons, _ := version.NewConstraint("> 0.0.0") + for pAddr, pCons := range pvm { - exists, err := s.schemaExists(pAddr, pCons) + if pAddr.IsLegacy() && pAddr.Type == "terraform" { + // The terraform provider is built into Terraform 0.11+ + // and while it's possible, users typically don't declare + // entry in required_providers block for it. + pAddr = tfaddr.NewProvider(tfaddr.BuiltInProviderHost, tfaddr.BuiltInProviderNamespace, "terraform") + } else if pAddr.IsLegacy() { + // Since we use recent version of Terraform to generate + // embedded schemas, these will never contain legacy + // addresses. + // + // A legacy namespace may come from missing + // required_providers entry & implied requirement + // from the provider block or 0.12-style entry, + // such as { grafana = "1.0" }. + // + // Implying "hashicorp" namespace here mimics behaviour + // of all recent (0.14+) Terraform versions. + pAddr.Namespace = "hashicorp" + } + + exists, err := s.schemaExists(pAddr, fakeCons) if err != nil { return nil, err } From 8d78d4ecd829d3eb3c368039c67537ff61abd453 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 30 Aug 2023 15:07:29 +0200 Subject: [PATCH 2/3] Improve MissingSchemas return value --- internal/state/provider_schema.go | 8 ++++---- internal/terraform/module/module_ops.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/state/provider_schema.go b/internal/state/provider_schema.go index 8cbca1424..a8418e721 100644 --- a/internal/state/provider_schema.go +++ b/internal/state/provider_schema.go @@ -212,11 +212,11 @@ func (s *ProviderSchemaStore) AllSchemasExist(pvm map[tfaddr.Provider]version.Co // MissingSchemas checks which schemas are missing in order to preload them from the bundled schemas. // Since we don't know the version of a schema on disk before loading it, we assume it's // good to just load it by address and ignore the version constraints. -func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Constraints) (map[tfaddr.Provider]version.Constraints, error) { - missingSchemas := make(map[tfaddr.Provider]version.Constraints, 0) +func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Constraints) ([]tfaddr.Provider, error) { + missingSchemas := make([]tfaddr.Provider, 0) fakeCons, _ := version.NewConstraint("> 0.0.0") - for pAddr, pCons := range pvm { + for pAddr := range pvm { if pAddr.IsLegacy() && pAddr.Type == "terraform" { // The terraform provider is built into Terraform 0.11+ // and while it's possible, users typically don't declare @@ -242,7 +242,7 @@ func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Con return nil, err } if !exists { - missingSchemas[pAddr] = pCons + missingSchemas = append(missingSchemas, pAddr) } } return missingSchemas, nil diff --git a/internal/terraform/module/module_ops.go b/internal/terraform/module/module_ops.go index 86fac3860..1f598d266 100644 --- a/internal/terraform/module/module_ops.go +++ b/internal/terraform/module/module_ops.go @@ -228,7 +228,7 @@ func PreloadEmbeddedSchema(ctx context.Context, logger *log.Logger, fs fs.ReadDi return nil } - for pAddr := range missingReqs { + for _, pAddr := range missingReqs { err := preloadSchemaForProviderAddr(ctx, pAddr, fs, schemaStore, logger) if err != nil { return err From 382f00974421004f8ef8dfd801821445050d664a Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 30 Aug 2023 16:11:51 +0200 Subject: [PATCH 3/3] Replace fake constraint --- internal/state/provider_schema.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/state/provider_schema.go b/internal/state/provider_schema.go index a8418e721..fce75f403 100644 --- a/internal/state/provider_schema.go +++ b/internal/state/provider_schema.go @@ -214,7 +214,6 @@ func (s *ProviderSchemaStore) AllSchemasExist(pvm map[tfaddr.Provider]version.Co // good to just load it by address and ignore the version constraints. func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Constraints) ([]tfaddr.Provider, error) { missingSchemas := make([]tfaddr.Provider, 0) - fakeCons, _ := version.NewConstraint("> 0.0.0") for pAddr := range pvm { if pAddr.IsLegacy() && pAddr.Type == "terraform" { @@ -237,7 +236,7 @@ func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Con pAddr.Namespace = "hashicorp" } - exists, err := s.schemaExists(pAddr, fakeCons) + exists, err := s.schemaExists(pAddr, version.Constraints{}) if err != nil { return nil, err }