diff --git a/caddyfile/parse.go b/caddyfile/parse.go index 44f8d9e1..d3f65f4a 100644 --- a/caddyfile/parse.go +++ b/caddyfile/parse.go @@ -218,8 +218,13 @@ func (p *parser) doImport() error { } var matches []string - relImportPattern := filepath.Join(filepath.Dir(absFile), importPattern) - matches, err = filepath.Glob(relImportPattern) + var globPattern string + if !filepath.IsAbs(importPattern) { + globPattern = filepath.Join(filepath.Dir(absFile), importPattern) + } else { + globPattern = importPattern + } + matches, err = filepath.Glob(globPattern) if err != nil { return p.Errf("Failed to use import pattern %s: %v", importPattern, err) @@ -239,6 +244,27 @@ func (p *parser) doImport() error { if err != nil { return err } + var importLine int + importDir := filepath.Dir(importFile) + for i, token := range newTokens { + if token.Text == "import" { + importLine = token.Line + continue + } + if token.Line == importLine { + var abs string + if !filepath.IsAbs(importFile) { + abs = filepath.Join(filepath.Dir(absFile), token.Text) + } else { + abs = filepath.Join(importDir, token.Text) + } + newTokens[i] = Token{ + Text: abs, + Line: token.Line, + File: token.File, + } + } + } importedTokens = append(importedTokens, newTokens...) }