Skip to content

Commit 1f72412

Browse files
committed
[hermes] Resolve Cargo and Hermes target dirs
gherrit-pr-id: G48b27aa33306f1b4bae6f7996a68c1a1869d0a9a
1 parent c475f79 commit 1f72412

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

tools/hermes/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ fn main() {
5858
}
5959

6060
// TODO: Create shadow skeleton.
61-
// shadow::create_shadow_skeleton(&roots.workspace, todo!(), todo!(), todo!()).unwrap();
61+
// shadow::create_shadow_skeleton(&roots.workspace, &roots.shadow_root, &roots.cargo_target_dir, todo!()).unwrap();
6262
}

tools/hermes/src/resolve.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use std::{env, path::PathBuf};
1+
use std::{
2+
env,
3+
hash::{Hash as _, Hasher as _},
4+
path::PathBuf,
5+
};
26

37
use anyhow::{anyhow, Context, Result};
4-
use cargo_metadata::{
5-
Metadata, MetadataCommand, Package, PackageName, Target, TargetKind,
6-
};
8+
use cargo_metadata::{Metadata, MetadataCommand, Package, PackageName, Target, TargetKind};
79
use clap::Parser;
810

911
#[derive(Parser, Debug)]
@@ -98,6 +100,8 @@ impl TryFrom<&TargetKind> for HermesTargetKind {
98100

99101
pub struct Roots {
100102
pub workspace: PathBuf,
103+
pub cargo_target_dir: PathBuf,
104+
pub shadow_root: PathBuf,
101105
pub roots: Vec<(PackageName, HermesTargetKind, PathBuf)>,
102106
}
103107

@@ -120,8 +124,12 @@ pub fn resolve_roots(args: &Args) -> Result<Roots> {
120124

121125
let selected_packages = resolve_packages(&metadata, &args.workspace)?;
122126

123-
let mut roots =
124-
Roots { workspace: metadata.workspace_root.as_std_path().to_owned(), roots: Vec::new() };
127+
let mut roots = Roots {
128+
workspace: metadata.workspace_root.as_std_path().to_owned(),
129+
cargo_target_dir: metadata.target_directory.as_std_path().to_owned(),
130+
shadow_root: resolve_shadow_path(&metadata),
131+
roots: Vec::new(),
132+
};
125133

126134
for package in selected_packages {
127135
log::trace!("Scanning package: {}", package.name);
@@ -145,6 +153,22 @@ pub fn resolve_roots(args: &Args) -> Result<Roots> {
145153
Ok(roots)
146154
}
147155

156+
fn resolve_shadow_path(metadata: &Metadata) -> PathBuf {
157+
// NOTE: Automatically handles `CARGO_TARGET_DIR` env var.
158+
let target_dir = metadata.target_directory.as_std_path();
159+
160+
// Hash the path to the workspace root to avoid collisions between different
161+
// workspaces using the same target directory.
162+
let workspace_root_hash = {
163+
let mut hasher = std::hash::DefaultHasher::new();
164+
hasher.write(b"hermes_shadow_salt");
165+
metadata.workspace_root.hash(&mut hasher);
166+
hasher.finish()
167+
};
168+
169+
target_dir.join(format!("hermes_shadow_{workspace_root_hash}"))
170+
}
171+
148172
/// Resolves which packages to process based on workspace flags and CWD.
149173
fn resolve_packages<'a>(
150174
metadata: &'a Metadata,

tools/hermes/src/shadow.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ pub fn create_shadow_skeleton(
1717
target_dir: &Path,
1818
skip_paths: &HashSet<PathBuf>,
1919
) -> Result<()> {
20+
if dest_root.exists() {
21+
std::fs::remove_dir_all(&dest_root)?;
22+
}
23+
std::fs::create_dir_all(&dest_root)?;
24+
2025
let walker = WalkDir::new(source_root)
2126
.follow_links(false) // Security: don't follow symlinks out of the root.
2227
.into_iter();

0 commit comments

Comments
 (0)