-
Notifications
You must be signed in to change notification settings - Fork 2
feat(packages): fail at unpack when a core arrives without its submodules #1401
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
Open
zackees
wants to merge
2
commits into
main
Choose a base branch
from
feat/1400-detect-missing-submodules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,117
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,234 @@ | ||
| //! Detect a core unpacked without its git submodule contents. | ||
| //! | ||
| //! GitHub's auto-generated source archives (`archive/refs/tags/…`) omit | ||
| //! submodules by design: the directories are created, the contents are not. | ||
| //! Several Arduino cores keep libraries as submodules, so an archive-sourced | ||
| //! package extracts to something that looks complete and fails much later, | ||
| //! inside the core's own headers. | ||
| //! | ||
| //! FastLED/fbuild#1380 is the worked example. `esp8266/Arduino` carries | ||
| //! `libraries/LittleFS/lib/littlefs`, so `#include <LittleFS.h>` reached | ||
| //! | ||
| //! ```text | ||
| //! LittleFS.h:38:10: fatal error: ../lib/littlefs/lfs.h: No such file | ||
| //! ``` | ||
| //! | ||
| //! and `__has_include(<LittleFS.h>)` still passed, because the header was | ||
| //! present and only the thing it includes was absent. No consumer-side guard | ||
| //! can detect that. | ||
| //! | ||
| //! The archive carries `.gitmodules` even when it drops the submodule | ||
| //! contents, which is what makes this cheap to catch: the file states exactly | ||
| //! which directories are supposed to be non-empty. | ||
|
|
||
| use std::path::Path; | ||
|
|
||
| use fbuild_core::path::NormalizedPath; | ||
|
|
||
| /// A declared submodule whose directory came out of the archive empty. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct EmptySubmodule { | ||
| /// Path as written in `.gitmodules`, relative to the repo root. | ||
| pub declared_path: String, | ||
| /// Where that landed on disk. | ||
| pub extracted_at: NormalizedPath, | ||
| } | ||
|
|
||
| /// Parse the `path = …` entries out of a `.gitmodules` file. | ||
| /// | ||
| /// Deliberately not a full INI parse. `.gitmodules` is written by git, the | ||
| /// only field this needs is `path`, and a permissive line scan cannot fail | ||
| /// closed on an unusual-but-valid file the way a strict parser can. | ||
| pub fn declared_submodule_paths(gitmodules: &str) -> Vec<String> { | ||
| gitmodules | ||
| .lines() | ||
| .filter_map(|line| { | ||
| let (key, value) = line.split_once('=')?; | ||
| if key.trim() != "path" { | ||
| return None; | ||
| } | ||
| let value = value.trim(); | ||
| (!value.is_empty()).then(|| value.to_string()) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Whether a directory has no entries. A missing directory is *not* empty for | ||
| /// this purpose: git records the submodule directory itself in the archive, so | ||
| /// its absence means something else went wrong and this check should not | ||
| /// claim otherwise. | ||
| fn is_empty_dir(path: &Path) -> bool { | ||
| match std::fs::read_dir(path) { | ||
| Ok(mut entries) => entries.next().is_none(), | ||
| Err(_) => false, | ||
| } | ||
| } | ||
|
|
||
| /// Report every declared submodule that extracted empty under `root`. | ||
| /// | ||
| /// Returns an empty vec when `root` has no `.gitmodules` — most packages are | ||
| /// not git repositories at all, and their absence is the normal case rather | ||
| /// than a problem. | ||
| pub fn find_empty_submodules(root: &Path) -> Vec<EmptySubmodule> { | ||
| let gitmodules = root.join(".gitmodules"); | ||
| let Ok(text) = std::fs::read_to_string(&gitmodules) else { | ||
| return Vec::new(); | ||
| }; | ||
|
|
||
| declared_submodule_paths(&text) | ||
| .into_iter() | ||
| .filter_map(|declared| { | ||
| let extracted_at = root.join(&declared); | ||
| is_empty_dir(&extracted_at).then(|| EmptySubmodule { | ||
| declared_path: declared, | ||
| extracted_at: NormalizedPath::from(extracted_at), | ||
| }) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Message for a package that unpacked without its submodule contents. | ||
| /// | ||
| /// Names the empty directories and the likely cause, because the symptom this | ||
| /// prevents — a missing header several layers inside a core — gives the reader | ||
| /// nothing to work with. | ||
| pub fn empty_submodule_error(package: &str, url: &str, empty: &[EmptySubmodule]) -> String { | ||
| let listed = empty | ||
| .iter() | ||
| .map(|e| format!(" - {}", e.declared_path)) | ||
| .collect::<Vec<_>>() | ||
| .join("\n"); | ||
| let source_archive_hint = if url.contains("/archive/refs/") { | ||
| "\n\nThe URL above is a GitHub auto-generated source archive, which \ | ||
| omits submodules by design. Use the release asset published on the \ | ||
| tag if the project provides one (that is what FastLED/fbuild#1380 \ | ||
| did for esp8266), or fetch with submodules." | ||
| } else { | ||
| "\n\nThe archive declares these submodules but shipped them empty." | ||
| }; | ||
| format!( | ||
| "{package} unpacked without its submodule contents. These directories \ | ||
| are declared in .gitmodules and came out empty:\n{listed}\n\nurl: \ | ||
| {url}{source_archive_hint}\n\nLeaving this to the compiler produces a \ | ||
| missing-header error inside the core, past any `__has_include` guard \ | ||
| a sketch could write (FastLED/fbuild#1380)." | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn write(root: &Path, rel: &str, body: &str) { | ||
| let p = root.join(rel); | ||
| std::fs::create_dir_all(p.parent().unwrap()).unwrap(); | ||
| std::fs::write(p, body).unwrap(); | ||
| } | ||
|
|
||
| const ESP8266_GITMODULES: &str = "\ | ||
| [submodule \"libraries/LittleFS/lib/littlefs\"] | ||
| \tpath = libraries/LittleFS/lib/littlefs | ||
| \turl = https://github.com/littlefs-project/littlefs.git | ||
| [submodule \"libraries/SoftwareSerial\"] | ||
| \tpath = libraries/SoftwareSerial | ||
| \turl = https://github.com/plerup/espsoftwareserial.git | ||
| "; | ||
|
|
||
| #[test] | ||
| fn declared_paths_are_read_from_gitmodules() { | ||
| assert_eq!( | ||
| declared_submodule_paths(ESP8266_GITMODULES), | ||
| vec![ | ||
| "libraries/LittleFS/lib/littlefs".to_string(), | ||
| "libraries/SoftwareSerial".to_string(), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /// The exact shape FastLED/fbuild#1380 reported: directories present, | ||
| /// contents absent. | ||
| #[test] | ||
| fn empty_submodule_directories_are_reported() { | ||
| let tmp = tempfile::TempDir::new().unwrap(); | ||
| let root = tmp.path(); | ||
| write(root, ".gitmodules", ESP8266_GITMODULES); | ||
| std::fs::create_dir_all(root.join("libraries/LittleFS/lib/littlefs")).unwrap(); | ||
| std::fs::create_dir_all(root.join("libraries/SoftwareSerial")).unwrap(); | ||
| // The vendored sources next to the empty submodule are what made the | ||
| // real failure confusing — lfs.c present, lfs.h absent. | ||
| write( | ||
| root, | ||
| "libraries/LittleFS/src/LittleFS.h", | ||
| "#include \"../lib/littlefs/lfs.h\"", | ||
| ); | ||
|
|
||
| let found = find_empty_submodules(root); | ||
| let paths: Vec<&str> = found.iter().map(|e| e.declared_path.as_str()).collect(); | ||
| assert_eq!( | ||
| paths, | ||
| vec![ | ||
| "libraries/LittleFS/lib/littlefs", | ||
| "libraries/SoftwareSerial" | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn populated_submodules_are_not_reported() { | ||
| let tmp = tempfile::TempDir::new().unwrap(); | ||
| let root = tmp.path(); | ||
| write(root, ".gitmodules", ESP8266_GITMODULES); | ||
| write(root, "libraries/LittleFS/lib/littlefs/lfs.h", "// header"); | ||
| write( | ||
| root, | ||
| "libraries/SoftwareSerial/SoftwareSerial.h", | ||
| "// header", | ||
| ); | ||
| assert!(find_empty_submodules(root).is_empty()); | ||
| } | ||
|
|
||
| /// Most packages are plain archives, not git checkouts. No `.gitmodules` | ||
| /// is the normal case and must not be treated as a finding. | ||
| #[test] | ||
| fn a_package_without_gitmodules_is_clean() { | ||
| let tmp = tempfile::TempDir::new().unwrap(); | ||
| write(tmp.path(), "cores/arduino/main.cpp", "int main(){}"); | ||
| assert!(find_empty_submodules(tmp.path()).is_empty()); | ||
| } | ||
|
|
||
| /// A declared submodule whose directory is missing entirely is a | ||
| /// different failure — an incomplete extract, not a submodule-less | ||
| /// archive. Reporting it here would send the reader after the wrong | ||
| /// cause. | ||
| #[test] | ||
| fn a_missing_submodule_directory_is_not_claimed_as_empty() { | ||
| let tmp = tempfile::TempDir::new().unwrap(); | ||
| write(tmp.path(), ".gitmodules", ESP8266_GITMODULES); | ||
| assert!(find_empty_submodules(tmp.path()).is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn the_error_names_the_directories_and_the_archive_kind() { | ||
| let empty = vec![EmptySubmodule { | ||
| declared_path: "libraries/LittleFS/lib/littlefs".to_string(), | ||
| extracted_at: NormalizedPath::from("/cache/x/libraries/LittleFS/lib/littlefs"), | ||
| }]; | ||
| let msg = empty_submodule_error( | ||
| "esp8266-arduino", | ||
| "https://github.com/esp8266/Arduino/archive/refs/tags/3.1.2.tar.gz", | ||
| &empty, | ||
| ); | ||
| assert!(msg.contains("libraries/LittleFS/lib/littlefs"), "{msg}"); | ||
| assert!(msg.contains("source archive"), "{msg}"); | ||
|
|
||
| let release = empty_submodule_error( | ||
| "esp8266-arduino", | ||
| "https://github.com/esp8266/Arduino/releases/download/3.1.2/esp8266-3.1.2.zip", | ||
| &empty, | ||
| ); | ||
| assert!( | ||
| !release.contains("source archive"), | ||
| "a release-asset URL must not be blamed on the archive form: {release}" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🌐 Web query:
According to the Rust standard-library documentation for the repository's declared Rust toolchain, what does std::path::Path::join do when its argument is absolute, and how does Path::components represent..and platform path prefixes?💡 Result:
According to the Rust standard library documentation, the behavior of std::path::Path::join and the representation of components are as follows: Path::join and Absolute Arguments When the argument provided to Path::join is absolute, it replaces the current path [1][2]. More specifically, the method creates an owned PathBuf with the path adjoined to self, but if the path argument is absolute, self is effectively ignored, and the argument becomes the result [1][3]. On Windows, this behavior is nuanced [1][3]: - If the argument has a root but no prefix (e.g., \windows), it replaces and returns everything except for the prefix (if any) of the original path [1][3]. - If the argument has a prefix but no root, the original path is ignored, and the argument is returned [1][3]. - If the original path has a verbatim prefix (e.g., \?\C:\windows) and the argument is not empty, the new path is normalized to remove all references to. and.. [1][3]. Path::components and Representation The Path::components method returns an iterator over the components of a path, where each component is represented by the Component enum [4][5]. - Representation of
..: The..path component is represented by the Component::ParentDir variant [4][5]. - Representation of Platform Prefixes: Platform-specific path prefixes (which occur on Windows) are represented by the Component::Prefix variant [4][5]. This variant holds a PrefixComponent struct, which encapsulates the raw, unparsed OsStr slice of the prefix as well as the parsed prefix data (accessible via the kind method) [6][7]. Unix paths do not have prefixes, so this variant does not occur there [6][8].Citations:
🏁 Script executed:
Repository: FastLED/fbuild
Length of output: 20529
🏁 Script executed:
Repository: FastLED/fbuild
Length of output: 50371
🏁 Script executed:
Repository: FastLED/fbuild
Length of output: 50373
🏁 Script executed:
Repository: FastLED/fbuild
Length of output: 3670
🏁 Script executed:
Repository: FastLED/fbuild
Length of output: 493
🏁 Script executed:
Repository: FastLED/fbuild
Length of output: 355
Keep declared submodule paths inside
root.root.join(&declared)accepts absolute paths and..components. A package-controlled.gitmodulescan makeis_empty_dirinspect an empty directory outside the extraction root and reject installation. RejectParentDir,RootDir, and WindowsPrefixcomponents. Also check resolved paths, because a symlink can bypass lexical checks. Add tests for absolute, parent-directory, and symlink paths.🤖 Prompt for AI Agents