caddy/caddyconfig/caddyfile
prettysunflower 7dedd1486c
fix(caddyfile): {block} in snippet (#7558)
* fix(caddyfile): {block} in snippet

Resolve issue #7557

So, here is the situation:
- Pull request #7206 included some changes to the doImport's function of
  Caddyfile's parser. What it does is that if there is no token within a
  block that follows the import, and the import contains `{block}`, then
  the `{block}` token is discarded.
- After this pull request:
  - Issue #7518 noticed that in cases that `{block}` was not imported,
    a runtime error was raised due to the assumption that tokens were
    always added to `tokensCopy` on every iteration of `importedTokens`.
    This was fixed by pull request #7543.
  - Issue #7557 notices that {block} can be ignored when imported from a
    certain file. There, it's again an issue with how the import works.
    When `import snippets` is called, this import instruction doesn't
    contains any nested blocks. And when the argument replacer that is
    the `importedTokens` loop is called and finds `{block}`, it uses the
    block from the file's import (which in this case is nothing),
    `{block}` is erased, and unavailable when the import directive is
    called for the imported snippet.

The changed in this commit addresses the second issue by checking before
replacing `{block}` if we're currently in a snippet definition, and
appending the `{block}` token to `tokensCopy` if we are.

With this changes, when importing those snippets, the `{block}` token
will be available to be replaced by the nested blocks in `tokensToAdd`
if needed, or erased if there are no nested blocks and `tokensToAdd` is empty.

Tests added in pull requests #7206 and #7543 passes with this new
implementation, confirming that unused `{block}` are accepted if nothing
is passed to `import`, as well as the other usual tests.
A new test was also added based on issue #7557 reporting, and also passes.

Signed-off-by: prettysunflower <me@prettysunflower.moe>

* caddyfile: add imported snippet block placeholder coverage

---------

Signed-off-by: prettysunflower <me@prettysunflower.moe>
Co-authored-by: Zen Dodd <mail@steadytao.com>
2026-04-15 02:58:53 -04:00
..
testdata Revert "caddyfile: Reject long heredoc markers (#6098)" (#6100) 2024-02-12 18:06:22 +00:00
adapter.go chore: upgrade .golangci.yml and workflow to v2 (#6924) 2025-06-03 02:24:32 +03:00
dispenser.go chore: Enable modernize linter (#7519) 2026-02-26 14:01:35 -07:00
dispenser_test.go caddyfile: Fix importing nested tokens for {block} (#7189) 2025-08-22 21:29:34 +00:00
formatter.go caddyfile: Improve import/global options UX for imports before global options (#7642) 2026-04-10 17:17:55 -06:00
formatter_fuzz.go core: Windows service integration (#4790) 2022-07-29 14:06:54 -06:00
formatter_test.go caddyfile: Improve import/global options UX for imports before global options (#7642) 2026-04-10 17:17:55 -06:00
importargs.go caddyfile: Fix variadic placeholder false positive when token contains : (#5883) 2023-10-13 02:28:20 -04:00
importgraph.go chore: Use slices package where possible (#6585) 2024-09-25 14:30:56 -06:00
lexer.go httpcaddyfile: Validates TLS DNS challenge options (#7099) 2025-06-30 23:58:16 +00:00
lexer_fuzz.go core: Windows service integration (#4790) 2022-07-29 14:06:54 -06:00
lexer_test.go caddyfile: Correctly close the heredoc when the closing marker appears immediately (#6062) 2024-01-25 14:55:00 +00:00
parse.go fix(caddyfile): {block} in snippet (#7558) 2026-04-15 02:58:53 -04:00
parse_test.go fix(caddyfile): {block} in snippet (#7558) 2026-04-15 02:58:53 -04:00