Skip to content

fix(formatter): prevent image size from displaying scientific notation - #7551

Open
Anuj-er wants to merge 1 commit into
docker:masterfrom
Anuj-er:fix/image-size-format-3091
Open

Anuj-er wants to merge 1 commit into
docker:masterfrom
Anuj-er:fix/image-size-format-3091

Conversation

@Anuj-er

@Anuj-er Anuj-er commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #3091

Root cause

imageContext.Size() formats with units.HumanSizeWithPrecision(size, 3), which formats the value with %g. %g switches to scientific notation when the value rounds up to the next power of 1000, so 999,517,084 bytes (999.5 MB) renders as 1e+03MB instead of 1000MB. Upstream go-units still has the same behaviour, so the fix belongs here.

Fix

New local helper humanSize() in cli/command/formatter/image.go, used by imageContext.Size(). It mirrors HumanSizeWithPrecision(size, 3) exactly, except when %g would emit scientific notation it falls back to a plain integer rendering in the same unit (1000MB). Scope is the reported surface only (docker image ls); the other HumanSizeWithPrecision call sites (container sizes, build cache, history, tree) are untouched.

Relation to earlier attempts

I am aware of #5059, #7059 and #7064. #5059 and #7059 swap precision 3 for 4 (HumanSize), which only moves the bug to the next boundary (9999.5 MB would render as 1e+04MB) and changes displayed precision for all sizes. #7064 bumps the go-units vendor copy. This PR instead fixes the %g edge at the call site while keeping the existing 3-significant-digit output everywhere else.

Tests

Added cases to TestImageContext: 999517084 bytes -> 1000MB (the reported case), 999500000 bytes -> 1000MB (boundary), plus 1000000000 -> 1GB and 2746000000 -> 2.75GB to pin existing behaviour. Verified the new cases fail without the fix (1e+03MB) and pass with it. Full cli/command/formatter package passes; gofmt and go vet clean.

Fixes docker#3091

units.HumanSizeWithPrecision formats the value with %g, which renders
values that round up to the next power of 1000 (e.g. 999.5MB) as
"1e+03MB" instead of "1000MB". Format image sizes with a local
helper that falls back to a plain integer rendering in that case,
keeping the existing 3-significant-digit output everywhere else.
Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

Image size reported as 1e+03MB by docker image ls

2 participants