@marcel91200 / caddy Mirror · read-only / commits / 9cde7155
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 01:45 UTC

caddyfile: Track import name instead of modifying filename (#5540)

* Merge branch 'master' into import_file_stack * remove space in log key

WeidiDeng committed May 26, 2023 at 03:05 UTC 9cde71552576dcc724eff38dee42879c927b72ec
5 files changed +15 -31
caddyconfig/caddyfile/dispenser.go
+3 -2
@@ -20,6 +20,7 @@ import (
20 "io"
21 "log"
22 "strconv"
23 + "strings"
24 )
25
26 // Dispenser is a type that dispenses tokens, similarly to a lexer,
@@ -398,7 +399,7 @@ func (d *Dispenser) ArgErr() error {
399 // SyntaxErr creates a generic syntax error which explains what was
400 // found and what was expected.
401 func (d *Dispenser) SyntaxErr(expected string) error {
401 - msg := fmt.Sprintf("%s:%d - Syntax error: Unexpected token '%s', expecting '%s'", d.File(), d.Line(), d.Val(), expected)
402 + 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, "','"))
403 return errors.New(msg)
404 }
405
@@ -420,7 +421,7 @@ func (d *Dispenser) Errf(format string, args ...any) error {
421
422 // WrapErr takes an existing error and adds the Caddyfile file and line number.
423 func (d *Dispenser) WrapErr(err error) error {
423 - return fmt.Errorf("%s:%d - Error during parsing: %w", d.File(), d.Line(), err)
424 + return fmt.Errorf("%s:%d - Error during parsing: %w, import chain: ['%s']", d.File(), d.Line(), err, strings.Join(d.Token().imports, "','"))
425 }
426
427 // Delete deletes the current token and returns the updated slice
caddyconfig/caddyfile/importargs.go
+4 -4
@@ -39,7 +39,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
39 if argRange == "" {
40 caddy.Log().Named("caddyfile").Warn(
41 "Placeholder "+token.Text+" cannot have an empty index",
42 - zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
42 + zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
43 return false, 0, 0
44 }
45
@@ -61,7 +61,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
61 if err != nil {
62 caddy.Log().Named("caddyfile").Warn(
63 "Variadic placeholder "+token.Text+" has an invalid start index",
64 - zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
64 + zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
65 return false, 0, 0
66 }
67 }
@@ -70,7 +70,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
70 if err != nil {
71 caddy.Log().Named("caddyfile").Warn(
72 "Variadic placeholder "+token.Text+" has an invalid end index",
73 - zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
73 + zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
74 return false, 0, 0
75 }
76 }
@@ -79,7 +79,7 @@ func parseVariadic(token Token, argCount int) (bool, int, int) {
79 if startIndex < 0 || startIndex > endIndex || endIndex > argCount {
80 caddy.Log().Named("caddyfile").Warn(
81 "Variadic placeholder "+token.Text+" indices are out of bounds, only "+strconv.Itoa(argCount)+" argument(s) exist",
82 - zap.String("file", token.File+":"+strconv.Itoa(token.Line)))
82 + zap.String("file", token.File+":"+strconv.Itoa(token.Line)), zap.Strings("import_chain", token.imports))
83 return false, 0, 0
84 }
85 return true, startIndex, endIndex
caddyconfig/caddyfile/lexer.go
+1 -18
@@ -39,7 +39,7 @@ type (
39 // Token represents a single parsable unit.
40 Token struct {
41 File string
42 - origFile string
42 + imports []string
43 Line int
44 Text string
45 wasQuoted rune // enclosing quote character, if any
@@ -321,23 +321,6 @@ func (l *lexer) finalizeHeredoc(val []rune, marker string) ([]rune, error) {
321 return []rune(out), nil
322 }
323
324 -// originalFile gets original filename before import modification.
325 -func (t Token) originalFile() string {
326 - if t.origFile != "" {
327 - return t.origFile
328 - }
329 - return t.File
330 -}
331 -
332 -// updateFile updates the token's source filename for error display
333 -// and remembers the original filename. Used during "import" processing.
334 -func (t *Token) updateFile(file string) {
335 - if t.origFile == "" {
336 - t.origFile = t.File
337 - }
338 - t.File = file
339 -}
340 -
324 func (t Token) Quoted() bool {
325 return t.wasQuoted > 0
326 }
caddyconfig/caddyfile/parse.go
+5 -5
@@ -429,7 +429,7 @@ func (p *parser) doImport(nesting int) error {
429 nodes = matches
430 }
431
432 - nodeName := p.Token().originalFile()
432 + nodeName := p.File()
433 if p.Token().snippetName != "" {
434 nodeName += fmt.Sprintf(":%s", p.Token().snippetName)
435 }
@@ -453,18 +453,18 @@ func (p *parser) doImport(nesting int) error {
453 // golang for range slice return a copy of value
454 // similarly, append also copy value
455 for i, token := range importedTokens {
456 - // set the token's file to refer to import directive line number and snippet name
456 + // update the token's imports to refer to import directive filename, line number and snippet name if there is one
457 if token.snippetName != "" {
458 - token.updateFile(fmt.Sprintf("%s:%d (import %s)", token.File, p.Line(), token.snippetName))
458 + token.imports = append(token.imports, fmt.Sprintf("%s:%d (import %s)", p.File(), p.Line(), token.snippetName))
459 } else {
460 - token.updateFile(fmt.Sprintf("%s:%d (import)", token.File, p.Line()))
460 + token.imports = append(token.imports, fmt.Sprintf("%s:%d (import)", p.File(), p.Line()))
461 }
462
463 // naive way of determine snippets, as snippets definition can only follow name + block
464 // format, won't check for nesting correctness or any other error, that's what parser does.
465 if !maybeSnippet && nesting == 0 {
466 // first of the line
467 - if i == 0 || importedTokens[i-1].originalFile() != token.originalFile() || importedTokens[i-1].Line+importedTokens[i-1].NumLineBreaks() < token.Line {
467 + if i == 0 || importedTokens[i-1].File != token.File || importedTokens[i-1].Line+importedTokens[i-1].NumLineBreaks() < token.Line {
468 index = 0
469 } else {
470 index++
caddyconfig/httpcaddyfile/builtins_test.go
+2 -2
@@ -229,7 +229,7 @@ func TestImportErrorLine(t *testing.T) {
229 import t1 true
230 }`,
231 errorFunc: func(err error) bool {
232 - return err != nil && strings.Contains(err.Error(), "Caddyfile:6 (import t1):2")
232 + return err != nil && strings.Contains(err.Error(), "Caddyfile:6 (import t1)")
233 },
234 },
235 {
@@ -240,7 +240,7 @@ func TestImportErrorLine(t *testing.T) {
240 import t1 true
241 }`,
242 errorFunc: func(err error) bool {
243 - return err != nil && strings.Contains(err.Error(), "Caddyfile:5 (import t1):2")
243 + return err != nil && strings.Contains(err.Error(), "Caddyfile:5 (import t1)")
244 },
245 },
246 {