Skip to content

Commit fbd5723

Browse files
authored
Merge pull request #182 from hashicorp/cherry-pick-22783
core: Warn when creating and applying with -target (cherry-pick hashicorp/terraform#22783)
2 parents f8f8e51 + 78fe3d0 commit fbd5723

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

terraform/context.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,17 @@ func (c *Context) Apply() (*states.State, tfdiags.Diagnostics) {
455455
c.state.PruneResourceHusks()
456456
}
457457

458+
if len(c.targets) > 0 {
459+
diags = diags.Append(tfdiags.Sourceless(
460+
tfdiags.Warning,
461+
"Applied changes may be incomplete",
462+
`The plan was created with the -target option in effect, so some changes requested in the configuration may have been ignored and the output values may not be fully updated. Run the following command to verify that no other changes are pending:
463+
terraform plan
464+
465+
Note that the -target option is not suitable for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.`,
466+
))
467+
}
468+
458469
return c.state, diags
459470
}
460471

@@ -471,6 +482,16 @@ func (c *Context) Plan() (*plans.Plan, tfdiags.Diagnostics) {
471482

472483
var diags tfdiags.Diagnostics
473484

485+
if len(c.targets) > 0 {
486+
diags = diags.Append(tfdiags.Sourceless(
487+
tfdiags.Warning,
488+
"Resource targeting is in effect",
489+
`You are creating a plan with the -target option, which means that the result of this plan may not represent all of the changes requested by the current configuration.
490+
491+
The -target option is not for routine use, and is provided only for exceptional situations such as recovering from errors or mistakes, or when Terraform specifically suggests to use it as part of an error message.`,
492+
))
493+
}
494+
474495
varVals := make(map[string]plans.DynamicValue, len(c.variables))
475496
for k, iv := range c.variables {
476497
// We use cty.DynamicPseudoType here so that we'll save both the

terraform/context_apply_test.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6723,15 +6723,34 @@ func TestContext2Apply_destroyTargetWithModuleVariableAndCount(t *testing.T) {
67236723
},
67246724
})
67256725

6726-
_, err := ctx.Plan()
6727-
if err != nil {
6728-
t.Fatalf("plan err: %s", err)
6726+
_, diags := ctx.Plan()
6727+
if diags.HasErrors() {
6728+
t.Fatalf("plan err: %s", diags)
6729+
}
6730+
if len(diags) != 1 {
6731+
// Should have one warning that -target is in effect.
6732+
t.Fatalf("got %d diagnostics in plan; want 1", len(diags))
6733+
}
6734+
if got, want := diags[0].Severity(), tfdiags.Warning; got != want {
6735+
t.Errorf("wrong diagnostic severity %#v; want %#v", got, want)
6736+
}
6737+
if got, want := diags[0].Description().Summary, "Resource targeting is in effect"; got != want {
6738+
t.Errorf("wrong diagnostic summary %#v; want %#v", got, want)
67296739
}
67306740

67316741
// Destroy, targeting the module explicitly
6732-
state, err = ctx.Apply()
6733-
if err != nil {
6734-
t.Fatalf("destroy apply err: %s", err)
6742+
state, diags = ctx.Apply()
6743+
if diags.HasErrors() {
6744+
t.Fatalf("destroy apply err: %s", diags)
6745+
}
6746+
if len(diags) != 1 {
6747+
t.Fatalf("got %d diagnostics; want 1", len(diags))
6748+
}
6749+
if got, want := diags[0].Severity(), tfdiags.Warning; got != want {
6750+
t.Errorf("wrong diagnostic severity %#v; want %#v", got, want)
6751+
}
6752+
if got, want := diags[0].Description().Summary, "Applied changes may be incomplete"; got != want {
6753+
t.Errorf("wrong diagnostic summary %#v; want %#v", got, want)
67356754
}
67366755
}
67376756

terraform/context_plan_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4662,9 +4662,18 @@ func TestContext2Plan_outputContainsTargetedResource(t *testing.T) {
46624662
},
46634663
})
46644664

4665-
_, err := ctx.Plan()
4666-
if err != nil {
4667-
t.Fatalf("err: %s", err)
4665+
_, diags := ctx.Plan()
4666+
if diags.HasErrors() {
4667+
t.Fatalf("err: %s", diags)
4668+
}
4669+
if len(diags) != 1 {
4670+
t.Fatalf("got %d diagnostics; want 1", diags)
4671+
}
4672+
if got, want := diags[0].Severity(), tfdiags.Warning; got != want {
4673+
t.Errorf("wrong diagnostic severity %#v; want %#v", got, want)
4674+
}
4675+
if got, want := diags[0].Description().Summary, "Resource targeting is in effect"; got != want {
4676+
t.Errorf("wrong diagnostic summary %#v; want %#v", got, want)
46684677
}
46694678
}
46704679

0 commit comments

Comments
 (0)