From 6510bc91e883047fbadaf59abc77494b9487d21a Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 15 Nov 2024 14:27:15 +0100 Subject: [PATCH] Use a provider's stable version when bundling schemas (#1860) --- .../unreleased/BUG FIXES-20241111-131048.yaml | 6 ++++ internal/schemas/gen/gen.go | 28 +++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 .changes/unreleased/BUG FIXES-20241111-131048.yaml diff --git a/.changes/unreleased/BUG FIXES-20241111-131048.yaml b/.changes/unreleased/BUG FIXES-20241111-131048.yaml new file mode 100644 index 000000000..70f3bb8fc --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20241111-131048.yaml @@ -0,0 +1,6 @@ +kind: BUG FIXES +body: Use a provider's stable version when bundling schemas +time: 2024-11-11T13:10:48.339488+01:00 +custom: + Issue: "1860" + Repository: terraform-ls diff --git a/internal/schemas/gen/gen.go b/internal/schemas/gen/gen.go index 12ca07301..ff3941b35 100644 --- a/internal/schemas/gen/gen.go +++ b/internal/schemas/gen/gen.go @@ -262,16 +262,6 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs return nil, fmt.Errorf("unable to create workspace dir: %w", err) } - dataDir := filepath.Join(input.DataDirPath, - input.Provider.Addr.Hostname.String(), - input.Provider.Addr.Namespace, - input.Provider.Addr.Type, - pVersion.String()) - err = os.MkdirAll(dataDir, 0755) - if err != nil { - return nil, fmt.Errorf("unable to create data dir: %w", err) - } - type templateData struct { TerraformVersion string LocalName string @@ -283,7 +273,6 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs required_providers { {{ .LocalName }} = { source = "{{ .Source }}" - {{ with .Version }}version = "{{ . }}"{{ end }} } } } @@ -354,7 +343,12 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs return nil, fmt.Errorf("provider version not found for %q", input.Provider.Addr.ForDisplay()) } if !pv.Equal(pVersion) { - return nil, fmt.Errorf("expected provider version %s to match %s", pv, pVersion) + // The Terraform registry currently returns pre-release versions as the latest available version, + // although Terraform defaults to the latest stable version. In this case, we'll ignore the + // latest version from the registry until they implement a filter for stable versions. + log.Printf("%s: registry version doesn't match installed version: %s != %s. Using %s", + input.Provider.Addr.ForDisplay(), pVersion, pv, pv) + pVersion = pv } } @@ -364,6 +358,16 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs return nil, err } + dataDir := filepath.Join(input.DataDirPath, + input.Provider.Addr.Hostname.String(), + input.Provider.Addr.Namespace, + input.Provider.Addr.Type, + pVersion.String()) + err = os.MkdirAll(dataDir, 0755) + if err != nil { + return nil, fmt.Errorf("unable to create data dir: %w", err) + } + f, err := os.Create(filepath.Join(dataDir, "schema.json.gz")) if err != nil { return nil, fmt.Errorf("failed to create schema file: %w", err)