Skip to content

Conversation

@sigurdm
Copy link
Contributor

@sigurdm sigurdm commented Sep 30, 2025

To recover space occupied by unused old dependencies in the pub cache, without affecting "active" projects.

Will enumerate all package configs from all active projects to find all the packages located within the pub cache.

Then enumerates all packages/git repos in the pub cache, and removes those that are not referred to by the active projects.

Active projects have been marked since dart 3.9.0.

@sigurdm sigurdm requested a review from szakarias October 6, 2025 09:06
Copy link
Contributor

@szakarias szakarias left a comment

Choose a reason for hiding this comment

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

LGTM

parts[parts.length - 1] == 'package_config.json') {
projectDir = p.joinAll(parts.sublist(0, parts.length - 2));
}
log.message('* $projectDir');
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe restrict the number of outputs here?

@sigurdm sigurdm requested a review from jonasfj November 4, 2025 11:01
hideNegatedUsage: true,
);
argParser.addFlag(
'ignore-timestamp',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'ignore-timestamp',
'ignore-age',

or maybe:

Suggested change
'ignore-timestamp',
'no-ignore-recent',

or

Suggested change
'ignore-timestamp',
'no-skip-recent',

or

Suggested change
'ignore-timestamp',
'no-keep-recent',

or (not that I like it)

Suggested change
'ignore-timestamp',
'collect-recent',

Comment on lines +43 to +73
final activeRoots = cache.activeRoots();
final validActiveRoots = <String>[];
final paths = <String>{};
for (final packageConfigPath in activeRoots) {
late final PackageConfig packageConfig;
try {
packageConfig = PackageConfig.fromJson(
json.decode(readTextFile(packageConfigPath)),
);
} on IOException catch (e) {
// Failed to read file - probably got deleted.
log.fine('Failed to read packageConfig $packageConfigPath: $e');
continue;
} on FormatException catch (e) {
log.warning(
'Failed to decode packageConfig $packageConfigPath: $e.\n'
'It could be corrupted',
);
// Failed to decode - probably corrupted.
continue;
}
for (final package in packageConfig.packages) {
final rootUri = p.canonicalize(
package.resolvedRootDir(packageConfigPath),
);
if (p.isWithin(cache.rootDir, rootUri)) {
paths.add(rootUri);
}
}
validActiveRoots.add(packageConfigPath);
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe, give the reader a two line comment, explaining what you are collecting into validActiveRoots and path.

Or maybe split it into a function.


final dontRemoveFilesOlderThan = const Duration(hours: 2);

CacheGcCommand() {
Copy link
Member

Choose a reason for hiding this comment

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

maybe also a dry-run flag?

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.

3 participants