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.
This commit is contained in:
Martin Atkins 2018-11-08 16:16:00 -08:00
parent 8b603e4877
commit f001cb8654
1 changed files with 17 additions and 0 deletions

View File

@ -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{