Commit d6110f8e authored by Pedro Nasser's avatar Pedro Nasser Committed by GitHub

Merge pull request #932 from pedronasser/fix-import

fix: import should always be relative to current file directory
parents 9ea05919 d7698ecf
...@@ -218,8 +218,13 @@ func (p *parser) doImport() error { ...@@ -218,8 +218,13 @@ func (p *parser) doImport() error {
} }
var matches []string var matches []string
relImportPattern := filepath.Join(filepath.Dir(absFile), importPattern) var globPattern string
matches, err = filepath.Glob(relImportPattern) if !filepath.IsAbs(importPattern) {
globPattern = filepath.Join(filepath.Dir(absFile), importPattern)
} else {
globPattern = importPattern
}
matches, err = filepath.Glob(globPattern)
if err != nil { if err != nil {
return p.Errf("Failed to use import pattern %s: %v", importPattern, err) return p.Errf("Failed to use import pattern %s: %v", importPattern, err)
...@@ -239,6 +244,27 @@ func (p *parser) doImport() error { ...@@ -239,6 +244,27 @@ func (p *parser) doImport() error {
if err != nil { if err != nil {
return err 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...) importedTokens = append(importedTokens, newTokens...)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment