Skip to content

Commit

Permalink
Use a provider's stable version when bundling schemas (#1860)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck authored Nov 15, 2024
1 parent 05b9a20 commit 6510bc9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20241111-131048.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 16 additions & 12 deletions internal/schemas/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -283,7 +273,6 @@ func schemaForProvider(ctx context.Context, client registry.Client, input Inputs
required_providers {
{{ .LocalName }} = {
source = "{{ .Source }}"
{{ with .Version }}version = "{{ . }}"{{ end }}
}
}
}
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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)
Expand Down

0 comments on commit 6510bc9

Please sign in to comment.