Skip to content

Compile dev deps #159

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

Merged
merged 6 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn initialize_build(
show_progress: bool,
path: &str,
bsc_path: Option<String>,
build_dev_deps: bool,
) -> Result<BuildState> {
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
Expand All @@ -150,7 +151,13 @@ pub fn initialize_build(
}

let timing_package_tree = Instant::now();
let packages = packages::make(filter, &project_root, &workspace_root, show_progress)?;
let packages = packages::make(
filter,
&project_root,
&workspace_root,
show_progress,
build_dev_deps,
)?;
let timing_package_tree_elapsed = timing_package_tree.elapsed();

if show_progress {
Expand Down Expand Up @@ -476,8 +483,15 @@ pub fn build(
None
};
let timing_total = Instant::now();
let mut build_state = initialize_build(default_timing, filter, show_progress, path, bsc_path)
.map_err(|e| anyhow!("Could not initialize build. Error: {e}"))?;
let mut build_state = initialize_build(
default_timing,
filter,
show_progress,
path,
bsc_path,
build_dev_deps,
)
.map_err(|e| anyhow!("Could not initialize build. Error: {e}"))?;

match incremental_build(
&mut build_state,
Expand Down
9 changes: 8 additions & 1 deletion src/build/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,14 @@ pub fn cleanup_after_build(build_state: &BuildState) {
pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Result<()> {
let project_root = helpers::get_abs_path(path);
let workspace_root = helpers::get_workspace_root(&project_root);
let packages = packages::make(&None, &project_root, &workspace_root, show_progress)?;
let packages = packages::make(
&None,
&project_root,
&workspace_root,
show_progress,
// Always clean dev dependencies
true,
)?;
let root_config_name = packages::read_package_name(&project_root)?;
let bsc_path = match bsc_path {
Some(bsc_path) => bsc_path,
Expand Down
6 changes: 3 additions & 3 deletions src/build/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn compile(
"cmi",
);

let cmi_digest = helpers::compute_file_hash(&Path::new(&cmi_path));
let cmi_digest = helpers::compute_file_hash(Path::new(&cmi_path));

let package = build_state
.get_package(&module.package_name)
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn compile(
&build_state.workspace_root,
build_dev_deps,
);
let cmi_digest_after = helpers::compute_file_hash(&Path::new(&cmi_path));
let cmi_digest_after = helpers::compute_file_hash(Path::new(&cmi_path));

// we want to compare both the hash of interface and the implementation
// compile assets to verify that nothing changed. We also need to checke the interface
Expand Down Expand Up @@ -326,7 +326,7 @@ pub fn compile(
if files_total_count == compile_universe_count {
break;
}
if in_progress_modules.len() == 0 || in_progress_modules.eq(&current_in_progres_modules) {
if in_progress_modules.is_empty() || in_progress_modules.eq(&current_in_progres_modules) {
// find the dependency cycle
let cycle = dependency_cycle::find(
&compile_universe
Expand Down
24 changes: 14 additions & 10 deletions src/build/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ pub fn get_source_files(
package_dir: &Path,
filter: &Option<regex::Regex>,
source: &config::PackageSource,
build_dev_deps: bool,
) -> AHashMap<String, SourceFileMeta> {
let mut map: AHashMap<String, SourceFileMeta> = AHashMap::new();

Expand All @@ -493,16 +494,9 @@ pub fn get_source_files(
};

let path_dir = Path::new(&source.dir);
// don't include dev sources for now
if type_ != &Some("dev".to_string()) {
if (build_dev_deps && type_ == &Some("dev".to_string())) || type_ != &Some("dev".to_string()) {
match read_folders(filter, package_dir, path_dir, recurse) {
Ok(files) => map.extend(files),
// Err(_e) if type_ == &Some("dev".to_string()) => {
// log::warn!(
// "Could not read folder: {}... Probably ok as type is dev",
// path_dir.to_string_lossy()
// )
// }
Err(_e) => log::error!(
"Could not read folder: {:?}. Specified in dependency: {}, located {:?}...",
path_dir.to_path_buf().into_os_string(),
Expand All @@ -520,13 +514,22 @@ pub fn get_source_files(
fn extend_with_children(
filter: &Option<regex::Regex>,
mut build: AHashMap<String, Package>,
build_dev_deps: bool,
) -> AHashMap<String, Package> {
for (_key, package) in build.iter_mut() {
let mut map: AHashMap<String, SourceFileMeta> = AHashMap::new();
package
.source_folders
.par_iter()
.map(|source| get_source_files(&package.name, Path::new(&package.path), filter, source))
.map(|source| {
get_source_files(
&package.name,
Path::new(&package.path),
filter,
source,
build_dev_deps,
)
})
.collect::<Vec<AHashMap<String, SourceFileMeta>>>()
.into_iter()
.for_each(|source| map.extend(source));
Expand Down Expand Up @@ -568,12 +571,13 @@ pub fn make(
root_folder: &str,
workspace_root: &Option<String>,
show_progress: bool,
build_dev_deps: bool,
) -> Result<AHashMap<String, Package>> {
let map = read_packages(root_folder, workspace_root.to_owned(), show_progress)?;

/* Once we have the deduplicated packages, we can add the source files for each - to minimize
* the IO */
let result = extend_with_children(filter, map);
let result = extend_with_children(filter, map, build_dev_deps);

Ok(result)
}
Expand Down
34 changes: 34 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,40 @@ mod tests {
}
}

#[test]
fn test_dev_sources_multiple() {
let json = r#"
{
"name": "@rescript/core",
"version": "0.5.0",
"sources": [
{ "dir": "src" },
{ "dir": "test", "type": "dev" }
],
"package-specs": {
"module": "esmodule",
"in-source": true
},
"bs-dev-dependencies": ["@rescript/tools"],
"suffix": ".mjs",
"warnings": {
"error": "+101"
}
}
"#;

let config = serde_json::from_str::<Config>(json).unwrap();
if let Some(OneOrMore::Multiple(sources)) = config.sources {
let src_dir = sources[0].to_qualified_without_children(None);
let test_dir = sources[1].to_qualified_without_children(None);

assert_eq!(test_dir.type_, Some(String::from("dev")));
} else {
dbg!(config.sources);
unreachable!()
}
}

#[test]
fn test_detect_gentypeconfig() {
let json = r#"
Expand Down
9 changes: 5 additions & 4 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ async fn async_watch(
create_sourcedirs: bool,
build_dev_deps: bool,
) -> notify::Result<()> {
let mut build_state =
build::initialize_build(None, filter, show_progress, path, None).expect("Can't initialize build");
let mut build_state = build::initialize_build(None, filter, show_progress, path, None, build_dev_deps)
.expect("Can't initialize build");
let mut needs_compile_type = CompileType::Incremental;
// create a mutex to capture if ctrl-c was pressed
let ctrlc_pressed = Arc::new(Mutex::new(false));
Expand Down Expand Up @@ -214,8 +214,9 @@ async fn async_watch(
}
CompileType::Full => {
let timing_total = Instant::now();
build_state = build::initialize_build(None, filter, show_progress, path, None)
.expect("Can't initialize build");
build_state =
build::initialize_build(None, filter, show_progress, path, None, build_dev_deps)
.expect("Can't initialize build");
let _ = build::incremental_build(
&mut build_state,
None,
Expand Down
7 changes: 4 additions & 3 deletions testrepo/bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
"@testrepo/dep01",
"@testrepo/dep02",
"@testrepo/new-namespace",
"@testrepo/namespace-casing"

"@testrepo/namespace-casing",
"@testrepo/with-dev-deps"
],
"bs-dependencies": [
"@testrepo/main",
"@testrepo/dep01",
"@testrepo/dep02",
"@testrepo/new-namespace",
"@testrepo/namespace-casing"
"@testrepo/namespace-casing",
"@testrepo/with-dev-deps"
],
"reason": {
"react-jsx": 3
Expand Down
3 changes: 2 additions & 1 deletion testrepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"packages/dep01",
"packages/dep02",
"packages/new-namespace",
"packages/namespace-casing"
"packages/namespace-casing",
"packages/with-dev-deps"
]
},
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions testrepo/packages/with-dev-deps/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@testrepo/with-dev-deps",
"version": "0.0.1",
"keywords": [
"rescript"
],
"author": "",
"license": "MIT",
"dependencies": {
"rescript": "*"
}
}
17 changes: 17 additions & 0 deletions testrepo/packages/with-dev-deps/rescript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@testrepo/with-dev-deps",
"sources": [
{
"dir": "src"
},
{
"dir": "test",
"type": "dev"
}
],
"package-specs": {
"module": "es6",
"in-source": true
},
"suffix": ".res.js"
}
11 changes: 11 additions & 0 deletions testrepo/packages/with-dev-deps/src/FileToTest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function add(a, b) {
return a + b | 0;
}

export {
add ,
}
/* No side effect */
1 change: 1 addition & 0 deletions testrepo/packages/with-dev-deps/src/FileToTest.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let add = (a, b) => a + b
6 changes: 6 additions & 0 deletions testrepo/packages/with-dev-deps/test/FileToTest_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let res = FileToTest.add(1, 2)
let expected = 3

if res !== expected {
failwith("Expected " ++ expected->Js.Int.toString ++ ", got " ++ res->Js.Int.toString)
}
2 changes: 1 addition & 1 deletion testrepo/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

rescript@*:
version "11.0.0"
resolved "https://registry.npmjs.org/rescript/-/rescript-11.0.0.tgz"
resolved "https://registry.yarnpkg.com/rescript/-/rescript-11.0.0.tgz#9a0b6fc998c360543c459aba49b77a572a0306cd"
integrity sha512-uIUwDZZmDUb7ymGkBiiGioxMg8hXh1mze/2k/qhYQcZGgi7PrLHQIW9AksM7gb9WnpjCAvFsA8U2VgC0nA468w==
22 changes: 22 additions & 0 deletions tests/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ rewatch build --no-timing=true &> ../tests/snapshots/dependency-cycle.txt
git checkout -- packages/new-namespace/src/NS_alias.res
rewatch build &> /dev/null

# it should compile dev dependencies with the --dev flag
rewatch build --dev &> /dev/null
file_count=$(find ./packages/with-dev-deps -name *.mjs | wc -l)
if [ "$file_count" -eq 2 ];
then
success "Compiled dev dependencies successfully"
else
error "Expected 2 files to be compiled with the --dev flag, found $file_count"
exit 1
fi

rewatch clean &> /dev/null
file_count=$(find ./packages/with-dev-deps -name *.mjs | wc -l)
if [ "$file_count" -eq 0 ];
then
success "Cleaned dev dependencies successfully"
else
error "Expected 0 files remaining after cleaning, found $file_count"
exit 1
fi


# it should not loop (we had an infinite loop when clean building with a cycle)
rewatch clean &> /dev/null
echo 'Dep01.log()' >> packages/new-namespace/src/NS_alias.res
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/dependency-cycle.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[1/7] 📦 Building package tree...[1/7] 📦 Built package tree in 0.00s
[2/7] 👀 Finding source files...[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 0/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 0/14 0.00s
[5/7] 🧱 Parsed 1 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] ❌ Compiled 0 modules in 0.00s
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/remove-file.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[1/7] 📦 Building package tree...[1/7] 📦 Built package tree in 0.00s
[2/7] 👀 Finding source files...[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 1/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 1/14 0.00s
[5/7] 🧱 Parsed 0 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] ❌ Compiled 1 modules in 0.00s
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-file-internal-dep-namespace.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[1/7] 📦 Building package tree...[1/7] 📦 Built package tree in 0.00s
[2/7] 👀 Finding source files...[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 2/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 2/14 0.00s
[5/7] 🧱 Parsed 2 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] ❌ Compiled 3 modules in 0.00s
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-file-internal-dep.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[1/7] 📦 Building package tree...[1/7] 📦 Built package tree in 0.00s
[2/7] 👀 Finding source files...[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 2/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 2/14 0.00s
[5/7] 🧱 Parsed 2 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] ❌ Compiled 2 modules in 0.00s
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-file-with-interface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
 No implementation file found for interface file (skipping): src/ModuleWithInterface.resi
[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 2/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 2/14 0.00s
[5/7] 🧱 Parsed 1 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] 🤺 Compiled 2 modules in 0.00s
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-file.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[1/7] 📦 Building package tree...[1/7] 📦 Built package tree in 0.00s
[2/7] 👀 Finding source files...[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 1/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 1/14 0.00s
[5/7] 🧱 Parsed 1 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] 🤺 Compiled 1 modules in 0.00s
Expand Down
2 changes: 1 addition & 1 deletion tests/snapshots/rename-interface-file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
 No implementation file found for interface file (skipping): src/ModuleWithInterface2.resi
[2/7] 👀 Found source files in 0.00s
[3/7] 📝 Reading compile state...[3/7] 📝 Read compile state 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 1/13 0.00s
[4/7] 🧹 Cleaning up previous build...[4/7] 🧹 Cleaned 1/14 0.00s
[5/7] 🧱 Parsed 1 source files in 0.00s
[6/7] 🌴 Collected deps in 0.00s
[7/7] 🤺 Compiled 2 modules in 0.00s
Expand Down
Loading