Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add missing provider schema versions #1060

Merged
merged 4 commits into from
Sep 2, 2022

Conversation

radeksimko
Copy link
Member

This is a follow-up to a recent bug fix - #1048

The linked PR addressed the fact that we may sometimes get provider schemas with nil versions. This PR additionally ensures that versions are in fact stored as they should be and clarifies some related edge cases in attached comments.

The temporary lack of versions for schemas is not as easily noticeable thanks to the loose/optimistic matching of schemas we perform, where even if we don't match the version, we always match based on the address, or even just provider name alone.

The only difference it makes is when there are two schemas for the same provider address of different versions - so that inaccuracy is being addressed.

schemas := make([]*ProviderSchema, 0)
for item := it.Next(); item != nil; item = it.Next() {
ps, ok := item.(*ProviderSchema)
if ok {
if ps.Schema == nil {
// Incomplete entry may be a result of provider version being
// sourced earlier where schema is yet to be sourced or sourcing failed.
continue
}
schemas = append(schemas, ps)
}
}
if len(schemas) == 0 && addr.Equals(NewDefaultProvider("terraform")) {
// assume that hashicorp/terraform is just the builtin provider
return s.ProviderSchema(modPath, NewBuiltInProvider("terraform"), vc)
}
if len(schemas) == 0 && addr.IsLegacy() {
if addr.Type == "terraform" {
return s.ProviderSchema(modPath, NewBuiltInProvider("terraform"), vc)
}
// Schema may be missing e.g. because Terraform 0.12
// required relevant provider block to be present
// to dump its schema in JSON output.
// First we try to find a provider
// by assuming the legacy provider is hashicorp's.
addr.Namespace = "hashicorp"
obj, err := txn.First(s.tableName, "id_prefix", addr)
if err != nil {
return nil, err
}
if obj != nil {
ps := obj.(*ProviderSchema)
if ps.Schema != nil {
return ps.Schema, nil
}
}
// Last we just try to loosely match the provider type
it, err := txn.Get(s.tableName, "id")
if err != nil {
return nil, err
}
for item := it.Next(); item != nil; item = it.Next() {
ps, ok := item.(*ProviderSchema)
if ok && ps.Schema != nil && ps.Address.Type == addr.Type {
schemas = append(schemas, ps)
}
}
}
if len(schemas) == 0 {
return nil, &NoSchemaError{}
}
ss := sortableSchemas{
schemas: schemas,
lookupModule: func(modPath string) (*Module, error) {
return moduleByPath(txn, modPath)
},
requiredModPath: modPath,
requiredVersion: vc,
}
sort.Stable(ss)
return ss.schemas[0].Schema, nil

Additionally - to make testing easier, I also added ranking of schemas based on version constraint match, further making schema matching more accurate.

@radeksimko radeksimko added the bug Something isn't working label Sep 1, 2022
@radeksimko radeksimko self-assigned this Sep 1, 2022
@radeksimko radeksimko requested a review from a team as a code owner September 1, 2022 12:31
@radeksimko radeksimko force-pushed the fix-missing-provider-versions branch from ccf138c to 5af48e8 Compare September 1, 2022 15:10
@radeksimko radeksimko marked this pull request as draft September 1, 2022 15:17
@radeksimko radeksimko force-pushed the fix-missing-provider-versions branch 7 times, most recently from e4c2ec3 to 37e172f Compare September 1, 2022 16:13
@radeksimko radeksimko force-pushed the fix-missing-provider-versions branch from 37e172f to 0263915 Compare September 1, 2022 16:20
@radeksimko radeksimko marked this pull request as ready for review September 1, 2022 16:23
Copy link
Member

@dbanck dbanck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for getting to the root cause

// sourced earlier where schema is yet to be sourced or sourcing failed.
continue
}
if ps.Version == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@radeksimko radeksimko merged commit fbf2839 into main Sep 2, 2022
@radeksimko radeksimko deleted the fix-missing-provider-versions branch September 2, 2022 11:19
@radeksimko radeksimko added this to the v0.29.2 milestone Sep 12, 2022
@github-actions
Copy link

This functionality has been released in v0.29.2 of the language server.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants