Skip to content

Commit 789591a

Browse files
authored
Fix sourcedirs.json generation (#7671)
* Catch potential symlinks * Generate sourcedirs.json * Revert build * Only write sourcedirs for local packages with files. * Revert build changes * Verify package_path is part of project_root * Updat comment * Add changelog entry * Rename in CI * Revert "Rename in CI" This reverts commit 170c1a6.
1 parent 1fa4d26 commit 789591a

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
- Add rust linting to CI with `clippy`. https://github.com/rescript-lang/rescript/pull/7675
3030

31+
#### :bug: Bug fix
32+
33+
- Fix `--create-sourcedirs` generation with for a single project. https://github.com/rescript-lang/rescript/pull/7671
34+
3135
# 12.0.0-beta.2
3236

3337
#### :boom: Breaking Change

rewatch/src/build/packages.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ fn read_dependencies(
331331
let (config, canonical_path) =
332332
match read_dependency(package_name, parent_path, project_root, workspace_root) {
333333
Err(error) => {
334-
if show_progress {
335-
println!(
336-
"{} {} Error building package tree. {}",
337-
style("[1/2]").bold().dim(),
338-
CROSS,
339-
error
340-
);
341-
}
334+
if show_progress {
335+
println!(
336+
"{} {} Error building package tree. {}",
337+
style("[1/2]").bold().dim(),
338+
CROSS,
339+
error
340+
);
341+
}
342342

343343
let parent_path_str = parent_path.to_string_lossy();
344344
log::error!(
@@ -356,9 +356,9 @@ fn read_dependencies(
356356
"We could not build package tree '{package_name}', at path '{parent_path_str}'. Error: {error}",
357357
);
358358
std::process::exit(2)
359-
}
360-
}
359+
}
361360
}
361+
}
362362
};
363363

364364
let is_pinned = parent_config
@@ -374,7 +374,7 @@ fn read_dependencies(
374374
project_root,
375375
workspace_root,
376376
show_progress,
377-
build_dev_deps
377+
build_dev_deps,
378378
);
379379

380380
Dependency {
@@ -413,7 +413,13 @@ pub fn read_package_name(package_dir: &Path) -> Result<String> {
413413
.ok_or_else(|| anyhow!("No name field found in package.json"))
414414
}
415415

416-
fn make_package(config: config::Config, package_path: &Path, is_pinned_dep: bool, is_root: bool) -> Package {
416+
fn make_package(
417+
config: config::Config,
418+
package_path: &Path,
419+
is_pinned_dep: bool,
420+
is_root: bool,
421+
project_root: &Path,
422+
) -> Package {
417423
let source_folders = match config.sources.to_owned() {
418424
Some(config::OneOrMore::Single(source)) => get_source_dirs(source, None),
419425
Some(config::OneOrMore::Multiple(sources)) => {
@@ -452,6 +458,11 @@ This inconsistency will cause issues with package resolution.\n",
452458
);
453459
}
454460

461+
let is_local_dep = {
462+
package_path.starts_with(project_root)
463+
&& !package_path.components().any(|c| c.as_os_str() == "node_modules")
464+
};
465+
455466
Package {
456467
name: package_name,
457468
config: config.to_owned(),
@@ -466,7 +477,7 @@ This inconsistency will cause issues with package resolution.\n",
466477
.expect("Could not canonicalize"),
467478
dirs: None,
468479
is_pinned_dep,
469-
is_local_dep: !package_path.components().any(|c| c.as_os_str() == "node_modules"),
480+
is_local_dep,
470481
is_root,
471482
}
472483
}
@@ -481,7 +492,7 @@ fn read_packages(
481492

482493
// Store all packages and completely deduplicate them
483494
let mut map: AHashMap<String, Package> = AHashMap::new();
484-
let root_package = make_package(root_config.to_owned(), project_root, false, true);
495+
let root_package = make_package(root_config.to_owned(), project_root, false, true, project_root);
485496
map.insert(root_package.name.to_string(), root_package);
486497

487498
let mut registered_dependencies_set: AHashSet<String> = AHashSet::new();
@@ -496,7 +507,7 @@ fn read_packages(
496507
));
497508
dependencies.iter().for_each(|d| {
498509
if !map.contains_key(&d.name) {
499-
let package = make_package(d.config.to_owned(), &d.path, d.is_pinned, false);
510+
let package = make_package(d.config.to_owned(), &d.path, d.is_pinned, false, project_root);
500511
map.insert(d.name.to_string(), package);
501512
}
502513
});

rewatch/src/sourcedirs.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ pub struct SourceDirs<'a> {
2121
}
2222

2323
fn package_to_dirs(package: &Package, root_package_path: &Path) -> AHashSet<Dir> {
24-
let relative_path = package.path.strip_prefix(root_package_path).unwrap();
25-
26-
package
27-
.dirs
28-
.as_ref()
29-
.unwrap_or(&AHashSet::new())
30-
.iter()
31-
.map(|path| relative_path.join(path))
32-
.collect::<AHashSet<PathBuf>>()
24+
match package.path.strip_prefix(root_package_path) {
25+
Err(_) => AHashSet::new(),
26+
Ok(relative_path) => package
27+
.dirs
28+
.as_ref()
29+
.unwrap_or(&AHashSet::new())
30+
.iter()
31+
.map(|path| relative_path.join(path))
32+
.collect::<AHashSet<PathBuf>>(),
33+
}
3334
}
3435

3536
fn deps_to_pkgs<'a>(
@@ -61,11 +62,13 @@ pub fn print(buildstate: &BuildState) {
6162
.find(|(_name, package)| package.is_root)
6263
.expect("Could not find root package");
6364

64-
// Take all packages apart from the root package
65+
// Take all local packages with source files.
66+
// In the case of a monorepo, the root package typically won't have any source files.
67+
// But in the case of a single package, it will be both local, root and have source files.
6568
let (dirs, pkgs): (Vec<AHashSet<Dir>>, Vec<AHashMap<PackageName, AbsolutePath>>) = buildstate
6669
.packages
6770
.par_iter()
68-
.filter(|(_name, package)| !package.is_root)
71+
.filter(|(_name, package)| package.is_local_dep && package.source_files.is_some())
6972
.map(|(_name, package)| {
7073
// Extract Directories
7174
let dirs = package_to_dirs(package, &root_package.path);

0 commit comments

Comments
 (0)