Skip to content

Commit bdf6aaf

Browse files
committed
fix
1 parent dec4564 commit bdf6aaf

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
]
@@ -609,15 +613,37 @@ fn compile_file(
609613
// we need to copy the source file to the build directory.
610614
// editor tools expects the source file in lib/bs for finding the current package
611615
// and in lib/ocaml when referencing modules in other packages
612-
let _ = std::fs::copy(
613-
std::path::Path::new(&package.path).join(path),
614-
std::path::Path::new(&package.get_build_path()).join(path),
615-
)
616-
.expect("copying source file failed");
616+
617+
// update: we now generate the file in lib/bs/... and then install it in the right
618+
// in-source location if the hash is different
619+
620+
// the in-source file. This is the currently "installed" file
621+
let in_source_hash =
622+
helpers::compute_file_hash(&std::path::Path::new(&package.path).join(path));
623+
624+
// this is the file that we just generated
625+
let generated_hash = helpers::compute_file_hash(
626+
&std::path::Path::new(&package.get_build_path()).join(path),
627+
);
628+
629+
match (in_source_hash, generated_hash) {
630+
(Some(in_source_hash), Some(generated_hash)) if in_source_hash == generated_hash => {
631+
// do nothing, the hashes are the same!
632+
()
633+
}
634+
_ => {
635+
// copy the file to the in-source location
636+
let _ = std::fs::copy(
637+
std::path::Path::new(&package.get_build_path()).join(path),
638+
std::path::Path::new(&package.path).join(path),
639+
)
640+
.expect("copying source file failed");
641+
}
642+
}
617643

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

src/build/parse.rs

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

217217
let suffix = package
218218
.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)