Revert "add cache clean cli subcommand"

This reverts commit 51d1477e4a.
This commit is contained in:
Luke Boswell 2025-11-30 11:07:48 +11:00
parent d4595d7356
commit bdc447db95
No known key found for this signature in database
GPG key ID: 54A7324B1B975757
3 changed files with 31 additions and 87 deletions

View file

@ -660,7 +660,7 @@ pub fn build(b: *std.Build) void {
// build steps
const run_step = b.step("run", "Build and run the roc cli");
const roc_step = b.step("roc", "Build the roc compiler without running it");
const test_step = b.step("test", "Run all tests");
const test_step = b.step("test", "Run all tests included in src/tests.zig");
const minici_step = b.step("minici", "Run a subset of CI build and test steps");
const fmt_step = b.step("fmt", "Format all zig code");
const check_fmt_step = b.step("check-fmt", "Check formatting of all zig code");

View file

@ -19,7 +19,6 @@ pub const CliArgs = union(enum) {
version,
docs: DocsArgs,
experimental_lsp: ExperimentalLspArgs,
clean_cache,
help: []const u8,
licenses,
problem: CliProblem,
@ -150,7 +149,6 @@ pub fn parse(alloc: mem.Allocator, args: []const []const u8) !CliArgs {
if (mem.eql(u8, args[0], "experimental-lsp")) return parseExperimentalLsp(args[1..]);
if (mem.eql(u8, args[0], "help")) return CliArgs{ .help = main_help };
if (mem.eql(u8, args[0], "licenses")) return parseLicenses(args[1..]);
if (mem.eql(u8, args[0], "clean-cache")) return parseCleanCache(args[1..]);
return try parseRun(alloc, args);
}
@ -173,7 +171,6 @@ const main_help =
\\ check Check the code for problems, but don't build or run it
\\ docs Generate documentation for a Roc package or platform
\\ experimental-lsp Start the experimental language server (LSP) implementation
\\ clean-cache Delete the Roc cache directory
\\ help Print this message
\\ licenses Prints license info for Roc as well as attributions to other projects used by Roc
\\
@ -565,7 +562,7 @@ fn parseVersion(args: []const []const u8) CliArgs {
fn parseLicenses(args: []const []const u8) CliArgs {
for (args) |arg| {
if (isHelpFlag(arg)) {
return CliArgs{ .help =
return CliArgs{ .help =
\\Prints license info for Roc as well as attributions to other projects used by Roc
\\
\\Usage: roc licenses
@ -581,31 +578,6 @@ fn parseLicenses(args: []const []const u8) CliArgs {
return CliArgs.licenses;
}
fn parseCleanCache(args: []const []const u8) CliArgs {
for (args) |arg| {
if (isHelpFlag(arg)) {
return CliArgs{ .help =
\\Delete the Roc cache directory
\\
\\This removes all cached packages and temporary files.
\\Cache locations by platform:
\\ - Linux: ~/.cache/roc/ (or $XDG_CACHE_HOME/roc/)
\\ - macOS: ~/Library/Caches/roc/ (or $XDG_CACHE_HOME/roc/)
\\ - Windows: %APPDATA%\Roc\
\\
\\Usage: roc clean-cache
\\
\\Options:
\\ -h, --help Print help
\\
};
} else {
return CliArgs{ .problem = CliProblem{ .unexpected_argument = .{ .cmd = "clean-cache", .arg = arg } } };
}
}
return CliArgs.clean_cache;
}
fn parseDocs(args: []const []const u8) CliArgs {
var path: ?[]const u8 = null;
var main: ?[]const u8 = null;
@ -1257,27 +1229,3 @@ test "roc licenses" {
try testing.expectEqualStrings("extrastuff", result.problem.unexpected_argument.arg);
}
}
test "roc clean-cache" {
const gpa = testing.allocator;
{
const result = try parse(gpa, &[_][]const u8{"clean-cache"});
defer result.deinit(gpa);
try testing.expectEqual(.clean_cache, std.meta.activeTag(result));
}
{
const result = try parse(gpa, &[_][]const u8{ "clean-cache", "extrastuff" });
defer result.deinit(gpa);
try testing.expectEqualStrings("extrastuff", result.problem.unexpected_argument.arg);
}
{
const result = try parse(gpa, &[_][]const u8{ "clean-cache", "-h" });
defer result.deinit(gpa);
try testing.expectEqual(.help, std.meta.activeTag(result));
}
{
const result = try parse(gpa, &[_][]const u8{ "clean-cache", "--help" });
defer result.deinit(gpa);
try testing.expectEqual(.help, std.meta.activeTag(result));
}
}

View file

@ -584,9 +584,6 @@ fn mainArgs(allocs: *Allocators, args: []const []const u8) !void {
.licenses => {
try stdout.writeAll(legalDetailsFileContent);
},
.clean_cache => {
cleanCache(stdout, stderr);
},
.problem => |problem| {
try switch (problem) {
.missing_flag_value => |details| stderr.print("Error: no value was supplied for {s}\n", .{details.flag}),
@ -2166,39 +2163,38 @@ fn resolvePlatformSpecToPaths(allocs: *Allocators, platform_spec: []const u8, ba
}
}
/// Get the roc cache directory for downloaded packages.
/// Uses CacheConfig for consistent cache directory locations across the codebase.
/// Get the roc cache directory for downloaded packages, creating it if needed.
/// Standard cache locations by platform:
/// - Linux/macOS: ~/.cache/roc/packages/ (respects XDG_CACHE_HOME if set)
/// - Windows: %LOCALAPPDATA%\roc\packages\
fn getRocCacheDir(allocator: std.mem.Allocator) ![]const u8 {
const base_dir = try CacheConfig.getDefaultCacheDir(allocator);
defer allocator.free(base_dir);
return std.fs.path.join(allocator, &.{ base_dir, "packages" });
// Check XDG_CACHE_HOME first (Linux/macOS)
if (getEnvVar(allocator, "XDG_CACHE_HOME")) |xdg_cache| {
defer allocator.free(xdg_cache);
return std.fs.path.join(allocator, &.{ xdg_cache, "roc", "packages" });
}
// Fall back to %LOCALAPPDATA%\roc\packages (Windows)
if (comptime builtin.os.tag == .windows) {
if (getEnvVar(allocator, "LOCALAPPDATA")) |local_app_data| {
defer allocator.free(local_app_data);
return std.fs.path.join(allocator, &.{ local_app_data, "roc", "packages" });
}
}
// Fall back to ~/.cache/roc/packages (Unix)
if (getEnvVar(allocator, "HOME")) |home| {
defer allocator.free(home);
return std.fs.path.join(allocator, &.{ home, ".cache", "roc", "packages" });
}
return error.NoCacheDir;
}
/// Delete the Roc cache directory.
fn cleanCache(stdout: *std.Io.Writer, stderr: *std.Io.Writer) void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const cache_dir = CacheConfig.getDefaultCacheDir(allocator) catch {
stderr.print("Error: Could not determine cache directory location\n", .{}) catch {};
return;
};
defer allocator.free(cache_dir);
// Check if the directory exists
std.fs.cwd().access(cache_dir, .{}) catch {
stdout.print("Cache directory does not exist: {s}\n", .{cache_dir}) catch {};
return;
};
// Delete the directory recursively
std.fs.cwd().deleteTree(cache_dir) catch |err| {
stderr.print("Error: Failed to delete cache directory {s}: {}\n", .{ cache_dir, err }) catch {};
return;
};
stdout.print("Deleted cache directory: {s}\n", .{cache_dir}) catch {};
/// Cross-platform helper to get environment variable.
/// Returns null if the variable is not set. Caller must free the returned slice.
fn getEnvVar(allocator: std.mem.Allocator, key: []const u8) ?[]const u8 {
return std.process.getEnvVarOwned(allocator, key) catch null;
}
/// Get list of target directory names to try, in preference order.