configs: record the source directory for modules
We can only do this when modules are loaded with Parser.LoadConfigDir, but in practice this is the common case anyway. This is important to support the path.module and path.root expressions in configuration.
This commit is contained in:
parent
fa2a76fa23
commit
70f1635416
|
@ -9,6 +9,18 @@ import (
|
|||
// Module is a container for a set of configuration constructs that are
|
||||
// evaluated within a common namespace.
|
||||
type Module struct {
|
||||
// SourceDir is the filesystem directory that the module was loaded from.
|
||||
//
|
||||
// This is populated automatically only for configurations loaded with
|
||||
// LoadConfigDir. If the parser is using a virtual filesystem then the
|
||||
// path here will be in terms of that virtual filesystem.
|
||||
|
||||
// Any other caller that constructs a module directly with NewModule may
|
||||
// assign a suitable value to this attribute before using it for other
|
||||
// purposes. It should be treated as immutable by all consumers of Module
|
||||
// values.
|
||||
SourceDir string
|
||||
|
||||
CoreVersionConstraints []VersionConstraint
|
||||
|
||||
Backend *Backend
|
||||
|
|
|
@ -41,6 +41,8 @@ func (p *Parser) LoadConfigDir(path string) (*Module, hcl.Diagnostics) {
|
|||
mod, modDiags := NewModule(primary, override)
|
||||
diags = append(diags, modDiags...)
|
||||
|
||||
mod.SourceDir = path
|
||||
|
||||
return mod, diags
|
||||
}
|
||||
|
||||
|
|
|
@ -30,13 +30,17 @@ func TestParserLoadConfigDirSuccess(t *testing.T) {
|
|||
parser := NewParser(nil)
|
||||
path := filepath.Join("test-fixtures/valid-modules", name)
|
||||
|
||||
_, diags := parser.LoadConfigDir(path)
|
||||
mod, diags := parser.LoadConfigDir(path)
|
||||
if len(diags) != 0 {
|
||||
t.Errorf("unexpected diagnostics")
|
||||
for _, diag := range diags {
|
||||
t.Logf("- %s", diag)
|
||||
}
|
||||
}
|
||||
|
||||
if mod.SourceDir != path {
|
||||
t.Errorf("wrong SourceDir value %q; want %s", mod.SourceDir, path)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue