Skip to content

Commit 4e6989c

Browse files
committed
fix
1 parent 848dabc commit 4e6989c

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

src/build/compile.rs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub fn compile(
148148
"cmi",
149149
);
150150

151-
let cmi_digest = helpers::compute_file_hash(&cmi_path);
151+
let cmi_digest = helpers::compute_file_hash(&Path::new(&cmi_path));
152152

153153
let package = build_state
154154
.get_package(&module.package_name)
@@ -189,7 +189,7 @@ pub fn compile(
189189
&build_state.workspace_root,
190190
build_dev_deps,
191191
);
192-
let cmi_digest_after = helpers::compute_file_hash(&cmi_path);
192+
let cmi_digest_after = helpers::compute_file_hash(&Path::new(&cmi_path));
193193

194194
// we want to compare both the hash of interface and the implementation
195195
// compile assets to verify that nothing changed. We also need to checke the interface
@@ -466,7 +466,11 @@ pub fn compiler_args(
466466
format!(
467467
"{}:{}:{}",
468468
root_config.get_module(),
469-
Path::new(file_path).parent().unwrap().to_str().unwrap(),
469+
// compile into lib/bs and later install in the right place
470+
Path::new("lib/bs")
471+
.join(Path::new(file_path).parent().unwrap())
472+
.to_str()
473+
.unwrap(),
470474
root_config.get_suffix()
471475
),
472476
]
@@ -610,15 +614,37 @@ fn compile_file(
610614
// we need to copy the source file to the build directory.
611615
// editor tools expects the source file in lib/bs for finding the current package
612616
// and in lib/ocaml when referencing modules in other packages
613-
let _ = std::fs::copy(
614-
std::path::Path::new(&package.path).join(path),
615-
std::path::Path::new(&package.get_build_path()).join(path),
616-
)
617-
.expect("copying source file failed");
617+
618+
// update: we now generate the file in lib/bs/... and then install it in the right
619+
// in-source location if the hash is different
620+
621+
// the in-source file. This is the currently "installed" file
622+
let in_source_hash =
623+
helpers::compute_file_hash(&std::path::Path::new(&package.path).join(path));
624+
625+
// this is the file that we just generated
626+
let generated_hash = helpers::compute_file_hash(
627+
&std::path::Path::new(&package.get_build_path()).join(path),
628+
);
629+
630+
match (in_source_hash, generated_hash) {
631+
(Some(in_source_hash), Some(generated_hash)) if in_source_hash == generated_hash => {
632+
// do nothing, the hashes are the same!
633+
()
634+
}
635+
_ => {
636+
// copy the file to the in-source location
637+
let _ = std::fs::copy(
638+
std::path::Path::new(&package.get_bs_build_path()).join(path),
639+
std::path::Path::new(&package.path).join(path),
640+
)
641+
.expect("copying source file failed");
642+
}
643+
}
618644

619645
let _ = std::fs::copy(
620646
std::path::Path::new(&package.path).join(path),
621-
std::path::Path::new(&package.get_build_path())
647+
std::path::Path::new(&package.get_ocaml_build_path())
622648
.join(std::path::Path::new(path).file_name().unwrap()),
623649
)
624650
.expect("copying source file failed");

src/build/parse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ pub fn generate_asts(
205205
// probably better to do this in a different function
206206
// specific to compiling mlmaps
207207
let compile_path = package.get_mlmap_compile_path();
208-
let mlmap_hash = helpers::compute_file_hash(&compile_path);
208+
let mlmap_hash = helpers::compute_file_hash(&Path::new(&compile_path));
209209
namespaces::compile_mlmap(package, module_name, &build_state.bsc_path);
210-
let mlmap_hash_after = helpers::compute_file_hash(&compile_path);
210+
let mlmap_hash_after = helpers::compute_file_hash(&Path::new(&compile_path));
211211

212212
let suffix = package
213213
.namespace

src/helpers.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,20 @@ pub fn format_namespaced_module_name(module_name: &str) -> String {
308308
}
309309
}
310310

311-
pub fn compute_file_hash(path: &str) -> Option<blake3::Hash> {
311+
pub fn compute_file_hash(path: &Path) -> Option<blake3::Hash> {
312312
match fs::read(path) {
313313
Ok(str) => Some(blake3::hash(&str)),
314314
Err(_) => None,
315315
}
316316
}
317317

318+
pub fn get_source_file_from_rescript_file(path: &Path, suffix: &str) -> PathBuf {
319+
path.with_extension(
320+
// suffix.to_string includes the ., so we need to remove it
321+
&suffix.to_string()[1..],
322+
)
323+
}
324+
318325
fn has_rescript_config(path: &Path) -> bool {
319326
path.join("bsconfig.json").exists() || path.join("rescript.json").exists()
320327
}

0 commit comments

Comments
 (0)