Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalization of configuration paths #19708

Merged
merged 2 commits into from
Dec 19, 2018
Merged

Conversation

apparentlymart
Copy link
Contributor

This PR includes two adjustments to how we normalize configuration paths.

The first is general and applies both to the paths we show in diagnostic messages and the paths we return from path.root and path.module: previously we were normalizing these to relative paths in some commands but not others, which is both cosmetically weird and potentially problematic since path.module might return something different depending on which Terraform command you are running. We will now normalize these to be relative paths in all cases.

The second applies only to the path. references in configuration, making them always use forward slashes even on Windows systems. This addresses #14986 by relying on the fact that Windows supports both slash types as long as they are used consistently within a path. This means that moving forward all constructed paths in Terraform configurations should use forward slashes, like "${path.module}/foo/bar", which will produce correct results on all supported platforms.

These two changes compliment each other by allowing paths to remain consistent across platforms. Making path.module and path.root appear relative avoids them ending up containing platform-specific prefixes like c:\ or /home that would cause churn when applying the same configuration on different hosts.

Since the slash normalization behavior applies only to Windows and we run our tests on Unix there's no direct unit testing of that behavior, but I added tests for the more general behavior of path.module and path.root nonetheless, to show that they still work as before on Unix systems.

Previously we were doing this rather inconsistently: some commands would
do it and others would not. By doing it here we ensure we always apply the
same normalization, regardless of which operation we're running.

This normalization is mostly for cosmetic purposes in error messages, but
it also ends up being used to populate path.module and path.root and so
it's important that we always produce consistent results here so that
we don't produce flappy changes as users work with different commands.

The fact that thus mutates a data structure as a side-effect is not ideal
but this is the best place to ensure it always gets applied without doing
any significant refactoring, since everything after this point happens in
the backend package where the normalizePath method is not available.
Previously we used the native slash type for the host platform, but that
leads to issues if the same configuration is applied on both Windows and
non-Windows systems.

Since Windows supports slashes and backslashes, we can safely return
always slashes here and require that users combine the result with
subsequent path parts using slashes, like:

    "${path.module}/foo/bar"

Previously the above would lead to an error on Windows if path.module
contained any backslashes.

This is not really possible to unit test directly right now since we
always run our tests on Unix systems and filepath.ToSlash is a no-op on
Unix. However, this does include some tests for the basic behavior to
verify that it's not regressed as a result of this change.

This will need to be reported in the changelog as a potential breaking
change, since anyone who was using Terraform _exclusively_ on Windows may
have been using expressions like "${path.module}foo\\bar" which they will
now need to update.

This fixes #14986.
@apparentlymart apparentlymart merged commit cf9499c into master Dec 19, 2018
@apparentlymart apparentlymart deleted the b-normalize-config-paths branch December 19, 2018 21:49
@ghost
Copy link

ghost commented Mar 30, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants