// This could either mean that it's explicitly "version": 0 or that
// the version property is missing. We'll assume the latter here
// because state snapshot version 0 was an encoding/gob binary format
// rather than a JSON format and so it would be very weird for
// that to show up in a JSON file.
returnnil,fmt.Errorf("invalid syntax: no format version number")
}
ifversionSniff.Version!=3{
returnnil,fmt.Errorf("unsupported backend state version %d; you may need to use Terraform CLI v%s to work in this directory",versionSniff.Version,versionSniff.TFVersion)
}
// If we get here then we can be sure that this file at least _thinks_
// it's format version 3.
varstateFileBackendStateFile
err=json.Unmarshal(src,&stateFile)
iferr!=nil{
returnnil,fmt.Errorf("invalid syntax: %w",err)
}
ifstateFile.Backend==nil&&stateFile.Remote!=nil{
// It's very unlikely to get here, but one way it could happen is
// if this working directory was most recently used with Terraform v0.8
// or earlier, which didn't yet include the concept of backends.
// This error message assumes that's the case.
returnnil,fmt.Errorf("this working directory uses legacy remote state and so must first be upgraded using Terraform v0.9")
returnnil,fmt.Errorf("attempted to encode a malformed backend state file; it contains state for both a 'backend' and a 'state_store' block. This is a bug in Terraform and should be reported.")
casef.Backend==nil&&f.StateStore==nil:
// This is valid - if the user has a backend state file and an implied local backend in use
// the backend state file exists but has no Backend data.
casef.Backend!=nil:
// Not implementing anything here - risk of breaking changes
casef.StateStore!=nil:
err:=f.StateStore.Validate()
iferr!=nil{
returnnil,err
}
default:
panic("error when determining whether backend state file was valid. This is a bug in Terraform and should be reported.")