Skip to content

Commit c72fbe2

Browse files
authored
Fix module resolution when terraform init is invoked from another directory (#840)
Fixes #835
1 parent 80497f5 commit c72fbe2

File tree

12 files changed

+45
-12
lines changed

12 files changed

+45
-12
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "one" {
2+
value = "foo"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "one" {
2+
source = "../one"
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"one","Source":"../modules/one","Dir":"../modules/one"}]}

integration/init-cwd/root/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "one" {
2+
source = "../modules/one"
3+
}

integration/init-cwd/root/result.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"issues": [],
3+
"errors": []
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Modules":[{"Key":"","Source":"","Dir":"root"},{"Key":"one","Source":"../modules/one","Dir":"modules/one"}]}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "one" {
2+
value = "foo"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "one" {
2+
source = "../one"
3+
}

integration/init-parent/result.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"issues": [],
3+
"errors": []
4+
}

integration/init-parent/root/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "one" {
2+
source = "../modules/one"
3+
}

integration_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,23 @@ func TestIntegration(t *testing.T) {
7777
Command: "./tflint --format json --module",
7878
Dir: "path",
7979
},
80+
{
81+
Name: "init from parent",
82+
Command: "./tflint --format json --module root",
83+
Dir: "init-parent",
84+
},
85+
{
86+
Name: "init from cwd",
87+
Command: "./tflint --format json --module",
88+
Dir: "init-cwd/root",
89+
},
8090
}
8191

8292
dir, _ := os.Getwd()
83-
defer os.Chdir(dir)
84-
8593
for _, tc := range cases {
8694
testDir := dir + "/integration/" + tc.Dir
95+
96+
defer os.Chdir(dir)
8797
os.Chdir(testDir)
8898

8999
if tc.Env != nil {
@@ -95,14 +105,15 @@ func TestIntegration(t *testing.T) {
95105
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
96106
cli := cmd.NewCLI(outStream, errStream)
97107
args := strings.Split(tc.Command, " ")
108+
98109
cli.Run(args)
99110

100111
var b []byte
101112
var err error
102113
if runtime.GOOS == "windows" && IsWindowsResultExist() {
103-
b, err = ioutil.ReadFile("result_windows.json")
114+
b, err = ioutil.ReadFile(filepath.Join(testDir, "result_windows.json"))
104115
} else {
105-
b, err = ioutil.ReadFile("result.json")
116+
b, err = ioutil.ReadFile(filepath.Join(testDir, "result.json"))
106117
}
107118
if err != nil {
108119
t.Fatal(err)

tflint/loader.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"log"
88
"os"
9-
"path/filepath"
109
"sort"
1110
"strings"
1211

@@ -270,14 +269,9 @@ func (l *Loader) moduleWalker() configs.ModuleWalker {
270269
}
271270
}
272271

273-
dir := filepath.Join(l.currentDir, record.Dir)
274-
if filepath.IsAbs(record.Dir) {
275-
// If record.Dir is an absolute path, leave it
276-
dir = record.Dir
277-
}
278-
log.Printf("[DEBUG] Trying to load the module: key=%s, version=%s, dir=%s", key, record.VersionStr, dir)
272+
log.Printf("[DEBUG] Trying to load the module: key=%s, version=%s, dir=%s", key, record.VersionStr, record.Dir)
279273

280-
mod, diags := l.parser.LoadConfigDir(dir)
274+
mod, diags := l.parser.LoadConfigDir(record.Dir)
281275
return mod, record.Version, diags
282276
})
283277
}

0 commit comments

Comments
 (0)