Skip to content

Commit c3f4901

Browse files
committed
add global config to enable artifact cache, debug logging for test
1 parent 3758e0d commit c3f4901

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub struct Config {
9595

9696
// Build params
9797
pub(crate) build_attempts: u16,
98+
pub(crate) use_build_artifact_cache: bool,
9899
pub(crate) rustwide_workspace: PathBuf,
99100
pub(crate) inside_docker: bool,
100101
pub(crate) docker_image: Option<String>,
@@ -130,6 +131,7 @@ impl Config {
130131

131132
Ok(Self {
132133
build_attempts: env("DOCSRS_BUILD_ATTEMPTS", 5)?,
134+
use_build_artifact_cache: env("DOCSRS_USE_BUILD_ARTIFACT_CACHE", true)?,
133135

134136
crates_io_api_call_retries: env("DOCSRS_CRATESIO_API_CALL_RETRIES", 3)?,
135137

src/docbuilder/rustwide_builder.rs

+30-14
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct RustwideBuilder {
5151
storage: Arc<Storage>,
5252
metrics: Arc<Metrics>,
5353
index: Arc<Index>,
54-
artifact_cache: ArtifactCache,
54+
artifact_cache: Option<ArtifactCache>,
5555
rustc_version: String,
5656
repository_stats_updater: Arc<RepositoryStatsUpdater>,
5757
skip_build_if_exists: bool,
@@ -72,11 +72,17 @@ impl RustwideBuilder {
7272
builder = builder.sandbox_image(image);
7373
}
7474

75-
let artifact_cache = ArtifactCache::new(config.prefix.join("artifact_cache"))?;
75+
let artifact_cache = if config.use_build_artifact_cache {
76+
Some(ArtifactCache::new(config.prefix.join("artifact_cache"))?)
77+
} else {
78+
None
79+
};
7680

7781
if cfg!(test) {
7882
builder = builder.fast_init(true);
79-
artifact_cache.purge()?;
83+
if let Some(ref artifact_cache) = artifact_cache {
84+
artifact_cache.purge()?;
85+
}
8086
}
8187

8288
let workspace = builder.init().map_err(FailureError::compat)?;
@@ -209,7 +215,9 @@ impl RustwideBuilder {
209215

210216
let has_changed = old_version.as_deref() != Some(&self.rustc_version);
211217
if has_changed {
212-
self.artifact_cache.purge()?;
218+
if let Some(ref artifact_cache) = self.artifact_cache {
219+
artifact_cache.purge()?;
220+
}
213221
self.add_essential_files()?;
214222
}
215223
Ok(has_changed)
@@ -418,15 +426,16 @@ impl RustwideBuilder {
418426
}
419427
};
420428

421-
if let Some(ref published_by) = release_data.published_by {
429+
if let (Some(ref artifact_cache), Some(ref published_by)) =
430+
(&self.artifact_cache, release_data.published_by)
431+
{
422432
info!(
423433
host_target_dir=?build.host_target_dir(),
424434
published_by_id=published_by.id,
425435
published_by_login=published_by.login,
426436
"restoring artifact cache",
427437
);
428-
if let Err(err) = self
429-
.artifact_cache
438+
if let Err(err) = artifact_cache
430439
.restore_to(&published_by.id.to_string(), build.host_target_dir())
431440
{
432441
warn!(?err, "could not restore artifact cache");
@@ -587,15 +596,16 @@ impl RustwideBuilder {
587596
}
588597
}
589598

590-
if let Some(ref published_by) = release_data.published_by {
599+
if let (Some(artifact_cache), Some(ref published_by)) =
600+
(&self.artifact_cache, release_data.published_by)
601+
{
591602
info!(
592603
host_target_dir=?build.host_target_dir(),
593604
published_by_id=published_by.id,
594605
published_by_login=published_by.login,
595606
"saving artifact cache",
596607
);
597-
if let Err(err) = self
598-
.artifact_cache
608+
if let Err(err) = artifact_cache
599609
.save(&published_by.id.to_string(), build.host_target_dir())
600610
.context("error saving artifact cache")
601611
{
@@ -1147,21 +1157,27 @@ mod tests {
11471157
// first build creates the cache
11481158
assert!(!expected_cache_dir.exists());
11491159
assert!(builder.build_package(crate_, version, PackageKind::CratesIo)?);
1160+
1161+
for chld in std::fs::read_dir(expected_cache_dir.parent().unwrap())? {
1162+
dbg!(&chld);
1163+
}
1164+
11501165
assert!(expected_cache_dir.exists());
11511166

11521167
// cache dir doesn't contain doc output
11531168
assert!(!expected_cache_dir.join("doc").exists());
11541169

1155-
// but seems to be a normal cargo target directory
1170+
// but seems to be a normal cargo target directory,
1171+
// which also means that `build_package` actually used the
1172+
// target directory, and it was moved into the cache afterwards.
11561173
for expected_file in &["CACHEDIR.TAG", "debug"] {
11571174
assert!(expected_cache_dir.join(expected_file).exists());
11581175
}
11591176

1160-
// do a second build
1177+
// do a second build,
1178+
// should not fail
11611179
assert!(builder.build_package(crate_, version, PackageKind::CratesIo)?);
11621180

1163-
// FIXME: how would I know if the cache was used?
1164-
11651181
Ok(())
11661182
});
11671183
}

0 commit comments

Comments
 (0)