Skip to content

Commit

Permalink
Make the values file path relative to the changed directory
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Dec 8, 2022
1 parent 384b150 commit 407bd21
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 26 deletions.
2 changes: 1 addition & 1 deletion integrationtest/inspection/chdir/.tflint.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ plugin "terraform" {
}

config {
varfile = ["dir/from_config.tfvars"]
varfile = ["from_config.tfvars"]
}
2 changes: 1 addition & 1 deletion integrationtest/inspection/inspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestIntegration(t *testing.T) {
},
{
Name: "chdir",
Command: fmt.Sprintf("tflint --chdir dir --module --var-file %s --format json", filepath.Join("dir", "from_cli.tfvars")),
Command: "tflint --chdir dir --module --var-file from_cli.tfvars --format json",
Dir: "chdir",
},
}
Expand Down
25 changes: 4 additions & 21 deletions terraform/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ var defaultVarsFilename = "terraform.tfvars"
// and returns terraform.InputValues in order of priority.
//
// The second and subsequent arguments are given the paths of value files to be read
// manually. Argument order matches precedence. These files do not respect the original
// working directory.
// manually. Argument order matches precedence.
func (l *Loader) LoadValuesFiles(dir string, files ...string) ([]InputValues, hcl.Diagnostics) {
values := []InputValues{}
diags := hcl.Diagnostics{}
Expand All @@ -145,14 +144,14 @@ func (l *Loader) LoadValuesFiles(dir string, files ...string) ([]InputValues, hc
}

for _, file := range autoLoadFiles {
vals, loadDiags := l.loadValuesFile(file, true)
vals, loadDiags := l.loadValuesFile(file)
diags = diags.Extend(loadDiags)
if !loadDiags.HasErrors() {
values = append(values, vals)
}
}
for _, file := range files {
vals, loadDiags := l.loadValuesFile(file, false)
vals, loadDiags := l.loadValuesFile(file)
diags = diags.Extend(loadDiags)
if !loadDiags.HasErrors() {
values = append(values, vals)
Expand All @@ -162,23 +161,7 @@ func (l *Loader) LoadValuesFiles(dir string, files ...string) ([]InputValues, hc
return values, diags
}

func (l *Loader) loadValuesFile(file string, auto bool) (InputValues, hcl.Diagnostics) {
if !auto {
// When reading the values file manually, it reads from the current directory,
// not the baseDir.
realPath, err := filepath.Rel(l.baseDir, file)
if err != nil {
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Failed to determine the real path of the values file",
Detail: err.Error(),
},
}
}
file = realPath
}

func (l *Loader) loadValuesFile(file string) (InputValues, hcl.Diagnostics) {
vals, diags := l.parser.LoadValuesFile(l.baseDir, file)
if diags.HasErrors() {
return nil, diags
Expand Down
6 changes: 3 additions & 3 deletions terraform/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ func TestLoadValuesFiles_withBaseDir(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// Files passed manually are relative to the baseDir, not the current directory.
// Files passed manually are relative to the current directory.
ret, diags := loader.LoadValuesFiles(
".",
filepath.Join("values_files", "cli1.tfvars"),
filepath.Join("values_files", "cli2.tfvars"),
"cli1.tfvars",
"cli2.tfvars",
)
if diags.HasErrors() {
t.Fatal(diags)
Expand Down
2 changes: 2 additions & 0 deletions terraform/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (p *Parser) loadHCLFile(baseDir, path string) (*hcl.File, hcl.Diagnostics)
{
Severity: hcl.DiagError,
Summary: "Failed to read file",
Subject: &hcl.Range{},
Detail: fmt.Sprintf("The file %q does not exist.", realPath),
},
}
Expand All @@ -200,6 +201,7 @@ func (p *Parser) loadHCLFile(baseDir, path string) (*hcl.File, hcl.Diagnostics)
{
Severity: hcl.DiagError,
Summary: "Failed to read file",
Subject: &hcl.Range{},
Detail: fmt.Sprintf("The file %q could not be read.", realPath),
},
}
Expand Down

0 comments on commit 407bd21

Please sign in to comment.