Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ the Cargo rule leaves Git checkouts alone,
the Deno rules leave localStorage and REPL history alone,
and the Maven rule refuses a repository with locally installed artifacts.
On macOS, a prune rule for Xcode selects only build directories
whose workspace no longer exists.
whose workspace no longer exists,
and refuses one that holds a Git repository with edits or local commits.

See the [rule reference](docs/rules.md#global-caches) for every global action,
its path, and its checks.
Expand Down
12 changes: 11 additions & 1 deletion docs/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ Global rules require their command to be available, except for the Xcode rule.
| RubyGems | none | `rubygems-cache` | `delete_contents` of `cache` in the directory that `gem env gemdir` prints; Plonge does not run `gem cleanup`, which uninstalls old gem versions | clear; requires `clean --global --include-clear --apply` to run. Installed gems stay; only the copies of their `.gem` files are deleted. |
| Homebrew | none yet | `homebrew` | `brew cleanup --prune=all` | clear; requires `clean --global --include-clear --apply` to run. Homebrew can remove old installed versions as well as cached downloads; Plonge measures only `brew --cache`. |
| Swift Package Manager | `.build` | `swiftpm-macos`, `swiftpm-linux` | `swift package purge-cache`, with `--cache-path` and `--scratch-path` set to the measured cache directory | clear; requires `clean --global --include-clear --apply` to run. Each rule applies only on its system. `artifacts` and `prebuilts` remain. |
| Xcode | `DerivedData` | `xcode-derived-data` | Built-in deletion of stale directories under `~/Library/Developer/Xcode/DerivedData`; no external command | prune; macOS only, when that directory can be read. Eligible with `clean --global`; requires `--apply` to delete. |
| Xcode | `DerivedData` | `xcode-derived-data` | Built-in deletion of stale directories under `~/Library/Developer/Xcode/DerivedData`; no external command | prune; macOS only, when that directory can be read. Eligible with `clean --global`; requires `--apply` to delete. Refuses a stale directory that holds a Git repository, except unchanged SwiftPM clones in `SourcePackages`, or a directory that it cannot read. |

A global rule can also set `delete_contents` in place of a cleanup command,
for a cache whose tool has none.
Expand All @@ -482,6 +482,16 @@ The Xcode global rule selects only directories whose XML `info.plist`
names an absolute `WorkspacePath` that no longer exists.
It keeps directories with missing or unreadable workspace metadata
and workspaces on unmounted volumes under `/Volumes`.
It also refuses a stale directory that holds a Git repository at any depth,
or a directory that it cannot read,
except the SwiftPM clones in `SourcePackages` that pass the project checks
for `.build`, other than the checks against `Package.resolved`,
which is gone with the workspace.
Without the pins, Plonge cannot tell whether a checkout was at the commit
that the workspace resolved, or whether a bare clone mirrors the package's URL;
it can tell only that neither holds work that Git cannot restore,
so a checkout's `HEAD` must be a commit that its remote-tracking refs
or its bare clone's tags hold.
The project `DerivedData` rule is different: it uses the project checks,
without checking Xcode workspace metadata.

Expand Down
26 changes: 26 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,32 @@ whose XML `info.plist` names an absolute `WorkspacePath` that no longer exists.
It keeps a directory without a readable `WorkspacePath`,
and a directory whose workspace is on a volume that is not mounted.

A stale directory usually holds the package clones that Xcode's SwiftPM keeps
in `SourcePackages`, which Xcode makes again when the workspace resolves.
Plonge allows them under the
[SwiftPM conditions](#swift-package-manager-checkouts),
except the checks against `Package.resolved`,
because the workspace that held the pins is gone:
a bare clone directly in `SourcePackages/repositories`
can mirror any origin,
and a checkout directly in `SourcePackages/checkouts`
can be at any commit that its `refs/remotes/origin` refs
or the tags that its bare clone also holds already hold.
A commit at the checkout's `HEAD` that no such ref holds is refused,
whether `HEAD` is detached or on a local branch.
Plonge refuses a stale directory that holds any other Git repository,
a clone that fails the other conditions, or a directory that it cannot read.
It checks again right before it deletes each directory,
and the outcome's size and action then count only the directories
that pass that check.
The report lists each refused directory and its reason
after the rule's action, and in `refused_folders` in JSON output;
its size is not in the rule's size, and it is not counted in the action:

```console
Refused ~/Library/Developer/Xcode/DerivedData/App-abc: contains a Git repository at SourcePackages/checkouts/dep that has changed, untracked, or ignored files
```

You can add a rule, or replace a default with the same `id`,
in `rules.toml`:

Expand Down
6 changes: 5 additions & 1 deletion internal/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Entry struct {
// It is false for a clear rule, for a rule whose targets give Bytes,
// and for the Xcode rule, whose Bytes is the size of the folders that it deletes.
WholeCache bool `json:"whole_cache,omitempty"`
// RefusedFolders are the folders that the rule would remove
// but keeps because they are or hold a Git repository, with the reason.
// They are not in Bytes or Command. Only the Xcode rule sets them.
RefusedFolders []scan.Refusal `json:"refused_folders,omitempty"`

argv []string
guards []*guard
Expand Down Expand Up @@ -298,7 +302,7 @@ func (r Runner) List(ctx context.Context, rs []rules.Global) []Entry {
e := &entries[i]
e.WholeCache = e.Skipped == "" && e.Kind == rules.KindPrune && !e.targeted
}
if e, ok := r.xcodeEntry(); ok {
if e, ok := r.xcodeEntry(ctx); ok {
entries = append(entries, e)
}
return entries
Expand Down
50 changes: 43 additions & 7 deletions internal/global/xcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/fs"
"os"
"path/filepath"
"slices"
"strings"

"github.com/mattt/plonge/internal/clean"
Expand All @@ -18,10 +19,13 @@ import (
// Xcode has no prune command, so this one rule is built in.
// A folder in DerivedData is stale when its info.plist names a WorkspacePath
// that no longer exists. Plonge keeps every other folder.
// It also refuses a stale folder that holds a Git repository
// with work that Git cannot restore, as scan.GuardDerivedData describes,
// and reports it in the entry's RefusedFolders.

// xcodeEntry returns the rule for stale DerivedData folders,
// or false when it does not apply on this machine.
func (r Runner) xcodeEntry() (Entry, bool) {
func (r Runner) xcodeEntry(ctx context.Context) (Entry, bool) {
if r.GOOS != "darwin" {
return Entry{}, false
}
Expand All @@ -31,18 +35,30 @@ func (r Runner) xcodeEntry() (Entry, bool) {
return Entry{}, false
}
e := Entry{ID: rules.XcodeRuleID, Tool: "xcode", Kind: rules.KindPrune, Path: dir, stale: []string{}}
var stale []string
for _, ent := range ents {
if folder := filepath.Join(dir, ent.Name()); ent.IsDir() && staleFolder(folder) {
e.stale = append(e.stale, folder)
stale = append(stale, folder)
}
}
for _, n := range scan.Sizes(e.stale) {
e.Bytes += n
sizes, reasons := scan.GuardDerivedData(ctx, stale, true)
for i, folder := range stale {
if reasons[i] != "" {
e.refuseFolder(folder, reasons[i])
continue
}
e.stale = append(e.stale, folder)
e.Bytes += sizes[i]
}
e.Command = fmt.Sprintf("delete %d stale DerivedData directories (built in)", len(e.stale))
e.Command = xcodeCommand(len(e.stale))
return e, true
}

// xcodeCommand is the Command of the Xcode rule when it deletes n folders.
func xcodeCommand(n int) string {
return fmt.Sprintf("delete %d stale DerivedData directories (built in)", n)
}

// staleFolder reports whether a DerivedData folder's workspace is gone.
// A folder without a readable WorkspacePath is not stale.
func staleFolder(folder string) bool {
Expand Down Expand Up @@ -90,20 +106,40 @@ func workspacePath(plist string) (string, bool) {
return "", false
}

// refuseFolder records a stale folder that plonge must not delete.
func (e *Entry) refuseFolder(folder, reason string) {
e.RefusedFolders = append(e.RefusedFolders, scan.Refusal{Path: folder, Rule: e.ID, Reason: reason})
}

// applyXcode deletes the stale folders, checking each one again first.
func applyXcode(_ context.Context, o *Outcome) {
// A folder that now holds a Git repository with work that Git cannot restore
// is refused and kept, and the other folders are still deleted.
// The outcome's size, action, and folders are rebuilt from the folders
// that pass this final check, and its size is what they held right before.
func applyXcode(ctx context.Context, o *Outcome) {
// The outcome shares its slices with the listed entry; do not write to them.
o.RefusedFolders = slices.Clone(o.RefusedFolders)
listed := o.stale
o.stale, o.Bytes = []string{}, 0
var failures []string
for _, folder := range o.stale {
for _, folder := range listed {
info, err := os.Lstat(folder)
if err != nil || !info.IsDir() || !staleFolder(folder) {
continue
}
if _, reasons := scan.GuardDerivedData(ctx, []string{folder}, false); reasons[0] != "" {
o.refuseFolder(folder, reasons[0])
continue
}
before := scan.DiskUsage(folder)
o.stale = append(o.stale, folder)
o.Bytes += before
if err := clean.RemoveTree(folder); err != nil {
failures = append(failures, err.Error())
}
o.FreedBytes += max(0, before-scan.DiskUsage(folder))
}
o.Command = xcodeCommand(len(o.stale))
if len(failures) > 0 {
o.Status, o.Reason = Failed, strings.Join(failures, "; ")
return
Expand Down
Loading
Loading