in internal/meta/base_meta.go [434:479]
func (meta baseMeta) PushState(ctx context.Context) error {
meta.tc.Trace(telemetry.Info, "PushState Enter")
defer meta.tc.Trace(telemetry.Info, "PushState Leave")
// Noop if tfclient is set
if meta.tfclient != nil {
return nil
}
// Don't push state if there is no state to push. This might happen when all the resources failed to import with "--continue".
if len(meta.baseState) == 0 {
return nil
}
// Ensure there is no out of band change on the base state
baseState, err := meta.tf.StatePull(ctx)
if err != nil {
return fmt.Errorf("failed to pull state: %v", err)
}
if baseState != string(meta.originBaseState) {
edits := myers.ComputeEdits(span.URIFromPath("origin.tfstate"), string(meta.originBaseState), baseState)
changes := fmt.Sprint(gotextdiff.ToUnified("origin.tfstate", "current.tfstate", string(meta.originBaseState), edits))
return fmt.Errorf("there is out-of-band changes on the state file:\n%s", changes)
}
// Create a temporary state file to hold the merged states, then push the state to the output directory.
f, err := os.CreateTemp("", "")
if err != nil {
return fmt.Errorf("creating a temporary state file: %v", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("closing the temporary state file %s: %v", f.Name(), err)
}
// #nosec G306
if err := os.WriteFile(f.Name(), meta.baseState, 0644); err != nil {
return fmt.Errorf("writing to the temporary state file: %v", err)
}
defer os.Remove(f.Name())
if err := meta.tf.StatePush(ctx, f.Name(), tfexec.Lock(true)); err != nil {
return fmt.Errorf("failed to push state: %v", err)
}
return nil
}