Skip to content

Add clear-caches directive for reducing Docker image sizes#5

Open
Copilot wants to merge 5 commits intomainfrom
copilot/add-clear-caches-directive
Open

Add clear-caches directive for reducing Docker image sizes#5
Copilot wants to merge 5 commits intomainfrom
copilot/add-clear-caches-directive

Conversation

Copy link
Contributor

Copilot AI commented Feb 15, 2026

Implementation Plan for clear-caches Feature

  • Add ClearCachesDirective type to aptfile package
  • Add parser function parseClearCachesDirective in aptfile/main.go
  • Update ParseLine switch statement to handle "clear-caches" directive
  • Add test case for clear-caches parsing in aptfile/main_test.go
  • Implement clearCaches function in main.go to run apt-get clean and remove cache directories
  • Add case in processAptfile to handle ClearCachesDirective
  • Defer clear-caches execution to run after packages are installed
  • Test the implementation with sample Aptfiles
  • Update documentation in README.md
  • Fix clear-caches to run only once even if multiple directives exist
  • Fix glob expansion in rm command by using sh -c
  • Run code security checks (CodeQL - no issues found)
  • All tests passing
  • Fix unrelated CI GoReleaser build failure by adding --snapshot --clean flags

Summary

Successfully implemented the clear-caches directive feature and fixed the unrelated CI build issue with GoReleaser that was failing because it expected a git tag on non-release builds.

Original prompt

This section details on the original issue you should resolve

<issue_title>Ability to clear caches at the end of installation</issue_title>
<issue_description>It would be helpful to be able to have some directive like

clear-caches

at the end of the Aptfile, so as to delete cache directories (commonly done in Dockerfiles to reduce image sizes).</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 15, 2026 04:20
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Copilot AI changed the title [WIP] Add ability to clear caches at the end of installation Add clear-caches directive for reducing Docker image sizes Feb 15, 2026
Copilot AI requested a review from ericsuh February 15, 2026 04:24
@ericsuh ericsuh marked this pull request as ready for review February 15, 2026 04:37
Copy link
Owner

@ericsuh ericsuh left a comment

Choose a reason for hiding this comment

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

@copilot please fix the test failure in CI, even though it is not related to this PR's changes.

Co-authored-by: ericsuh <382805+ericsuh@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 15, 2026

@copilot please fix the test failure in CI, even though it is not related to this PR's changes.

Fixed the GoReleaser CI failure by adding --snapshot --clean flags to the build command. This allows GoReleaser to build without requiring a git tag, which is appropriate for CI builds on PRs. (commit 13cf2e8)

Copy link

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

Adds a new clear-caches Aptfile directive to reduce final image/disk footprint by cleaning apt caches after all package installation has completed, aligning with common Docker optimization patterns.

Changes:

  • Add clear-caches directive parsing in aptfile (no args/options).
  • Defer cache-clearing execution in processAptfile until after installPackages.
  • Document the directive in the README and add a parser unit test.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
main.go Collects clear-caches directives during parsing and runs a new clearCaches() step after installs.
aptfile/main.go Introduces ClearCachesDirective and parsing logic for clear-caches.
aptfile/main_test.go Adds a ParseLine unit test case for the new directive.
README.md Documents clear-caches usage in the example Aptfile.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 86 to +87
pkgs := make([]aptfile.PackageDirective, 0)
clearCachesDirectives := make([]aptfile.ClearCachesDirective, 0)
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

clearCachesDirectives is only used as a presence check (len(...) > 0) and the directive struct is empty, so storing every occurrence is unnecessary. Consider replacing this slice with a simple boolean (e.g., clearCachesRequested) to better express intent and avoid redundant allocations/append calls.

Copilot uses AI. Check for mistakes.
Comment on lines +134 to +139
// Execute clear-caches once if any directives exist
if len(clearCachesDirectives) > 0 {
if err := clearCaches(dryRun); err != nil {
log.Fatalf("Failed to clear caches: %v", err)
}
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

New clear-caches behavior (deferral until after installPackages and idempotent execution) isn’t covered by tests. Since this file already has unit tests, add a test that runs processAptfile(..., dryRun=true) (or calls clearCaches(true)) while capturing stdout/stderr to assert the directive is executed exactly once and only after the install step.

Copilot uses AI. Check for mistakes.
Copilot AI requested a review from ericsuh February 15, 2026 04:44
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.

Ability to clear caches at the end of installation

3 participants