From f001cb8654d74d531eec7da1898dc707dbaff7b1 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 8 Nov 2018 16:16:00 -0800 Subject: [PATCH] command: Fix terraform init -from-module with relative paths Since our new approach here works by installing with a synthetic module configuration block, we need to treat relative paths as a special case for two reasons: - Relative paths in module addresses are relative to the file containing the call rather than the working directory, but -from-module uses the working directory (and the call is in a synthetic "file" anyway) - We need to force Terraform to pass the path through to go-getter rather than just treating it as a relative reference, since we really do want a copy of the directory in this case, even if it is local. To address both of these things, we'll detect a relative path and turn it into an absolute path before beginning installation. This is a bit hacky, but this is consistent with the general philosophy of the -from-module implementation where it does hacky things so that the rest of the installer code can be spared of dealing with its special cases. This is covered by a couple of existing tests that run init -from-module, including TestInit_fromModule_dstInSrc which now passes. --- configs/configload/loader_init_from_module.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/configs/configload/loader_init_from_module.go b/configs/configload/loader_init_from_module.go index 0e41ea22c3..c383e2f4a3 100644 --- a/configs/configload/loader_init_from_module.go +++ b/configs/configload/loader_init_from_module.go @@ -127,6 +127,23 @@ func (l *Loader) InitDirFromModule(rootDir, sourceAddr string, hooks InstallHook }, } + // -from-module allows relative paths but it's different than a normal + // module address where it'd be resolved relative to the module call + // (which is synthetic, here.) To address this, we'll just patch up any + // relative paths to be absolute paths before we run, ensuring we'll + // get the right result. This also, as an important side-effect, ensures + // that the result will be "downloaded" with go-getter (copied from the + // source location), rather than just recorded as a relative path. + { + maybePath := filepath.ToSlash(sourceAddr) + if maybePath == "." || strings.HasPrefix(maybePath, "./") || strings.HasPrefix(maybePath, "../") { + if wd, err := os.Getwd(); err == nil { + sourceAddr = filepath.Join(wd, sourceAddr) + log.Printf("[TRACE] -from-module relative path rewritten to absolute path %s", sourceAddr) + } + } + } + // Now we need to create an artificial root module that will seed our // installation process. fakeRootModule := &configs.Module{