Detect and report when plugin cache dir matches filesystem mirror directory
Over in #29696 we saw that it's an easy mistake to make to accidentally configure your global cache directory to be the same as one of the filesystem mirror directories Terraform is using, particularly if it's one of the implied ones that therefore isn't otherwise mentioned directly in the CLI configuration.
There is a warning about this in the the Provider Plugin Cache docs:
The plugin cache directory must not also be one of the configured or implied filesystem mirror directories, since the cache management logic conflicts with the filesystem mirror logic when operating on the same directory.
...but whereever it's feasible we prefer to produce warnings or errors directly in the Terraform CLI UI rather than only in the docs, because it's easy to miss warnings like the above on your initial read and then not think to look back at the docs if you run into a related problem later on.
With all of that said then, this issue is here to represent investigating to what extent we may be able to detect this situation and report on it. There are some caveats and notes we already identified in the other issue which I'll repeat here for easier reference, though there may be others we didn't think of yet:
The definition of two paths referring to "the same directory" necessarily involves doing some OS-specific work in order to be precise. For example, on any OS which supports symlinks there may be multiple lexically-different paths that end up referring to the same physical directory, but "the same physical directory" is below the level of abstraction used by most file manipulation APIs.
Fortunately, modern Go has
os.SameFilewhich seems to abstract that away pretty well, and also it shouldn't hurt if we get some false negatives here as long as we can get a correct answer in common, simple cases.I observed in the other issue that implied mirror directories might make this heuristic trickier because they might not actually exist yet when Terraform checks them.
However, with fresh eyes today as I write this I think this might not actually be a big deal in practice, because we require users manually create the
plugin_cache_dirdirectory before using it, and so an implied mirror directory incorrectly configured also as aplugin_cache_dirmust already have been created in order for Terraform to use it as a cache successfully.There's already some similar logic using
os.SameFileto detect if someone tries to configure theplugin_cache_dirto refer to the current working directory's local cache dir (the.terraform/providerssubdirectory, by default). We might be able to base this new check on what that's doing.Our v1.0 Compatibility Promises call for us to avoid making changes that might break existing automation. Although configuring the same directory as both a cache and a filesystem mirror is incorrect, it can unfortunately work without error as long as the user is starting with a fresh filesystem each time, and that's a common situation for automation where it may be running in a temporary filesystem built from a container image, or similar.
Given that, we might be constrained to make this just a warning during
terraform init, rather than a full error, so that we can still let folks know that they are doing something unsupported but let them address that problem as an independent step from upgrading to a newer version of Terraform CLI.
Source: hashicorp/terraform