configs: Return diagnostics (almost) directly from ParseProviderSourceString

This function can already produce suitable diagnostic messages which we'd
like to preserve, but it cannot produce source location information, and
so we'll amend the diagnostics to include that on the way out while
retaining all of the other values in the diagnostics.
This commit is contained in:
Martin Atkins 2020-03-12 10:53:44 -07:00
parent a851566c56
commit 946eda3f3c
1 changed files with 9 additions and 10 deletions

View File

@ -183,18 +183,17 @@ func (m *Module) appendFile(file *File) hcl.Diagnostics {
if reqd.Source.SourceStr != "" {
var sourceDiags tfdiags.Diagnostics
fqn, sourceDiags = addrs.ParseProviderSourceString(reqd.Source.SourceStr)
if sourceDiags.HasErrors() {
for i := range sourceDiags {
if sourceDiags[i].Severity() == tfdiags.Error {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid provider source string",
Detail: sourceDiags[i].Description().Detail,
Subject: &reqd.Source.DeclRange,
})
}
hclDiags := sourceDiags.ToHCL()
// The diagnostics from ParseProviderSourceString don't contain
// source location information because it has no context to compute
// them from, and so we'll add those in quickly here before we
// return.
for _, diag := range hclDiags {
if diag.Subject == nil {
diag.Subject = reqd.Source.DeclRange.Ptr()
}
}
diags = append(diags, hclDiags...)
} else {
fqn = addrs.NewLegacyProvider(reqd.Name)
}