Skip to content

fix(env): quote legacy ENV values that contain spaces - #74

Merged
reteps merged 1 commit into
reteps:mainfrom
RyoKusnadi:fix/quote-legacy-env-values
Nov 7, 2026
Merged

reteps merged 1 commit into
reteps:mainfrom
RyoKusnadi:fix/quote-legacy-env-values

Conversation

@RyoKusnadi

Copy link
Copy Markdown
Contributor

Problem

Formatting a legacy ENV line whose value has spaces gives a Dockerfile that doesn't parse, so dockerfmt --write breaks the build.

How it happens: any ENV KEY some value line (legacy form, everything after the key is the value) with more than one word, e.g. JAVA_OPTS.

Steps to reproduce

FROM alpine
ENV JAVA_OPTS -Xms256m -Xmx512m

Run dockerfmt Dockerfile.

Expected: ENV JAVA_OPTS="-Xms256m -Xmx512m"
Actual: ENV JAVA_OPTS=-Xms256m -Xmx512m, which fails with "can't find = in "-Xmx512m"".

Cause

formatEnv in lib/format.go joined key and value with = and never quoted.

Fix

New formatLegacyEnv: no whitespace in the value gives KEY=value as before; an already single-quoted value is kept as is; otherwise the value is wrapped in double quotes. If a spaced value also has quotes or backslashes, I keep the legacy form, since re-quoting would change the value (and the legacy form is valid).

Scope

LABEL and ARG go through formatBasic, which keeps their text, so they aren't converted and need no change.

Testing

Added cases to TestPerDirective (spaces, tabs/repeated spaces, $VAR, already quoted, embedded quotes). Without the fix 4 of them fail, e.g. actual ENV JAVA_OPTS=-Xms256m -Xmx512m; with it go test ./lib/ . and go vet ./lib/ pass. Not run: the JS/wasm build.

Environment: fix/quote-legacy-env-values @ d775b50, go1.27.1, darwin/arm64

Fixes #69

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
@reteps
reteps merged commit ef5fa8f into reteps:main Nov 7, 2026
3 checks passed
@reteps

reteps commented Nov 7, 2026

Copy link
Copy Markdown
Owner

Thanks! Merged as part of #83 and released in v0.6.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy ENV key value with a multi-word value becomes an invalid key=value

2 participants