Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve NuGet cache log output #151

Merged
merged 12 commits into from
Nov 11, 2024
52 changes: 21 additions & 31 deletions buildpacks/dotnet/src/layers/nuget_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type HandleResult = Result<

pub(crate) fn handle(
context: &BuildContext<DotnetBuildpack>,
log: Print<state::Bullet<Stdout>>,
mut log: Print<state::Bullet<Stdout>>,
) -> HandleResult {
let nuget_cache_layer = context.cached_layer(
layer_name!("nuget-cache"),
Expand All @@ -36,7 +36,7 @@ pub(crate) fn handle(
launch: false,
invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
restored_layer_action: &|metadata: &NugetCacheLayerMetadata, _path| {
if metadata.restore_count > MAX_NUGET_CACHE_RESTORE_COUNT {
if metadata.restore_count >= MAX_NUGET_CACHE_RESTORE_COUNT {
(RestoredLayerAction::DeleteLayer, metadata.restore_count)
} else {
(RestoredLayerAction::KeepLayer, metadata.restore_count)
Expand All @@ -45,36 +45,26 @@ pub(crate) fn handle(
},
)?;

let mut log_bullet = log.bullet("NuGet cache");
nuget_cache_layer.write_metadata(NugetCacheLayerMetadata {
restore_count: match nuget_cache_layer.state {
LayerState::Restored { cause: count } => count + 1.0,
LayerState::Empty { .. } => 0.0,
},
})?;

match nuget_cache_layer.state {
LayerState::Restored {
cause: restore_count,
} => {
log_bullet = log_bullet.sub_bullet("Reusing NuGet package cache");
nuget_cache_layer.write_metadata(NugetCacheLayerMetadata {
restore_count: restore_count + 1.0,
})?;
}
LayerState::Empty { cause } => {
match cause {
EmptyLayerCause::NewlyCreated => {
log_bullet = log_bullet.sub_bullet("Created NuGet package cache");
}
EmptyLayerCause::InvalidMetadataAction { .. } => {
log_bullet =
log_bullet.sub_bullet("Purged NuGet package cache due to invalid metadata");
}
EmptyLayerCause::RestoredLayerAction {
cause: restore_count,
} => {
log_bullet = log_bullet.sub_bullet(format!(
"Purged NuGet package cache after {restore_count} builds"
));
}
if let Some(message) = match nuget_cache_layer.state {
LayerState::Restored { .. } => Some("Reusing package cache".to_string()),
LayerState::Empty { cause } => match cause {
EmptyLayerCause::NewlyCreated => None,
EmptyLayerCause::InvalidMetadataAction { .. } => {
Some("Clearing package cache due to invalid metadata".to_string())
}
EmptyLayerCause::RestoredLayerAction { cause: count } => {
Some(format!("Clearing package cache after {count} uses"))
}
nuget_cache_layer.write_metadata(NugetCacheLayerMetadata { restore_count: 1.0 })?;
}
},
} {
log = log.bullet("NuGet cache").sub_bullet(message).done();
}
Ok((nuget_cache_layer, log_bullet.done()))
Ok((nuget_cache_layer, log))
}
4 changes: 2 additions & 2 deletions buildpacks/dotnet/tests/nuget_layer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ fn test_nuget_restore_and_cache() {
.env("MSBUILD_VERBOSITY_LEVEL", "normal"),
|context| {
assert_empty!(context.pack_stderr);
assert_contains!(&context.pack_stdout, "Created NuGet package cache");
assert_not_contains!(&context.pack_stdout, "NuGet cache");
assert_contains!(&context.pack_stdout, "Installed Newtonsoft.Json 13.0.3 from https://api.nuget.org/v3/index.json to /layers/heroku_dotnet/nuget-cache/newtonsoft.json/13.0.3 with content hash HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ==.");
assert_contains!(&context.pack_stdout, "Restored /workspace/consoleapp.csproj");

// Verify NuGet package layer caching behavior
let config = context.config.clone();
context.rebuild(config, |rebuild_context| {
assert_not_contains!(&rebuild_context.pack_stdout, "Created NuGet package cache");
assert_not_contains!(&rebuild_context.pack_stdout, "Installed Newtonsoft.Json 13.0.3");
assert_contains!(&rebuild_context.pack_stdout, "Reusing package cache");
assert_contains!(&rebuild_context.pack_stdout, "Restored /workspace/consoleapp.csproj");
});
});
Expand Down