Fileexists returns an inconsistent result

I have switched to the most recent terraform version (from 1.8.5 to 1.13.1).

After that update, a local execution of terraform results in the following error:

│ Error: Error in function call
│
│   on main.tf line 42, in locals:
│   42:     file_exists = fileexists("./jslogpush.zip")
│     ├────────────────
│     │ while calling fileexists(path)
│
│ Call to function "fileexists" failed: function returned an inconsistent result.

The error occurs only as changes are applied.
I do not understand how fileexists could have an inconsistent result. My expectation is that it returns either true or false, depending of whether the given file exists. The result is not part of the state, so what is it compared to that could make it inconsistent?

The code that is using it is a straightforward declaration of locals:

locals {
    ...
    file_exists = fileexists("./jslogpush.zip")
    ...
} 

Hi @wollo77,

A configuration is evaluated multiples times during normal execution, primarily once to evaluate the configuration when generating a plan, and then again to apply that plan. In order to successfully apply a plan, the entire configuration must again evaluate to values consistent to what they were when the plan was originally generated, otherwise you could get unpredictable results, errors, or crashes.

We switched from 1.12.2 to 1.13.1 and same issue appeared. We haven’t identified the culprit but the report it’s consistent with our experience. In our case the issue is with function file

Call to function "file" failed: function returned an inconsistent result.

This error could occur, for example, if the jslogpush.zip file did not exist during the plan phase but then did exist during the apply phase, because in that case the plan phase call would return false but then the apply phase call would return true.

Does some action in the configuration cause jslogpush.zip to be created at some point between the plan and apply phases?

Thanks for the answers, @jbardin and @apparentlymart!

Has this behaviour (the double checking of the file) been introduced recently? I can execute the same configuration on TF 12.2 without issue, the error only occurs on TF 13.1.

Yes, it’s listed under enhancements in the 1.13 release notes. There were many reports of crashes and other difficult to diagnose behaviors if external files caused the effective configuration to change between plan and apply. That consistency was always assumed before, but there had not previously been a way to enforce it.

Got it, thank you so much for the clarification!