Skip to content

cmd/snap: improve snap size reporting#16669

Open
bboozzoo wants to merge 3 commits intocanonical:masterfrom
bboozzoo:bboozzoo/fix-quantity-snap-info
Open

cmd/snap: improve snap size reporting#16669
bboozzoo wants to merge 3 commits intocanonical:masterfrom
bboozzoo:bboozzoo/fix-quantity-snap-info

Conversation

@bboozzoo
Copy link
Contributor

@bboozzoo bboozzoo commented Feb 25, 2026

The code used a helper which rounds down the quantities for formatting. The rounding error isn't super significant while in the MBs range, but above 1GB, there's an observable difference between 1.9GB and 1GB.

Switch to a different helper which tries to preserve the informational value of the formatted quantity.

Fixes: LP#2142655 SNAPDENG-36492

Thanks for helping us make a better snapd!
Have you signed the license agreement and read the contribution guide?

@bboozzoo bboozzoo added this to the 2.75 milestone Feb 25, 2026
@bboozzoo bboozzoo requested a review from Copilot February 25, 2026 11:20
@bboozzoo bboozzoo added the Simple 😃 A small PR which can be reviewed quickly label Feb 25, 2026
@github-actions github-actions bot added the Needs Documentation -auto- Label automatically added which indicates the change needs documentation label Feb 25, 2026
@bboozzoo bboozzoo force-pushed the bboozzoo/fix-quantity-snap-info branch from 29babaa to 198bab2 Compare February 25, 2026 11:23
The code used a helper which rounds down the quantities for formatting.
The rounding error isn't super significant while in the MBs range, but
above 1GB, there's an observable difference between 1.9GB and 1GB.

Switch to a different helper which tries to preserve the informational
value of the formatted quantity.

Fixes: LP#2142655

Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
@bboozzoo bboozzoo force-pushed the bboozzoo/fix-quantity-snap-info branch from 198bab2 to 69d020f Compare February 25, 2026 11:23
@github-actions
Copy link

github-actions bot commented Feb 25, 2026

Fri Feb 27 20:43:18 UTC 2026
The following results are from: https://github.com/canonical/snapd/actions/runs/22497888399

Failures:

Preparing:

  • openstack:ubuntu-core-20-64:
  • openstack:ubuntu-core-20-64:
  • openstack:ubuntu-core-20-64:
  • openstack:ubuntu-core-20-64:
  • openstack:ubuntu-24.04-64:tests/cross/go-build:s390x

Executing:

  • openstack:arch-linux-64:tests/main/snap-info
  • openstack:debian-12-64:tests/main/cgroup-devices-v2
  • openstack:debian-12-64:tests/main/snap-info
  • openstack:ubuntu-core-18-64:tests/core/gadget-update-pc
  • openstack:ubuntu-core-18-64:tests/main/snap-info
  • openstack:ubuntu-core-18-64:tests/main/component-from-store
  • openstack:ubuntu-core-20-64:tests/core/mem-cgroup-disabled
  • openstack:ubuntu-core-20-64:tests/main/snap-info
  • openstack:ubuntu-core-20-64:tests/main/component-from-store
  • openstack:ubuntu-core-24-64:tests/main/snap-user-service-socket-activation
  • openstack:ubuntu-core-24-64:tests/main/snap-info
  • openstack:ubuntu-core-24-64:tests/main/component-from-store
  • openstack:ubuntu-20.04-64:tests/main/snap-info
  • openstack:ubuntu-20.04-64:tests/main/catalog-update
  • openstack:ubuntu-20.04-64:tests/main/component-from-store
  • openstack:ubuntu-25.10-64:tests/main/component-from-store
  • openstack:ubuntu-25.10-64:tests/main/snap-info
  • openstack:ubuntu-24.04-64:tests/main/snap-info
  • openstack:ubuntu-24.04-64:tests/main/component-from-store

Restoring:

  • openstack:ubuntu-core-18-64:tests/core/gadget-update-pc
  • openstack:ubuntu-core-18-64:tests/core/
  • openstack:ubuntu-core-18-64:
  • openstack:ubuntu-core-20-64:tests/core/mem-cgroup-disabled
  • openstack:ubuntu-core-20-64:tests/core/
  • openstack:ubuntu-core-20-64:
  • openstack:ubuntu-25.10-64:tests/main/apparmor-prompting-prompt-restoration
  • openstack:ubuntu-25.10-64:tests/main/
  • openstack:ubuntu-25.10-64:
  • openstack:ubuntu-24.04-64:tests/cross/go-build:s390x
  • openstack:ubuntu-24.04-64:

