Skip to content

Commit bfa5d9c

Browse files
authored
fix: avoid ignoring hidden tfvars files (#968)
1 parent 000cb53 commit bfa5d9c

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

internal/terraform/ast/ast.go

-13
This file was deleted.

internal/terraform/ast/module.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,21 @@ func (mf ModFilename) IsJSON() bool {
1717
}
1818

1919
func IsModuleFilename(name string) bool {
20-
return (strings.HasSuffix(name, ".tf") ||
21-
strings.HasSuffix(name, ".tf.json")) &&
22-
!isIgnoredFile(name)
20+
if isIgnoredFile(name) {
21+
// See https://github.com/hashicorp/terraform/blob/d35bc05/internal/configs/parser_config_dir.go#L107
22+
return false
23+
}
24+
25+
return strings.HasSuffix(name, ".tf") ||
26+
strings.HasSuffix(name, ".tf.json")
27+
}
28+
29+
// isIgnoredFile returns true if the given filename (which must not have a
30+
// directory path ahead of it) should be ignored as e.g. an editor swap file.
31+
func isIgnoredFile(name string) bool {
32+
return strings.HasPrefix(name, ".") || // Unix-like hidden files
33+
strings.HasSuffix(name, "~") || // vim
34+
strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs
2335
}
2436

2537
type ModFiles map[ModFilename]*hcl.File

internal/terraform/ast/variables.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ func NewVarsFilename(name string) (VarsFilename, bool) {
1616
}
1717

1818
func IsVarsFilename(name string) bool {
19-
return (strings.HasSuffix(name, ".tfvars") ||
20-
strings.HasSuffix(name, ".tfvars.json")) &&
21-
!isIgnoredFile(name)
19+
// even files which are normally ignored/hidden,
20+
// such as .foo.tfvars (with leading .) are accepted here
21+
// see https://github.com/hashicorp/terraform/blob/77e6b62/internal/command/meta.go#L734-L738
22+
return strings.HasSuffix(name, ".tfvars") ||
23+
strings.HasSuffix(name, ".tfvars.json")
2224
}
2325

2426
func (vf VarsFilename) String() string {

0 commit comments

Comments
 (0)