-
Notifications
You must be signed in to change notification settings - Fork 146
[hermes] Resolve Cargo and Hermes target dirs #3007
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
base: Gdc72294eb3c40c4afa61ea6e8f965ba310cd22ef
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| use std::{env, path::PathBuf}; | ||
| use std::{ | ||
| env, | ||
| hash::{Hash as _, Hasher as _}, | ||
| path::PathBuf, | ||
| }; | ||
|
|
||
| use anyhow::{anyhow, Context, Result}; | ||
| use cargo_metadata::{ | ||
| Metadata, MetadataCommand, Package, PackageName, Target, TargetKind, | ||
| }; | ||
| use cargo_metadata::{Metadata, MetadataCommand, Package, PackageName, Target, TargetKind}; | ||
| use clap::Parser; | ||
|
|
||
| #[derive(Parser, Debug)] | ||
|
|
@@ -98,6 +100,8 @@ impl TryFrom<&TargetKind> for HermesTargetKind { | |
|
|
||
| pub struct Roots { | ||
| pub workspace: PathBuf, | ||
| pub cargo_target_dir: PathBuf, | ||
| pub shadow_root: PathBuf, | ||
| pub roots: Vec<(PackageName, HermesTargetKind, PathBuf)>, | ||
| } | ||
|
|
||
|
|
@@ -120,8 +124,12 @@ pub fn resolve_roots(args: &Args) -> Result<Roots> { | |
|
|
||
| let selected_packages = resolve_packages(&metadata, &args.workspace)?; | ||
|
|
||
| let mut roots = | ||
| Roots { workspace: metadata.workspace_root.as_std_path().to_owned(), roots: Vec::new() }; | ||
| let mut roots = Roots { | ||
| workspace: metadata.workspace_root.as_std_path().to_owned(), | ||
| cargo_target_dir: metadata.target_directory.as_std_path().to_owned(), | ||
| shadow_root: resolve_shadow_path(&metadata), | ||
| roots: Vec::new(), | ||
| }; | ||
|
|
||
| for package in selected_packages { | ||
| log::trace!("Scanning package: {}", package.name); | ||
|
|
@@ -145,6 +153,22 @@ pub fn resolve_roots(args: &Args) -> Result<Roots> { | |
| Ok(roots) | ||
| } | ||
|
|
||
| fn resolve_shadow_path(metadata: &Metadata) -> PathBuf { | ||
| // NOTE: Automatically handles `CARGO_TARGET_DIR` env var. | ||
| let target_dir = metadata.target_directory.as_std_path(); | ||
|
|
||
| // Hash the path to the workspace root to avoid collisions between different | ||
| // workspaces using the same target directory. | ||
| let workspace_root_hash = { | ||
| let mut hasher = std::hash::DefaultHasher::new(); | ||
| hasher.write(b"hermes_shadow_salt"); | ||
| metadata.workspace_root.hash(&mut hasher); | ||
| hasher.finish() | ||
| }; | ||
|
Comment on lines
+162
to
+167
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a build tool where deterministic output is important, it's better to use a stable hashing algorithm. A simple and fast option is Here is an example of how you could use it:
[dependencies]
# ...
fnv = "1.0"
// Add to imports: use fnv::FnvHasher;
let workspace_root_hash = {
let mut hasher = FnvHasher::default();
hasher.write(b"hermes_shadow_salt");
metadata.workspace_root.hash(&mut hasher);
hasher.finish()
}; |
||
|
|
||
| target_dir.join(format!("hermes_shadow_{workspace_root_hash}")) | ||
| } | ||
|
|
||
| /// Resolves which packages to process based on workspace flags and CWD. | ||
| fn resolve_packages<'a>( | ||
| metadata: &'a Metadata, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,11 @@ pub fn create_shadow_skeleton( | |||||||||||||||||||||
| target_dir: &Path, | ||||||||||||||||||||||
| skip_paths: &HashSet<PathBuf>, | ||||||||||||||||||||||
| ) -> Result<()> { | ||||||||||||||||||||||
| if dest_root.exists() { | ||||||||||||||||||||||
| std::fs::remove_dir_all(&dest_root)?; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| std::fs::create_dir_all(&dest_root)?; | ||||||||||||||||||||||
|
Comment on lines
+20
to
+23
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic for cleaning the destination directory has a couple of race conditions and bugs that can cause the tool to fail unexpectedly:
A more robust approach is needed to handle all these filesystem states correctly.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| let walker = WalkDir::new(source_root) | ||||||||||||||||||||||
| .follow_links(false) // Security: don't follow symlinks out of the root. | ||||||||||||||||||||||
| .into_iter(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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.
std::hash::DefaultHasheris not guaranteed to produce the same hash across different Rust versions or platforms. This means that if the Rust toolchain is updated, the generated hash for the workspace path might change. This would result in a newhermes_shadow_*directory being created in yourtargetdirectory, leaving the old one as an orphaned artifact.While this is not a correctness issue for a single run (as the shadow directory is cleaned), it can lead to clutter in the build output directory over time. If a stable path is desirable, consider using a stable hashing algorithm. You could use a hasher from a crate like
seahashorahashwith a fixed seed, or a cryptographic hash like SHA-2.