Skip to content

Commit f83cd4f

Browse files
Retry watcher initialization and watch invalid dependencies
Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 3377441 commit f83cd4f

6 files changed

Lines changed: 329 additions & 115 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#### :bug: Bug fix
2525

26-
- Keep watch mode running after full rebuild initialization fails so it can recover after a later valid change. https://github.com/rescript-lang/rescript/pull/8641
26+
- Keep watch mode running after full rebuild initialization fails, retry initialization on source edits, and watch newly added dependencies for configuration fixes. https://github.com/rescript-lang/rescript/pull/8641
2727
- Fix compiler crashes when wrapping values in `Some` or passing optional arguments whose types come from a transitive dependency. https://github.com/rescript-lang/rescript/pull/8719
2828
- Fix unused-pattern warnings for variant spreads reported without a location. https://github.com/rescript-lang/rescript/pull/8744
2929
- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. https://github.com/rescript-lang/rescript/pull/8741

‎rewatch/src/build/packages.rs‎

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use crate::helpers::StrippedVerbatimPath;
88
use crate::helpers::emojis::*;
99
use crate::project_context::{MonoRepoContext, ProjectContext};
1010
use ahash::{AHashMap, AHashSet};
11-
use anyhow::{Result, anyhow};
11+
use anyhow::{Context, Result, anyhow};
1212
use console::style;
1313
use log::debug;
1414
use rayon::prelude::*;
1515
use std::collections::hash_map::Entry;
1616
use std::error;
17+
use std::fmt;
1718
use std::fs::{self};
1819
use std::hash::{Hash, Hasher};
1920
use std::path::{Path, PathBuf};
@@ -282,6 +283,30 @@ pub fn read_dependency(
282283
Ok(canonical_path)
283284
}
284285

286+
/// Retain the resolved directory even when its config cannot be read, so watch mode can
287+
/// observe a correction to a dependency that was absent from the previous build state.
288+
#[derive(Debug)]
289+
pub struct DependencyConfigError {
290+
pub package_path: PathBuf,
291+
source: anyhow::Error,
292+
}
293+
294+
impl fmt::Display for DependencyConfigError {
295+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296+
write!(
297+
f,
298+
"Could not read dependency config at {}",
299+
self.package_path.display()
300+
)
301+
}
302+
}
303+
304+
impl error::Error for DependencyConfigError {
305+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
306+
Some(self.source.as_ref())
307+
}
308+
}
309+
285310
/// Given a config, recursively finds all dependencies.
286311
/// 1. It starts with registering dependencies and
287312
/// prevents the operation for the ones which are already
@@ -296,7 +321,7 @@ fn read_dependencies(
296321
show_progress: bool,
297322
is_local_dep: bool,
298323
prod: bool,
299-
) -> Vec<Dependency> {
324+
) -> Result<Vec<Dependency>> {
300325
let mut dependencies: Vec<String> = package_config.get_dependency_names();
301326

302327
// Concatenate dev dependencies if is_local_dep is true and not in prod mode
@@ -311,17 +336,14 @@ fn read_dependencies(
311336
// Package already registered - check for duplicate (different path)
312337
// Re-resolve from current package and from root to compare paths
313338
if let Ok(current_path) = read_dependency(package_name, package_config, project_context)
314-
&& let Ok(chosen_path) = read_dependency(package_name, &project_context.current_config, project_context)
339+
&& let Ok(chosen_path) =
340+
read_dependency(package_name, &project_context.current_config, project_context)
315341
&& current_path != chosen_path
316342
{
317343
// Different paths - this is a duplicate
318344
let root_path = project_context.get_root_path();
319-
let chosen_relative = chosen_path
320-
.strip_prefix(root_path)
321-
.unwrap_or(&chosen_path);
322-
let duplicate_relative = current_path
323-
.strip_prefix(root_path)
324-
.unwrap_or(&current_path);
345+
let chosen_relative = chosen_path.strip_prefix(root_path).unwrap_or(&chosen_path);
346+
let duplicate_relative = current_path.strip_prefix(root_path).unwrap_or(&current_path);
325347
let current_package_path = package_config
326348
.path
327349
.parent()
@@ -348,39 +370,22 @@ fn read_dependencies(
348370
.collect::<Vec<String>>()
349371
// Read all config files in parallel instead of blocking
350372
.par_iter()
351-
.map(|package_name| {
352-
let (config, canonical_path) =
353-
match read_dependency(package_name, package_config, project_context) {
354-
Err(error) => {
355-
if show_progress {
356-
println!(
357-
"{} {} Error building package tree. {}",
358-
style("[1/2]").bold().dim(),
359-
CROSS,
360-
error
361-
);
362-
}
363-
364-
let parent_path_str = project_context.get_root_path().to_string_lossy();
365-
log::error!(
366-
"Could not build package tree reading dependency '{package_name}' at path '{parent_path_str}'. Error: {error}",
373+
.map(|package_name| -> Result<Dependency> {
374+
let canonical_path =
375+
read_dependency(package_name, package_config, project_context).inspect_err(|error| {
376+
if show_progress {
377+
println!(
378+
"{} {} Error building package tree. {}",
379+
style("[1/2]").bold().dim(),
380+
CROSS,
381+
error
367382
);
368-
369-
std::process::exit(2)
370-
}
371-
Ok(canonical_path) => {
372-
match read_config(&canonical_path) {
373-
Ok(config) => (config, canonical_path),
374-
Err(error) => {
375-
let parent_path_str = project_context.get_root_path().to_string_lossy();
376-
log::error!(
377-
"Could not build package tree for '{package_name}' at path '{parent_path_str}'. Error: {error}",
378-
);
379-
std::process::exit(2)
380-
}
381-
}
382383
}
383-
};
384+
})?;
385+
let config = read_config(&canonical_path).map_err(|source| DependencyConfigError {
386+
package_path: canonical_path.clone(),
387+
source,
388+
})?;
384389

385390
let is_local_dep = is_local_dependency(project_context, package_name, &canonical_path);
386391

@@ -391,15 +396,16 @@ fn read_dependencies(
391396
show_progress,
392397
is_local_dep,
393398
prod,
394-
);
399+
)
400+
.with_context(|| format!("Could not read dependencies of '{package_name}'"))?;
395401

396-
Dependency {
402+
Ok(Dependency {
397403
name: package_name.to_owned(),
398404
config,
399405
path: canonical_path,
400406
dependencies,
401407
is_local_dep,
402-
}
408+
})
403409
})
404410
.collect()
405411
}
@@ -585,7 +591,7 @@ fn read_packages(
585591
show_progress,
586592
/* is local dep */ true,
587593
prod,
588-
));
594+
)?);
589595

590596
for d in dependencies.iter() {
591597
if !map.contains_key(&d.name) {

0 commit comments

Comments
 (0)