Skipped tests from snapd-testing-skip

  • openstack:ubuntu-24.04-64:tests/main/i18n

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves snap size reporting in the snap info command by switching from strutil.SizeToStr (which rounds down) to quantity.FormatAmount (which preserves informational value with decimal precision). This addresses the issue where sizes above 1GB would lose significant precision, e.g., 1.9GB displaying as 1GB.

Changes:

  • Replaced strutil.SizeToStr with quantity.FormatAmount(uint64(size), -1) + "B" in channel info formatting
  • Added comprehensive test coverage for large sizes (GB and MB ranges) with new test TestInfoHumanSizes
  • Updated all existing test expectations to reflect the new, more precise formatting

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cmd/snap/cmd_info.go Changed size formatting from strutil.SizeToStr to quantity.FormatAmount for better precision
cmd/snap/cmd_info_test.go Updated test expectations for new size format and added new test for large sizes

revision: fmt.Sprintf("(%s)", revision),
size: strutil.SizeToStr(size),
notes: notes.String(),
// -1 for auto-width of 5 chars
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

For consistency, consider also updating the size formatting in cmd/snap/cmd_snap_op.go line 1123 (snap refresh --list command) and cmd/snap/cmd_component.go line 104 (snap component command) to use the same quantity.FormatAmount approach. This would ensure all snap size displays benefit from the improved precision, especially for sizes above 1GB.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Signed-off-by: Maciej Borzecki <maciej.borzecki@canonical.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

esc *escapes
}

func formatSize(sz int64) string {
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The conversion from int64 to uint64 in formatSize is potentially unsafe if negative size values occur. While the current codebase doesn't appear to produce negative sizes in practice, the type conversion would wrap negative values to very large positive values (e.g., -1 becomes 18446744073709551615).

Consider adding a guard to handle negative values explicitly, for example:

func formatSize(sz int64) string {
    if sz < 0 {
        return "n/a"
    }
    return quantity.FormatAmount(uint64(sz), -1) + "B"
}

This would make the function more defensive and prevent confusing output if negative sizes are ever introduced through bugs or API changes.

Suggested change
func formatSize(sz int64) string {
func formatSize(sz int64) string {
if sz < 0 {
return "n/a"
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@ZeyadYasser ZeyadYasser left a comment

Choose a reason for hiding this comment

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

LGTM, Thank you!

Would it make sense to remove strutil.SizeToStr in a follow up?

Copy link
Contributor

@Mohit-Chachada Mohit-Chachada left a comment

Choose a reason for hiding this comment

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

Thanks! Just a few minor inline comments.

Additionally, I saw that there is one more usage of strutil.SizeToStr remaining at https://github.com/canonical/snapd/blob/master/osutil/disk.go#L37. Perhaps, we want to change that too and delete strutil.SizeToStr altogether? Removing it would help to prevent any such future (mis-)uses of it.

esc *escapes
}

func formatSize(sz int64) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

this already exists (with a different name) at https://github.com/canonical/snapd/blob/master/cmd/snap/cmd_snapshot.go#L36-L38.
Could you reuse that?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see both are the same function but with different name

Copy link
Contributor

Choose a reason for hiding this comment

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

but not sure if makes sense to move to strutil/quantity.

revision: fmt.Sprintf("(%s)", revision),
size: strutil.SizeToStr(size),
notes: notes.String(),
// -1 for auto-width of 5 chars
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: the comment seems out of place as there is no literal -1 here. Maybe it was from before when the actual call to quantity.FormatAmount with -1 as argument was here.

The comment suits more inside the formatSize method but as mentioned above we could instead re-use the already existing method from cmd_snapshot.go. If helpful, we can add this comment there.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.53%. Comparing base (4017487) to head (67c1df1).
⚠️ Report is 33 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #16669      +/-   ##
==========================================
+ Coverage   77.52%   77.53%   +0.01%     
==========================================
  Files        1355     1359       +4     
  Lines      186844   187316     +472     
  Branches     2449     2446       -3     
==========================================
+ Hits       144851   145240     +389     
- Misses      33237    33299      +62     
- Partials     8756     8777      +21     
Flag Coverage Δ
unittests 77.53% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

Needs Documentation -auto- Label automatically added which indicates the change needs documentation Simple 😃 A small PR which can be reviewed quickly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants