diff --git a/d2ir/compile.go b/d2ir/compile.go index fa68afbf9d..37d3445a97 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -27,7 +27,8 @@ type globContext struct { type compiler struct { err *d2parser.ParseError - fs fs.FS + fs fs.FS + imports []string // importStack is used to detect cyclic imports. importStack []string // importCache enables reuse of files imported multiple times. @@ -80,7 +81,7 @@ func Compile(ast *d2ast.Map, opts *CompileOptions) (*Map, []string, error) { if !c.err.Empty() { return nil, nil, c.err } - return m, c.importStack, nil + return m, c.imports, nil } func (c *compiler) overlayClasses(m *Map) { diff --git a/d2ir/import.go b/d2ir/import.go index ef56871277..c8ef907608 100644 --- a/d2ir/import.go +++ b/d2ir/import.go @@ -80,6 +80,13 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) { } defer c.popImportStack() + // Only get immediate imports. + if len(c.importStack) == 2 { + if _, ok := c.importCache[impPath]; !ok { + c.imports = append(c.imports, imp.PathWithPre()) + } + } + ir, ok := c.importCache[impPath] if ok { return ir, true