Skip to content

Commit 848dabc

Browse files
authored
Gentype fix (#149)
1 parent 04ffb68 commit 848dabc

File tree

12 files changed

+121
-106
lines changed

12 files changed

+121
-106
lines changed

src/build.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ pub fn get_compiler_args(
7878
};
7979
helpers::get_rescript_version(&bsc_path)
8080
};
81+
8182
// make PathBuf from package root and get the relative path for filename
8283
let relative_filename = PathBuf::from(&filename)
83-
.strip_prefix(PathBuf::from(&package_root).parent().unwrap())
84+
.strip_prefix(PathBuf::from(&package_root))
8485
.unwrap()
8586
.to_string_lossy()
8687
.to_string();
@@ -108,7 +109,7 @@ pub fn get_compiler_args(
108109
let compiler_args = compiler_args(
109110
&rescript_config,
110111
&root_rescript_config,
111-
&ast_path,
112+
&ast_path.to_string_lossy(),
112113
&rescript_version,
113114
&relative_filename,
114115
is_interface,
@@ -451,7 +452,7 @@ pub fn incremental_build(
451452
pub fn write_build_ninja(build_state: &BuildState) {
452453
for package in build_state.packages.values() {
453454
// write empty file:
454-
let mut f = File::create(std::path::Path::new(&package.get_bs_build_path()).join("build.ninja"))
455+
let mut f = File::create(std::path::Path::new(&package.get_build_path()).join("build.ninja"))
455456
.expect("Unable to write file");
456457
f.write_all(b"").expect("unable to write to ninja file");
457458
}

src/build/clean.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,11 @@ pub fn cleanup_previous_build(
158158
.get_mut(module_name)
159159
.expect("Could not find module for ast file");
160160

161-
let compile_dirty = compile_assets_state.cmi_modules.get(module_name);
161+
let compile_dirty = compile_assets_state.cmt_modules.get(module_name);
162+
// if there is a new AST but it has not been compiled yet, we mark the module as compile dirty
163+
// we do this by checking if the cmt file is newer than the AST file. We always compile the
164+
// interface AND implementation. For some reason the CMI file is not always rewritten if it
165+
// doesn't have any changes, that's why we just look at the CMT file.
162166
if let Some(compile_dirty) = compile_dirty {
163167
let last_modified = Some(ast_last_modified);
164168

@@ -356,7 +360,7 @@ pub fn clean(path: &str, show_progress: bool, bsc_path: Option<String>) -> Resul
356360
let path = std::path::Path::new(&path_str);
357361
let _ = std::fs::remove_dir_all(path);
358362

359-
let path_str = package.get_bs_build_path();
363+
let path_str = package.get_ocaml_build_path();
360364
let path = std::path::Path::new(&path_str);
361365
let _ = std::fs::remove_dir_all(path);
362366
});

src/build/compile.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn compile(
162162
let result = compile_file(
163163
package,
164164
root_package,
165-
&package.get_iast_path(&path),
165+
&helpers::get_ast_path(&path).to_string_lossy(),
166166
module,
167167
&build_state.rescript_version,
168168
true,
@@ -179,7 +179,7 @@ pub fn compile(
179179
let result = compile_file(
180180
package,
181181
root_package,
182-
&package.get_ast_path(&source_file.implementation.path),
182+
&helpers::get_ast_path(&source_file.implementation.path).to_string_lossy(),
183183
module,
184184
&build_state.rescript_version,
185185
false,
@@ -379,16 +379,16 @@ pub fn compiler_args(
379379
.par_iter()
380380
.map(|package_name| {
381381
let canonicalized_path = if let Some(packages) = packages {
382-
packages
383-
.get(package_name)
384-
.expect("expect package")
385-
.path
386-
.to_string()
382+
let package = packages.get(package_name).expect("expect package");
383+
package.path.to_string()
387384
} else {
388385
packages::read_dependency(package_name, project_root, project_root, workspace_root)
389386
.expect("cannot find dep")
390387
};
391-
vec!["-I".to_string(), packages::get_build_path(&canonicalized_path)]
388+
vec![
389+
"-I".to_string(),
390+
packages::get_ocaml_build_path(&canonicalized_path),
391+
]
392392
})
393393
.collect::<Vec<Vec<String>>>();
394394

@@ -415,9 +415,6 @@ pub fn compiler_args(
415415
packages::Namespace::NoNamespace => vec![],
416416
};
417417

418-
let jsx_args = root_config.get_jsx_args();
419-
let jsx_module_args = root_config.get_jsx_module_args();
420-
let jsx_mode_args = root_config.get_jsx_mode_args();
421418
let uncurried_args = root_config.get_uncurried_args(version);
422419
let gentype_arg = root_config.get_gentype_arg();
423420

@@ -478,15 +475,12 @@ pub fn compiler_args(
478475
vec![
479476
namespace_args,
480477
read_cmi_args,
481-
vec!["-I".to_string(), ".".to_string()],
478+
vec!["-I".to_string(), "../ocaml".to_string()],
482479
deps.concat(),
483-
gentype_arg,
484-
jsx_args,
485-
jsx_module_args,
486-
jsx_mode_args,
487480
uncurried_args,
488481
bsc_flags.to_owned(),
489482
warning_args,
483+
gentype_arg,
490484
// vec!["-warn-error".to_string(), "A".to_string()],
491485
// ^^ this one fails for bisect-ppx
492486
// this is the default
@@ -497,6 +491,7 @@ pub fn compiler_args(
497491
// "-I".to_string(),
498492
// abs_node_modules_path.to_string() + "/rescript/ocaml",
499493
// ],
494+
vec!["-bs-v".to_string(), format!("{}", version)],
500495
vec![ast_path.to_string()],
501496
]
502497
.concat()
@@ -515,6 +510,7 @@ fn compile_file(
515510
workspace_root: &Option<String>,
516511
build_dev_deps: bool,
517512
) -> Result<Option<String>> {
513+
let ocaml_build_path_abs = package.get_ocaml_build_path();
518514
let build_path_abs = package.get_build_path();
519515
let implementation_file_path = match &module.source_type {
520516
SourceType::SourceFile(ref source_file) => Ok(&source_file.implementation.path),
@@ -539,7 +535,6 @@ fn compile_file(
539535
&Some(packages),
540536
build_dev_deps,
541537
);
542-
543538
let to_mjs = Command::new(bsc_path)
544539
.current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap())
545540
.args(to_mjs_args)
@@ -566,35 +561,41 @@ fn compile_file(
566561
// perhaps we can do this copying somewhere else
567562
if !is_interface {
568563
let _ = std::fs::copy(
569-
build_path_abs.to_string() + "/" + &module_name + ".cmi",
570-
std::path::Path::new(&package.get_bs_build_path())
564+
std::path::Path::new(&package.get_build_path())
571565
.join(dir)
572566
// because editor tooling doesn't support namespace entries yet
573567
// we just remove the @ for now. This makes sure the editor support
574568
// doesn't break
575569
.join(module_name.to_owned() + ".cmi"),
570+
ocaml_build_path_abs.to_string() + "/" + &module_name + ".cmi",
576571
);
577572
let _ = std::fs::copy(
578-
build_path_abs.to_string() + "/" + &module_name + ".cmj",
579-
std::path::Path::new(&package.get_bs_build_path())
573+
std::path::Path::new(&package.get_build_path())
580574
.join(dir)
581575
.join(module_name.to_owned() + ".cmj"),
576+
ocaml_build_path_abs.to_string() + "/" + &module_name + ".cmj",
582577
);
583578
let _ = std::fs::copy(
584-
build_path_abs.to_string() + "/" + &module_name + ".cmt",
585-
std::path::Path::new(&package.get_bs_build_path())
579+
std::path::Path::new(&package.get_build_path())
586580
.join(dir)
587581
// because editor tooling doesn't support namespace entries yet
588582
// we just remove the @ for now. This makes sure the editor support
589583
// doesn't break
590584
.join(module_name.to_owned() + ".cmt"),
585+
ocaml_build_path_abs.to_string() + "/" + &module_name + ".cmt",
591586
);
592587
} else {
593588
let _ = std::fs::copy(
594-
build_path_abs.to_string() + "/" + &module_name + ".cmti",
595-
std::path::Path::new(&package.get_bs_build_path())
589+
std::path::Path::new(&package.get_build_path())
596590
.join(dir)
597591
.join(module_name.to_owned() + ".cmti"),
592+
ocaml_build_path_abs.to_string() + "/" + &module_name + ".cmti",
593+
);
594+
let _ = std::fs::copy(
595+
std::path::Path::new(&package.get_build_path())
596+
.join(dir)
597+
.join(module_name.to_owned() + ".cmi"),
598+
ocaml_build_path_abs.to_string() + "/" + &module_name + ".cmi",
598599
);
599600
}
600601
match &module.source_type {
@@ -611,7 +612,7 @@ fn compile_file(
611612
// and in lib/ocaml when referencing modules in other packages
612613
let _ = std::fs::copy(
613614
std::path::Path::new(&package.path).join(path),
614-
std::path::Path::new(&package.get_bs_build_path()).join(path),
615+
std::path::Path::new(&package.get_build_path()).join(path),
615616
)
616617
.expect("copying source file failed");
617618

@@ -672,7 +673,7 @@ pub fn mark_modules_with_expired_deps_dirty(build_state: &mut BuildState) {
672673
let dependent_module = build_state.modules.get(dependent).unwrap();
673674
match dependent_module.source_type {
674675
SourceType::SourceFile(_) => {
675-
match (module.last_compiled_cmt, module.last_compiled_cmi) {
676+
match (module.last_compiled_cmt, module.last_compiled_cmt) {
676677
(None, None) | (Some(_), None) | (None, Some(_)) => {
677678
// println!(
678679
// "🛑 {} is a dependent of {} but has no cmt/cmi",
@@ -686,7 +687,7 @@ pub fn mark_modules_with_expired_deps_dirty(build_state: &mut BuildState) {
686687
// we compare the last compiled time of the dependent module with the last
687688
// compile of the interface of the module it depends on, if the interface
688689
// didn't change it doesn't matter
689-
match (dependent_module.last_compiled_cmt, module.last_compiled_cmi) {
690+
match (dependent_module.last_compiled_cmt, module.last_compiled_cmt) {
690691
(Some(last_compiled_dependent), Some(last_compiled)) => {
691692
if last_compiled_dependent < last_compiled {
692693
// println!(
@@ -719,7 +720,7 @@ pub fn mark_modules_with_expired_deps_dirty(build_state: &mut BuildState) {
719720
let dependent_module = build_state.modules.get(dependent_of_namespace).unwrap();
720721

721722
if let (Some(last_compiled_dependent), Some(last_compiled)) =
722-
(dependent_module.last_compiled_cmt, module.last_compiled_cmi)
723+
(dependent_module.last_compiled_cmt, module.last_compiled_cmt)
723724
{
724725
if last_compiled_dependent < last_compiled {
725726
modules_with_expired_deps.insert(dependent.to_string());

src/build/deps.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ fn get_dep_modules(
99
namespace: Option<String>,
1010
package_modules: &AHashSet<String>,
1111
valid_modules: &AHashSet<String>,
12+
package: &packages::Package,
1213
) -> AHashSet<String> {
1314
let mut deps = AHashSet::new();
15+
let ast_file = package.get_build_path() + "/" + ast_file;
1416
if let Ok(lines) = helpers::read_lines(ast_file.to_string()) {
1517
// we skip the first line with is some null characters
1618
// the following lines in the AST are the dependency modules
@@ -74,23 +76,25 @@ pub fn get_deps(build_state: &mut BuildState, deleted_modules: &AHashSet<String>
7476
let package = build_state
7577
.get_package(&module.package_name)
7678
.expect("Package not found");
77-
let ast_path = package.get_ast_path(&source_file.implementation.path);
79+
let ast_path = helpers::get_ast_path(&source_file.implementation.path);
7880
if module.deps_dirty || !build_state.deps_initialized {
7981
let mut deps = get_dep_modules(
80-
&ast_path,
82+
&ast_path.to_string_lossy(),
8183
package.namespace.to_suffix(),
8284
package.modules.as_ref().unwrap(),
8385
all_mod,
86+
&package,
8487
);
8588

8689
if let Some(interface) = &source_file.interface {
87-
let iast_path = package.get_iast_path(&interface.path);
90+
let iast_path = helpers::get_ast_path(&interface.path);
8891

8992
deps.extend(get_dep_modules(
90-
&iast_path,
93+
&iast_path.to_string_lossy(),
9194
package.namespace.to_suffix(),
9295
package.modules.as_ref().unwrap(),
9396
all_mod,
97+
&package,
9498
))
9599
}
96100
match &package.namespace {

src/build/logs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ enum Location {
1616

1717
fn get_log_file_path(package: &packages::Package, subfolder: Location) -> String {
1818
let build_folder = match subfolder {
19-
Location::Bs => package.get_bs_build_path(),
20-
Location::Ocaml => package.get_build_path(),
19+
Location::Bs => package.get_build_path(),
20+
Location::Ocaml => package.get_ocaml_build_path(),
2121
};
2222

2323
build_folder.to_owned() + "/.compiler.log"

src/build/packages.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,16 @@ pub struct Package {
6363
}
6464

6565
pub fn get_build_path(canonical_path: &str) -> String {
66+
format!("{}/lib/bs", canonical_path)
67+
}
68+
69+
pub fn get_ocaml_build_path(canonical_path: &str) -> String {
6670
format!("{}/lib/ocaml", canonical_path)
6771
}
6872

6973
impl Package {
70-
pub fn get_bs_build_path(&self) -> String {
71-
format!("{}/lib/bs", self.path)
74+
pub fn get_ocaml_build_path(&self) -> String {
75+
get_ocaml_build_path(&self.path)
7276
}
7377

7478
pub fn get_build_path(&self) -> String {
@@ -94,14 +98,6 @@ impl Package {
9498
.expect("namespace should be set for mlmap module")
9599
+ ".cmi"
96100
}
97-
98-
pub fn get_ast_path(&self, source_file: &str) -> String {
99-
helpers::get_compiler_asset(self, &packages::Namespace::NoNamespace, source_file, "ast")
100-
}
101-
102-
pub fn get_iast_path(&self, source_file: &str) -> String {
103-
helpers::get_compiler_asset(self, &packages::Namespace::NoNamespace, source_file, "iast")
104-
}
105101
}
106102

107103
impl PartialEq for Package {
@@ -561,14 +557,6 @@ pub fn make(
561557
* the IO */
562558
let result = extend_with_children(filter, map);
563559

564-
result.values().for_each(|package| {
565-
if let Some(dirs) = &package.dirs {
566-
dirs.iter().for_each(|dir| {
567-
let _ = std::fs::create_dir_all(std::path::Path::new(&package.get_bs_build_path()).join(dir));
568-
})
569-
}
570-
});
571-
572560
Ok(result)
573561
}
574562

@@ -589,7 +577,7 @@ pub fn parse_packages(build_state: &mut BuildState) {
589577
build_state.module_names.extend(package_modules)
590578
}
591579
let build_path_abs = package.get_build_path();
592-
let bs_build_path = package.get_bs_build_path();
580+
let bs_build_path = package.get_ocaml_build_path();
593581
helpers::create_build_path(&build_path_abs);
594582
helpers::create_build_path(&bs_build_path);
595583

0 commit comments

Comments
 (0)