Skip to content

Commit 585f66a

Browse files
authored
Rename the gc config table (#15367)
This renames the gc config table to `[cache]` to help avoid some confusion, and to set up a namespace for possible expansion in the future for different kind of cache controls. Low-level settings are stuffed into the `[cache.global-clean]` table, but we do not expect to stabilize these at this time. Only the top-level `cache.auto-clean-frequency` setting is expected to be stabilized. Closes #14292
2 parents 80525dc + efb3419 commit 585f66a

File tree

3 files changed

+48
-47
lines changed

3 files changed

+48
-47
lines changed

src/cargo/core/gc.rs

+31-34
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,15 @@ fn auto_gc_inner(gctx: &GlobalContext) -> CargoResult<()> {
9191
Ok(())
9292
}
9393

94-
/// Automatic garbage collection settings from the `gc.auto` config table.
94+
/// Cache cleaning settings from the `cache.global-clean` config table.
9595
///
9696
/// NOTE: Not all of these options may get stabilized. Some of them are very
9797
/// low-level details, and may not be something typical users need.
9898
///
9999
/// If any of these options are `None`, the built-in default is used.
100100
#[derive(Deserialize, Default)]
101101
#[serde(rename_all = "kebab-case")]
102-
struct AutoConfig {
103-
/// The maximum frequency that automatic garbage collection happens.
104-
frequency: Option<String>,
102+
struct GlobalCleanConfig {
105103
/// Anything older than this duration will be deleted in the source cache.
106104
max_src_age: Option<String>,
107105
/// Anything older than this duration will be deleted in the compressed crate cache.
@@ -173,49 +171,49 @@ impl GcOpts {
173171
/// Updates the configuration of this [`GcOpts`] to incorporate the
174172
/// settings from config.
175173
pub fn update_for_auto_gc(&mut self, gctx: &GlobalContext) -> CargoResult<()> {
176-
let auto_config = gctx
177-
.get::<Option<AutoConfig>>("gc.auto")?
174+
let config = gctx
175+
.get::<Option<GlobalCleanConfig>>("cache.global-clean")?
178176
.unwrap_or_default();
179-
self.update_for_auto_gc_config(&auto_config)
177+
self.update_for_auto_gc_config(&config)
180178
}
181179

182-
fn update_for_auto_gc_config(&mut self, auto_config: &AutoConfig) -> CargoResult<()> {
180+
fn update_for_auto_gc_config(&mut self, config: &GlobalCleanConfig) -> CargoResult<()> {
183181
self.max_src_age = newer_time_span_for_config(
184182
self.max_src_age,
185-
"gc.auto.max-src-age",
186-
auto_config
183+
"cache.global-clean.max-src-age",
184+
config
187185
.max_src_age
188186
.as_deref()
189187
.unwrap_or(DEFAULT_MAX_AGE_EXTRACTED),
190188
)?;
191189
self.max_crate_age = newer_time_span_for_config(
192190
self.max_crate_age,
193-
"gc.auto.max-crate-age",
194-
auto_config
191+
"cache.global-clean.max-crate-age",
192+
config
195193
.max_crate_age
196194
.as_deref()
197195
.unwrap_or(DEFAULT_MAX_AGE_DOWNLOADED),
198196
)?;
199197
self.max_index_age = newer_time_span_for_config(
200198
self.max_index_age,
201-
"gc.auto.max-index-age",
202-
auto_config
199+
"cache.global-clean.max-index-age",
200+
config
203201
.max_index_age
204202
.as_deref()
205203
.unwrap_or(DEFAULT_MAX_AGE_DOWNLOADED),
206204
)?;
207205
self.max_git_co_age = newer_time_span_for_config(
208206
self.max_git_co_age,
209-
"gc.auto.max-git-co-age",
210-
auto_config
207+
"cache.global-clean.max-git-co-age",
208+
config
211209
.max_git_co_age
212210
.as_deref()
213211
.unwrap_or(DEFAULT_MAX_AGE_EXTRACTED),
214212
)?;
215213
self.max_git_db_age = newer_time_span_for_config(
216214
self.max_git_db_age,
217-
"gc.auto.max-git-db-age",
218-
auto_config
215+
"cache.global-clean.max-git-db-age",
216+
config
219217
.max_git_db_age
220218
.as_deref()
221219
.unwrap_or(DEFAULT_MAX_AGE_DOWNLOADED),
@@ -255,30 +253,28 @@ impl<'a, 'gctx> Gc<'a, 'gctx> {
255253
/// Performs automatic garbage cleaning.
256254
///
257255
/// This returns immediately without doing work if garbage collection has
258-
/// been performed recently (since `gc.auto.frequency`).
256+
/// been performed recently (since `cache.auto-clean-frequency`).
259257
fn auto(&mut self, clean_ctx: &mut CleanContext<'gctx>) -> CargoResult<()> {
260258
if !self.gctx.cli_unstable().gc {
261259
return Ok(());
262260
}
263-
let auto_config = self
261+
let freq = self
264262
.gctx
265-
.get::<Option<AutoConfig>>("gc.auto")?
266-
.unwrap_or_default();
267-
let Some(freq) = parse_frequency(
268-
auto_config
269-
.frequency
270-
.as_deref()
271-
.unwrap_or(DEFAULT_AUTO_FREQUENCY),
272-
)?
273-
else {
263+
.get::<Option<String>>("cache.auto-clean-frequency")?;
264+
let Some(freq) = parse_frequency(freq.as_deref().unwrap_or(DEFAULT_AUTO_FREQUENCY))? else {
274265
tracing::trace!(target: "gc", "auto gc disabled");
275266
return Ok(());
276267
};
277268
if !self.global_cache_tracker.should_run_auto_gc(freq)? {
278269
return Ok(());
279270
}
271+
let config = self
272+
.gctx
273+
.get::<Option<GlobalCleanConfig>>("cache.global-clean")?
274+
.unwrap_or_default();
275+
280276
let mut gc_opts = GcOpts::default();
281-
gc_opts.update_for_auto_gc_config(&auto_config)?;
277+
gc_opts.update_for_auto_gc_config(&config)?;
282278
self.gc(clean_ctx, &gc_opts)?;
283279
if !clean_ctx.dry_run {
284280
self.global_cache_tracker.set_last_auto_gc()?;
@@ -337,7 +333,7 @@ fn parse_frequency(frequency: &str) -> CargoResult<Option<Duration>> {
337333
}
338334
let duration = maybe_parse_time_span(frequency).ok_or_else(|| {
339335
format_err!(
340-
"config option `gc.auto.frequency` expected a value of \"always\", \"never\", \
336+
"config option `cache.auto-clean-frequency` expected a value of \"always\", \"never\", \
341337
or \"N seconds/minutes/days/weeks/months\", got: {frequency:?}"
342338
)
343339
})?;
@@ -460,17 +456,18 @@ mod tests {
460456
assert_eq!(maybe_parse_time_span(" 1 day"), None);
461457
assert_eq!(maybe_parse_time_span("1 second"), None);
462458

463-
let e = parse_time_span_for_config("gc.auto.max-src-age", "-1 days").unwrap_err();
459+
let e =
460+
parse_time_span_for_config("cache.global-clean.max-src-age", "-1 days").unwrap_err();
464461
assert_eq!(
465462
e.to_string(),
466-
"config option `gc.auto.max-src-age` \
463+
"config option `cache.global-clean.max-src-age` \
467464
expected a value of the form \"N seconds/minutes/days/weeks/months\", \
468465
got: \"-1 days\""
469466
);
470467
let e = parse_frequency("abc").unwrap_err();
471468
assert_eq!(
472469
e.to_string(),
473-
"config option `gc.auto.frequency` \
470+
"config option `cache.auto-clean-frequency` \
474471
expected a value of \"always\", \"never\", or \"N seconds/minutes/days/weeks/months\", \
475472
got: \"abc\""
476473
);

src/doc/src/reference/unstable.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ cargo build -Zgc
16011601
Automatic deletion happens on commands that are already doing a significant amount of work,
16021602
such as all of the build commands (`cargo build`, `cargo test`, `cargo check`, etc.), and `cargo fetch`.
16031603
The deletion happens just after resolution and packages have been downloaded.
1604-
Automatic deletion is only done once per day (see `gc.auto.frequency` to configure).
1604+
Automatic deletion is only done once per day (see `cache.auto-clean-frequency` to configure).
16051605
Automatic deletion is disabled if cargo is offline such as with `--offline` or `--frozen` to avoid deleting artifacts that may need to be used if you are offline for a long period of time.
16061606

16071607
#### Automatic gc configuration
@@ -1612,11 +1612,14 @@ The settings available are:
16121612
```toml
16131613
# Example config.toml file.
16141614

1615-
# This table defines the behavior for automatic garbage collection.
1616-
[gc.auto]
1617-
# The maximum frequency that automatic garbage collection happens.
1618-
# Can be "never" to disable automatic-gc, or "always" to run on every command.
1619-
frequency = "1 day"
1615+
# This table defines settings for cargo's caches.
1616+
[cache]
1617+
# The maximum frequency that automatic cleaning of the cache happens.
1618+
# Can be "never" to disable, or "always" to run on every command.
1619+
auto-clean-frequency = "1 day"
1620+
1621+
# Sub-table for defining specific settings for cleaning the global cache.
1622+
[cache.global-clean]
16201623
# Anything older than this duration will be deleted in the source cache.
16211624
max-src-age = "1 month"
16221625
# Anything older than this duration will be deleted in the compressed crate cache.

tests/testsuite/global_cache_tracker.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,9 @@ fn auto_gc_config() {
338338
.file(
339339
".cargo/config.toml",
340340
r#"
341-
[gc.auto]
342-
frequency = "always"
341+
[cache]
342+
auto-clean-frequency = "always"
343+
[cache.global-clean]
343344
max-src-age = "1 day"
344345
max-crate-age = "3 days"
345346
max-index-age = "3 days"
@@ -407,13 +408,13 @@ fn auto_gc_config() {
407408

408409
#[cargo_test]
409410
fn frequency() {
410-
// gc.auto.frequency settings
411+
// cache.auto-clean-frequency settings
411412
let p = basic_foo_bar_project();
412413
p.change_file(
413414
".cargo/config.toml",
414415
r#"
415-
[gc.auto]
416-
frequency = "never"
416+
[cache]
417+
auto-clean-frequency = "never"
417418
"#,
418419
);
419420
// Populate data in the past.
@@ -437,7 +438,7 @@ fn frequency() {
437438

438439
// Try again with a setting that allows it to run.
439440
p.cargo("check -Zgc")
440-
.env("CARGO_GC_AUTO_FREQUENCY", "1 day")
441+
.env("CARGO_CACHE_AUTO_CLEAN_FREQUENCY", "1 day")
441442
.masquerade_as_nightly_cargo(&["gc"])
442443
.run();
443444
assert_eq!(get_index_names().len(), 0);
@@ -1258,7 +1259,7 @@ fn package_cache_lock_during_build() {
12581259
p_foo2
12591260
.cargo("check -Zgc")
12601261
.masquerade_as_nightly_cargo(&["gc"])
1261-
.env("CARGO_GC_AUTO_FREQUENCY", "always")
1262+
.env("CARGO_CACHE_AUTO_CLEAN_FREQUENCY", "always")
12621263
.env("CARGO_LOG", "gc=debug")
12631264
.with_stderr_data(str![[r#"
12641265
[UPDATING] `dummy-registry` index

0 commit comments

Comments
 (0)