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 empty diags not getting associated with source. #27710

Merged
merged 1 commit into from
Feb 12, 2021
Merged

Conversation

paddycarver
Copy link
Contributor

Right now, there's a bug that if a diagnostic comes back from the
provider with an AttributePath set, but no steps in the AttributePath,
Terraform thinks it's an attribute-specific diagnostic and not a
whole-resource diagnostic, but then doesn't associate it with any
specific attribute, meaning the diagnostic doesn't get associated with
the config at all.

This PR changes things to check if there are any steps in the
AttributePath before deciding this isn't a whole-resource diagnostic,
and if there aren't, treats it as a whole-resource diagnostic, instead.

See hashicorp/terraform-plugin-sdk#561 for more details on how this
surfaces in the wild.

Right now, there's a bug that if a diagnostic comes back from the
provider with an AttributePath set, but no steps in the AttributePath,
Terraform _thinks_ it's an attribute-specific diagnostic and not a
whole-resource diagnostic, but then doesn't associate it with any
specific attribute, meaning the diagnostic doesn't get associated with
the config at all.

This PR changes things to check if there are any steps in the
AttributePath before deciding this isn't a whole-resource diagnostic,
and if there aren't, treats it as a whole-resource diagnostic, instead.

See hashicorp/terraform-plugin-sdk#561 for more details on how this
surfaces in the wild.
@paddycarver paddycarver added the bug label Feb 8, 2021
@codecov
Copy link

codecov bot commented Feb 8, 2021

Codecov Report

Merging #27710 (c840e9c) into master (9c16e57) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
plugin/convert/diagnostics.go 50.64% <100.00%> (ø)
terraform/node_resource_plan.go 98.05% <0.00%> (+1.94%) ⬆️
internal/providercache/dir.go 73.46% <0.00%> (+6.12%) ⬆️

@paddycarver
Copy link
Contributor Author

(I wasn't quite clear on how to test this, as nothing about the surfaced diagnostic makes it clear that this has happened, but if anyone has any suggestions, I'm willing to try them!)

paddycarver added a commit to hashicorp/terraform-plugin-sdk that referenced this pull request Feb 8, 2021
When returning Diagnostics, only populate the AttributePath if there are
actually steps. Terraform core has a bug (see hashicorp/terraform#27710)
that will result in any Diagnostic that has an AttributePath with no
steps not being associated with a specific part of the config.

Fixes #561.
@pselle pselle requested a review from a team February 9, 2021 16:13
Copy link
Contributor

@alisdair alisdair left a comment

Choose a reason for hiding this comment

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

This looks good to me!

Below is a test which covers this change. Can you check if it makes sense to you? This is my first time looking at this code and I may have missed something.

// Test that a diagnostic with a present but empty attribute results in a
// whole body diagnostic. We verify this by inspecting the resulting Subject
// from the diagnostic when considered in the context of a config body.
func TestProtoDiagnostics_emptyAttributePath(t *testing.T) {
	protoDiags := []*proto.Diagnostic{
		{
			Severity: proto.Diagnostic_ERROR,
			Summary:  "error 1",
			Detail:   "error 1 detail",
			Attribute: &proto.AttributePath{
				Steps: []*proto.AttributePath_Step{
					// this slice is intentionally left empty
				},
			},
		},
	}
	tfDiags := ProtoToDiagnostics(protoDiags)

	testConfig := `provider "test" {
  foo = "bar"
}`
	f, parseDiags := hclsyntax.ParseConfig([]byte(testConfig), "test.tf", hcl.Pos{Line: 1, Column: 1})
	if parseDiags.HasErrors() {
		t.Fatal(parseDiags)
	}
	diags := tfDiags.InConfigBody(f.Body)

	if len(tfDiags) != 1 {
		t.Fatalf("expected 1 diag, got %d", len(tfDiags))
	}
	got := diags[0].Source().Subject
	want := &tfdiags.SourceRange{
		Filename: "test.tf",
		Start:    tfdiags.SourcePos{Line: 1, Column: 1},
		End:      tfdiags.SourcePos{Line: 1, Column: 1},
	}

	if !cmp.Equal(got, want, typeComparer, valueComparer) {
		t.Fatal(cmp.Diff(got, want, typeComparer, valueComparer))
	}
}

paddycarver added a commit to hashicorp/terraform-plugin-sdk that referenced this pull request Feb 10, 2021
When returning Diagnostics, only populate the AttributePath if there are
actually steps. Terraform core has a bug (see hashicorp/terraform#27710)
that will result in any Diagnostic that has an AttributePath with no
steps not being associated with a specific part of the config.

Fixes #561.
@jbardin
Copy link
Member

jbardin commented Feb 12, 2021

Thanks @paddycarver!

@jbardin jbardin merged commit 6e72b04 into master Feb 12, 2021
@pselle pselle deleted the paddy_empty_diags branch February 12, 2021 21:16
paddycarver added a commit that referenced this pull request Feb 18, 2021
Add a test for a diagnostic with no steps to make sure it still gets
associated with the resource in the config. Follow up to #27710 using
@alisdair's suggested testing.
paddycarver added a commit that referenced this pull request Feb 18, 2021
Add a test for a diagnostic with no steps to make sure it still gets
associated with the resource in the config. Follow up to #27710 using
@alisdair's suggested testing.
@bflad
Copy link
Contributor

bflad commented Feb 24, 2021

Should this be backported to 0.14 as well?

Reference: #27283

@pselle
Copy link
Contributor

pselle commented Feb 25, 2021

Thank you for raising this question, @bflad (and linking to a closeable issue 😇 )... I'll put this as an item for the Core team to discuss since we're in a beta period for 0.15 now, and we have bit more process around backporting during these times 😄

pselle pushed a commit that referenced this pull request Mar 9, 2021
Add a test for a diagnostic with no steps to make sure it still gets
associated with the resource in the config. Follow up to #27710 using
@alisdair's suggested testing.
@ghost
Copy link

ghost commented Mar 15, 2021

I'm going to lock this issue 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 similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants