-
Notifications
You must be signed in to change notification settings - Fork 18
Layer cleanup to prevent runtime layer invalidation #1274
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a4d9c62
Layer cleanup to prevent runtime layer invalidation
colincasey 2ec02f3
Remove commented out filter
colincasey d0b2a17
Fix snapshot
colincasey 5739225
Use `PYTHONDONTWRITEBYTECODE` to reduce cleanup surface (#1275)
colincasey c24ea15
Update main.rs
colincasey 08be171
Merge branch 'main' into fix_runtime_layer_invalidation
colincasey e5b481b
Fix formatting errors
colincasey 0170f33
Code review suggestions
colincasey 2e695e4
Updated snapshots
colincasey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| use crate::NodeJsBuildpack; | ||
| use crate::layer_cleanup::LayerCleanupTarget; | ||
| use std::cell::RefCell; | ||
| use std::ops::Deref; | ||
|
|
||
| /// Wrapper around libcnb `BuildContext` that tracks layers needing cleanup | ||
| /// of non-deterministic build artifacts (Python bytecode, Makefiles) | ||
| pub(crate) struct NodeJsBuildContext { | ||
| inner: libcnb::build::BuildContext<NodeJsBuildpack>, | ||
| cleanup_registry: RefCell<Vec<LayerCleanupTarget>>, | ||
| } | ||
|
|
||
| impl NodeJsBuildContext { | ||
| pub(crate) fn new(inner: libcnb::build::BuildContext<NodeJsBuildpack>) -> Self { | ||
| Self { | ||
| inner, | ||
| cleanup_registry: RefCell::new(Vec::new()), | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn register_layer_for_cleanup(&self, target: LayerCleanupTarget) { | ||
| self.cleanup_registry.borrow_mut().push(target); | ||
| } | ||
|
|
||
| pub(crate) fn layers_to_cleanup(&self) -> Vec<LayerCleanupTarget> { | ||
| self.cleanup_registry.borrow().iter().cloned().collect() | ||
| } | ||
| } | ||
|
|
||
| impl Deref for NodeJsBuildContext { | ||
| type Target = libcnb::build::BuildContext<NodeJsBuildpack>; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.inner | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| use bullet_stream::global::print; | ||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
| use walkdir::WalkDir; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub(crate) enum LayerKind { | ||
| /// pnpm virtual store layer (contains native module builds with Makefiles) | ||
| Virtual, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub(crate) struct LayerCleanupTarget { | ||
| pub(crate) path: PathBuf, | ||
| pub(crate) kind: LayerKind, | ||
| } | ||
|
|
||
| /// Remove Makefile and *.mk files from native module build directories | ||
| /// These files have non-deterministic dependency ordering causing layer invalidation | ||
| fn remove_build_makefiles(base_path: &Path) -> Result<usize, std::io::Error> { | ||
| let mut removed_count = 0; | ||
|
|
||
| // Walk directory tree looking for build/Makefile patterns | ||
| for entry in WalkDir::new(base_path) | ||
| .into_iter() | ||
| .filter_map(Result::ok) | ||
| .filter(|e| { | ||
| if !e.file_type().is_file() { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if this is a Makefile or .mk file in a build/ directory | ||
| let path = e.path(); | ||
| if let Some(parent) = path.parent() | ||
| && parent.file_name() == Some(std::ffi::OsStr::new("build")) | ||
| && let Some(filename) = path.file_name() | ||
| { | ||
| return filename.to_string_lossy() == "Makefile"; | ||
| } | ||
|
|
||
| false | ||
| }) | ||
| { | ||
| fs::remove_file(entry.path())?; | ||
| removed_count += 1; | ||
| } | ||
|
|
||
| Ok(removed_count) | ||
| } | ||
|
|
||
| /// Clean up non-deterministic build artifacts from a layer | ||
| pub(crate) fn cleanup_layer(target: &LayerCleanupTarget) -> Result<(), std::io::Error> { | ||
| let path = &target.path; | ||
|
|
||
| if !path.exists() { | ||
| // Layer doesn't exist, nothing to clean | ||
| return Ok(()); | ||
| } | ||
|
|
||
| match target.kind { | ||
| LayerKind::Virtual => { | ||
| // pnpm virtual store: contains symlinked packages with native module builds | ||
| // Clean Makefiles from: virtual/store/*/node_modules/*/build/ | ||
| print::bullet("Cleaning up pnpm virtual store layer"); | ||
| let removed = remove_build_makefiles(path)?; | ||
| if removed > 0 { | ||
| print::sub_bullet(format!("Removed {removed} Makefile artifacts")); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use std::fs; | ||
| use tempfile::TempDir; | ||
|
|
||
| #[test] | ||
| fn test_remove_build_makefiles() { | ||
| let temp = TempDir::new().unwrap(); | ||
| let base = temp.path(); | ||
|
|
||
| // Create build directory with Makefile | ||
| let build_dir = base.join("node_modules/some-package/build"); | ||
| fs::create_dir_all(&build_dir).unwrap(); | ||
| fs::write(build_dir.join("Makefile"), b"makefile content").unwrap(); | ||
| fs::write(build_dir.join("output.o"), b"binary").unwrap(); // Should not be removed | ||
|
|
||
| let removed = remove_build_makefiles(base).unwrap(); | ||
|
|
||
| assert_eq!(removed, 1); // Makefile | ||
| assert!(!build_dir.join("Makefile").exists()); | ||
| assert!(build_dir.join("output.o").exists()); // Not a makefile | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.