caddy
caddy copied to clipboard
Snippets can't be imported in named routes
Description
import is not recognized in named routes. With the following configuration:
# Caddyfile
(mysnippet) {
encode zstd gzip
}
&(myroute) {
import mysnippet
respond "Hello, Caddy!"
}
example.com {
invoke myroute
}
It results in:
$ caddy adapt -c Caddyfile
Error: unrecognized directive: import - are you sure your Caddyfile structure (nesting and braces) is correct?, at config/caddy/Caddyfile:8
Environment
- Caddy version: 2.8.4
- Platform: Linux (Fedora Silverblue 40)
I also reproduced it on the latest commit (https://github.com/caddyserver/caddy/commit/f8861ca16bd475e8519e7dbf5a2b55e81b329874).
Hi, I just ran into this limitation myself (Caddy v2.10.0). Is this considered a bug or is this by design? I might be able to send an update to the documentation in this case. To me, it sounds like this contradicts the following statement from the import docs:
it is evaluated before the structure is parsed, and it can appear anywhere in the Caddyfile.
I haven't had a chance to dig into this. Named routes were added as a relatively new feature, they work similarly to import at the Caddyfile parsing level so there's probably a bit of contention that makes them not play nice with eachother. PRs welcome in case someone wants to take a shot at fixing it.