mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 22:23:48 +03:00
caddyfile: Track import name instead of modifying filename (#5540)
* Merge branch 'master' into import_file_stack * remove space in log key
This commit is contained in:
parent
942fbb37ec
commit
9cde715525
5 changed files with 15 additions and 31 deletions
|
@ -20,6 +20,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Dispenser is a type that dispenses tokens, similarly to a lexer,
|
// Dispenser is a type that dispenses tokens, similarly to a lexer,
|
||||||
|
@ -398,7 +399,7 @@ func (d *Dispenser) ArgErr() error {
|
||||||
// SyntaxErr creates a generic syntax error which explains what was
|
// SyntaxErr creates a generic syntax error which explains what was
|
||||||
// found and what was expected.
|
// found and what was expected.
|
||||||
func (d *Dispenser) SyntaxErr(expected string) error {
|
func (d *Dispenser) SyntaxErr(expected string) error {
|
||||||
msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s'", d.File(), d.Line(), d.Val(), expected)
|
msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s', import chain: ['%s']", d.File(), d.Line(), d.Val(), expected, strings.Join(d.Token().imports, "','"))
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,7 +421,7 @@ func (d *Dispenser) Errf(format string, args ...any) error {
|
||||||
|
|
||||||
// WrapErr takes an existing error and adds the Caddyfile file and line number.
|
// WrapErr takes an existing error and adds the Caddyfile file and line number.
|
||||||
func (d *Dispenser) WrapErr(err error) error {
|
func (d *Dispenser) WrapErr(err error) error {
|
||||||
return fmt.Errorf("%s:%d - Error during parsing: %w", d.File(), d.Line(), err)
|
return fmt.Errorf("%s:%d - Error during parsing: %w, import chain: ['%s']", d.File(), d.Line(), err, strings.Join(d.Token().imports, "','"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes the current token and returns the updated slice
|
// Delete deletes the current token and returns the updated slice
|
||||||
|
|
|
@ -39,7 +39,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
|
||||||
if argRange == "" {
|
if argRange == "" {
|
||||||
caddy.Log().Named("caddyfile").Warn(
|
caddy.Log().Named("caddyfile").Warn(
|
||||||
"Placeholder "+token.Text+" cannot have an empty index",
|
"Placeholder "+token.Text+" cannot have an empty index",
|
||||||
zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
|
zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
|
||||||
return false, 0, 0
|
return false, 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
caddy.Log().Named("caddyfile").Warn(
|
caddy.Log().Named("caddyfile").Warn(
|
||||||
"Variadic placeholder "+token.Text+" has an invalid start index",
|
"Variadic placeholder "+token.Text+" has an invalid start index",
|
||||||
zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
|
zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
|
||||||
return false, 0, 0
|
return false, 0, 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
caddy.Log().Named("caddyfile").Warn(
|
caddy.Log().Named("caddyfile").Warn(
|
||||||
"Variadic placeholder "+token.Text+" has an invalid end index",
|
"Variadic placeholder "+token.Text+" has an invalid end index",
|
||||||
zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
|
zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
|
||||||
return false, 0, 0
|
return false, 0, 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
|
||||||
if startIndex < 0 || startIndex > endIndex || endIndex > argCount {
|
if startIndex < 0 || startIndex > endIndex || endIndex > argCount {
|
||||||
caddy.Log().Named("caddyfile").Warn(
|
caddy.Log().Named("caddyfile").Warn(
|
||||||
"Variadic placeholder "+token.Text+" indices are out of bounds, only "+strconv.Itoa(argCount)+" argument(s) exist",
|
"Variadic placeholder "+token.Text+" indices are out of bounds, only "+strconv.Itoa(argCount)+" argument(s) exist",
|
||||||
zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
|
zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
|
||||||
return false, 0, 0
|
return false, 0, 0
|
||||||
}
|
}
|
||||||
return true, startIndex, endIndex
|
return true, startIndex, endIndex
|
||||||
|
|
|
@ -39,7 +39,7 @@ type (
|
||||||
// Token represents a single parsable unit.
|
// Token represents a single parsable unit.
|
||||||
Token struct {
|
Token struct {
|
||||||
File string
|
File string
|
||||||
origFile string
|
imports []string
|
||||||
Line int
|
Line int
|
||||||
Text string
|
Text string
|
||||||
wasQuoted rune // enclosing quote character, if any
|
wasQuoted rune // enclosing quote character, if any
|
||||||
|
@ -321,23 +321,6 @@ func (l *lexer) finalizeHeredoc(val []rune, marker string) ([]rune, error) {
|
||||||
return []rune(out), nil
|
return []rune(out), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// originalFile gets original filename before import modification.
|
|
||||||
func (t Token) originalFile() string {
|
|
||||||
if t.origFile != "" {
|
|
||||||
return t.origFile
|
|
||||||
}
|
|
||||||
return t.File
|
|
||||||
}
|
|
||||||
|
|
||||||
// updateFile updates the token's source filename for error display
|
|
||||||
// and remembers the original filename. Used during "import" processing.
|
|
||||||
func (t *Token) updateFile(file string) {
|
|
||||||
if t.origFile == "" {
|
|
||||||
t.origFile = t.File
|
|
||||||
}
|
|
||||||
t.File = file
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t Token) Quoted() bool {
|
func (t Token) Quoted() bool {
|
||||||
return t.wasQuoted > 0
|
return t.wasQuoted > 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -429,7 +429,7 @@ func (p *parser) doImport(nesting int) error {
|
||||||
nodes = matches
|
nodes = matches
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeName := p.Token().originalFile()
|
nodeName := p.File()
|
||||||
if p.Token().snippetName != "" {
|
if p.Token().snippetName != "" {
|
||||||
nodeName += fmt.Sprintf(":%s", p.Token().snippetName)
|
nodeName += fmt.Sprintf(":%s", p.Token().snippetName)
|
||||||
}
|
}
|
||||||
|
@ -453,18 +453,18 @@ func (p *parser) doImport(nesting int) error {
|
||||||
// golang for range slice return a copy of value
|
// golang for range slice return a copy of value
|
||||||
// similarly, append also copy value
|
// similarly, append also copy value
|
||||||
for i, token := range importedTokens {
|
for i, token := range importedTokens {
|
||||||
// set the token's file to refer to import directive line number and snippet name
|
// update the token's imports to refer to import directive filename, line number and snippet name if there is one
|
||||||
if token.snippetName != "" {
|
if token.snippetName != "" {
|
||||||
token.updateFile(fmt.Sprintf("%s:%d (import %s)", token.File, p.Line(), token.snippetName))
|
token.imports = append(token.imports, fmt.Sprintf("%s:%d (import %s)", p.File(), p.Line(), token.snippetName))
|
||||||
} else {
|
} else {
|
||||||
token.updateFile(fmt.Sprintf("%s:%d (import)", token.File, p.Line()))
|
token.imports = append(token.imports, fmt.Sprintf("%s:%d (import)", p.File(), p.Line()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// naive way of determine snippets, as snippets definition can only follow name + block
|
// naive way of determine snippets, as snippets definition can only follow name + block
|
||||||
// format, won't check for nesting correctness or any other error, that's what parser does.
|
// format, won't check for nesting correctness or any other error, that's what parser does.
|
||||||
if !maybeSnippet && nesting == 0 {
|
if !maybeSnippet && nesting == 0 {
|
||||||
// first of the line
|
// first of the line
|
||||||
if i == 0 || importedTokens[i-1].originalFile() != token.originalFile() || importedTokens[i-1].Line+importedTokens[i-1].NumLineBreaks() < token.Line {
|
if i == 0 || importedTokens[i-1].File != token.File || importedTokens[i-1].Line+importedTokens[i-1].NumLineBreaks() < token.Line {
|
||||||
index = 0
|
index = 0
|
||||||
} else {
|
} else {
|
||||||
index++
|
index++
|
||||||
|
|
|
@ -229,7 +229,7 @@ func TestImportErrorLine(t *testing.T) {
|
||||||
import t1 true
|
import t1 true
|
||||||
}`,
|
}`,
|
||||||
errorFunc: func(err error) bool {
|
errorFunc: func(err error) bool {
|
||||||
return err != nil && strings.Contains(err.Error(), "Caddyfile:6 (import t1):2")
|
return err != nil && strings.Contains(err.Error(), "Caddyfile:6 (import t1)")
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -240,7 +240,7 @@ func TestImportErrorLine(t *testing.T) {
|
||||||
import t1 true
|
import t1 true
|
||||||
}`,
|
}`,
|
||||||
errorFunc: func(err error) bool {
|
errorFunc: func(err error) bool {
|
||||||
return err != nil && strings.Contains(err.Error(), "Caddyfile:5 (import t1):2")
|
return err != nil && strings.Contains(err.Error(), "Caddyfile:5 (import t1)")
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue