Skip to content

Commit 870a6e8

Browse files
committed
Traverse upwards for single context projects
1 parent 83aa0bc commit 870a6e8

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

rewatch/src/helpers.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,63 @@ pub fn try_package_path(
154154
} else if path_from_root.exists() {
155155
Ok(path_from_root)
156156
} else {
157+
// As a last resort, when we're in a Single project context, traverse upwards
158+
// starting from the parent of the package root (package_config.path.parent().parent())
159+
// and probe each ancestor's node_modules for the dependency. This covers hoisted
160+
// workspace setups when building a package standalone.
161+
if project_context.monorepo_context.is_none() {
162+
match package_config.path.parent().and_then(|p| p.parent()) {
163+
Some(start_dir) => {
164+
return find_dep_in_upward_node_modules(start_dir, package_name);
165+
}
166+
None => {
167+
log::debug!(
168+
"try_package_path: cannot compute start directory for upward traversal from '{}'",
169+
package_config.path.to_string_lossy()
170+
);
171+
}
172+
}
173+
}
174+
157175
Err(anyhow!(
158176
"The package \"{package_name}\" is not found (are node_modules up-to-date?)..."
159177
))
160178
}
161179
}
162180

181+
fn find_dep_in_upward_node_modules(start_dir: &Path, package_name: &str) -> anyhow::Result<PathBuf> {
182+
log::debug!(
183+
"try_package_path: falling back to upward traversal for '{}' starting at '{}'",
184+
package_name,
185+
start_dir.to_string_lossy()
186+
);
187+
188+
let mut current = Some(start_dir);
189+
while let Some(dir) = current {
190+
let candidate = package_path(dir, package_name);
191+
log::debug!("try_package_path: checking '{}'", candidate.to_string_lossy());
192+
if candidate.exists() {
193+
log::debug!(
194+
"try_package_path: found '{}' at '{}' via upward traversal",
195+
package_name,
196+
candidate.to_string_lossy()
197+
);
198+
return Ok(candidate);
199+
}
200+
current = dir.parent();
201+
}
202+
log::debug!(
203+
"try_package_path: no '{}' found during upward traversal from '{}'",
204+
package_name,
205+
start_dir.to_string_lossy()
206+
);
207+
Err(anyhow!(
208+
"try_package_path: upward traversal did not find '{}' starting at '{}'",
209+
package_name,
210+
start_dir.to_string_lossy()
211+
))
212+
}
213+
163214
pub fn get_abs_path(path: &Path) -> PathBuf {
164215
let abs_path_buf = PathBuf::from(path);
165216

0 commit comments

Comments
 (0)