Commit e60400a9 authored by Matthew Holt's avatar Matthew Holt

caddyfile: Use full, absolute file path in token structs (fixes #1892)

When two Caddyfiles with the same name, but different paths, are
imported, it can cause a weird bug because isNewLine() returned false
when it should return true, since the files are actually different,
but it couldn't know that because only the base name was stored,
not the whole path.
parent e377eeff
...@@ -212,7 +212,7 @@ func (p *parser) doImport() error { ...@@ -212,7 +212,7 @@ func (p *parser) doImport() error {
// and then use glob to get list of matching filenames // and then use glob to get list of matching filenames
absFile, err := filepath.Abs(p.Dispenser.filename) absFile, err := filepath.Abs(p.Dispenser.filename)
if err != nil { if err != nil {
return p.Errf("Failed to get absolute path of file: %s", p.Dispenser.filename) return p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.filename, err)
} }
var matches []string var matches []string
...@@ -246,8 +246,8 @@ func (p *parser) doImport() error { ...@@ -246,8 +246,8 @@ func (p *parser) doImport() error {
if err != nil { if err != nil {
return err return err
} }
var importLine int var importLine int
importDir := filepath.Dir(importFile)
for i, token := range newTokens { for i, token := range newTokens {
if token.Text == "import" { if token.Text == "import" {
importLine = token.Line importLine = token.Line
...@@ -260,7 +260,7 @@ func (p *parser) doImport() error { ...@@ -260,7 +260,7 @@ func (p *parser) doImport() error {
} else if !filepath.IsAbs(importFile) { } else if !filepath.IsAbs(importFile) {
abs = filepath.Join(filepath.Dir(absFile), token.Text) abs = filepath.Join(filepath.Dir(absFile), token.Text)
} else { } else {
abs = filepath.Join(importDir, token.Text) abs = filepath.Join(filepath.Dir(importFile), token.Text)
} }
newTokens[i] = Token{ newTokens[i] = Token{
Text: abs, Text: abs,
...@@ -269,6 +269,7 @@ func (p *parser) doImport() error { ...@@ -269,6 +269,7 @@ func (p *parser) doImport() error {
} }
} }
} }
importedTokens = append(importedTokens, newTokens...) importedTokens = append(importedTokens, newTokens...)
} }
...@@ -300,8 +301,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) { ...@@ -300,8 +301,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err) return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
} }
// Tack the filename onto these tokens so errors show the imported file's name // Tack the file path onto these tokens so errors show the imported file's name
filename := filepath.Base(importFile) // (we use full, absolute path to avoid bugs: issue #1892)
filename, err := filepath.Abs(importFile)
if err != nil {
return nil, p.Errf("Failed to get absolute path of file: %s: %v", p.Dispenser.filename, err)
}
for i := 0; i < len(importedTokens); i++ { for i := 0; i < len(importedTokens); i++ {
importedTokens[i].File = filename importedTokens[i].File = filename
} }
......
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