Import <args> slicing not working

1. The problem I’m having:

I am trying to use snippet + import to simplify my Caddyfile. I use wildcard certificate, so based on what I have read, I need to use handle. I found in the forums an example.

I am testing it out with respond, and respond works as expected when I

(sub) {
        @sub-{args[0]} host {args[0]}.{$HOSTNAME}
        handle @sub-{args[0]} {
                respond "{args[0]} {args[1]} {args[:]}"
        }
}

Test import:

*.example.com {
    import sub heartbeat Caddy is up - subsnippet
}

returns heartbeat Caddy {args[:]}

2. Error messages and/or full log output:

According to the docs import (Caddyfile directive) — Caddy Documentation
{args[:]} should return all the arguments from version 2.7.0, but it does not work for me.
I have tried different slicings, but every time I use :, it does not parse. What am I doing wrong?

3. Caddy version:

/srv # caddy -v
v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

a. System environment:

Docker inside Debian 12 VM on Proxmox host. Geekom A5 computer.

b. Command:

Dockerfile

FROM caddy:2.7.6-builder AS builder

RUN xcaddy build \
    --with github.com/caddy-dns/duckdns
FROM caddy:2.7.6

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

docker-compose

services:
  caddy:
    # image: caddy:2
    # building own caddy to include duckdns
    build:
      context: ./build/caddy
      network: host
    restart: unless-stopped
    ports:
      - 80:80
      - 443:443
    environment:
      HOSTNAME: $DOMAIN
      TZ: $TZ
    volumes:
      - ./build/caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./secrets/caddy/data:/data
      - ./secrets/caddy/config:/config

c. Service/unit/compose file:

see above

d. My complete Caddy config:

I will add a working config file later if needed.

5. Links to relevant resources:

A current limitation is that variadic style args can only be used as a single token, and not within another token. That’s because it may expand to multiple tokens.

If you need to use space-separated string inside another, then wrap it with quotes on your import line then use {args[2]} or whatever instead.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.