compose
compose copied to clipboard
[BUG] False "variable is not set" warning when using the ${A:+} interpolation syntax
Description
When a variable interpolation is nested inside a ${A:+}-style interpolation construct for the same variable, compose emits a "variable is not set" warning, even though the nested evaluation isn't supposed to be evaluated.
Steps To Reproduce
-
Create
compose.ymlas follows:services: test: environment: A: ${A:+${A},}B image: busybox command: ['sh', '-c', 'echo "$$A"'] -
Run
docker compose up. It prints the following:WARN[0000] The "A" variable is not set. Defaulting to a blank string. [+] Running 1/1 ✔ Container docker-compose-repro-test-1 Recreated 0.1s Attaching to test-1 test-1 | B test-1 exited with code 0
The warning is a false positive: the nested ${A}, expression should only be evaluated if A is defined, which it isn't.
Compose Version
Docker Compose version v2.33.1
Docker Environment
Client: Docker Engine - Community
Version: 28.0.1
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.21.1
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.33.1
Path: /usr/libexec/docker/cli-plugins/docker-compose
scout: Docker Scout (Docker Inc.)
Version: v1.14.0
Path: /home/dpb/.docker/cli-plugins/docker-scout
Server:
Containers: 3
Running: 1
Paused: 0
Stopped: 2
Images: 242
Server Version: 28.0.1
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bcc810d6b9066471b0b6fa75f557a15a1cbf31bb
runc version: v1.2.4-0-g6c52b3f
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 5.15.133.1-microsoft-standard-WSL2
Operating System: Ubuntu 24.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 12
Total Memory: 11.68GiB
Name: [redacted]
ID: ded0336a-e1e9-4025-8553-4b4392a16670
Docker Root Dir: /var/lib/docker
Debug Mode: false
Experimental: false
Insecure Registries:
::1/128
127.0.0.0/8
Live Restore Enabled: false
WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support
Anything else?
For comparison, in bash an equivalent scenario works as expected:
$ bash -u -c 'echo ${A:+${A},}B'
B
The -u option prohibits interpolating undefined variables, but no error is produced.
bash variable support is way more elaborated that compose one 😅 also, AFAICT bash never warn you about empty variable, just defaults to blank, so the comparison is pretty limited here. What is the actual use-case that revealed this limitation ? Your example is easily replaceable with a default value in variable
This is the actual use case - I want the end result to be ${A},B when A is set, and B when it isn't. It's not solvable with just a default value, since the comma should not be present when A is undefined.
also, AFAICT bash never warn you about empty variable, just defaults to blank, so the comparison is pretty limited here.
Yeah, there's no warning, but there's an option to make it an error, so I thought it would be a fair comparison.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I have the same issue with B: ${A:-default}, it simply doesn't work. The default will never be chosen when using $B in the command line.
This issue has been automatically marked as not stale anymore due to the recent activity.
Same, B: ${A:-default} does nothing