diff --git a/buildpacks/dotnet/src/layers/nuget_cache.rs b/buildpacks/dotnet/src/layers/nuget_cache.rs index 9264ea3..7f46e62 100644 --- a/buildpacks/dotnet/src/layers/nuget_cache.rs +++ b/buildpacks/dotnet/src/layers/nuget_cache.rs @@ -27,7 +27,7 @@ type HandleResult = Result< pub(crate) fn handle( context: &BuildContext, - log: Print>, + mut log: Print>, ) -> HandleResult { let nuget_cache_layer = context.cached_layer( layer_name!("nuget-cache"), @@ -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) @@ -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)) } diff --git a/buildpacks/dotnet/tests/nuget_layer_test.rs b/buildpacks/dotnet/tests/nuget_layer_test.rs index 562ca00..61f3c0c 100644 --- a/buildpacks/dotnet/tests/nuget_layer_test.rs +++ b/buildpacks/dotnet/tests/nuget_layer_test.rs @@ -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"); }); });