Skip to content

Commit 87a169a

Browse files
committed
make it work
1 parent 04ffb68 commit 87a169a

File tree

6 files changed

+88
-67
lines changed

6 files changed

+88
-67
lines changed

src/build.rs

Lines changed: 3 additions & 2 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,

src/build/compile.rs

Lines changed: 19 additions & 19 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,
@@ -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

@@ -480,13 +477,10 @@ pub fn compiler_args(
480477
read_cmi_args,
481478
vec!["-I".to_string(), ".".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()
@@ -539,7 +534,6 @@ fn compile_file(
539534
&Some(packages),
540535
build_dev_deps,
541536
);
542-
543537
let to_mjs = Command::new(bsc_path)
544538
.current_dir(helpers::canonicalize_string_path(&build_path_abs.to_owned()).unwrap())
545539
.args(to_mjs_args)
@@ -566,35 +560,41 @@ fn compile_file(
566560
// perhaps we can do this copying somewhere else
567561
if !is_interface {
568562
let _ = std::fs::copy(
569-
build_path_abs.to_string() + "/" + &module_name + ".cmi",
570-
std::path::Path::new(&package.get_bs_build_path())
563+
std::path::Path::new(&package.get_build_path())
571564
.join(dir)
572565
// because editor tooling doesn't support namespace entries yet
573566
// we just remove the @ for now. This makes sure the editor support
574567
// doesn't break
575568
.join(module_name.to_owned() + ".cmi"),
569+
build_path_abs.to_string() + "/" + &module_name + ".cmi",
576570
);
577571
let _ = std::fs::copy(
578-
build_path_abs.to_string() + "/" + &module_name + ".cmj",
579-
std::path::Path::new(&package.get_bs_build_path())
572+
std::path::Path::new(&package.get_build_path())
580573
.join(dir)
581574
.join(module_name.to_owned() + ".cmj"),
575+
build_path_abs.to_string() + "/" + &module_name + ".cmj",
582576
);
583577
let _ = std::fs::copy(
584-
build_path_abs.to_string() + "/" + &module_name + ".cmt",
585-
std::path::Path::new(&package.get_bs_build_path())
578+
std::path::Path::new(&package.get_build_path())
586579
.join(dir)
587580
// because editor tooling doesn't support namespace entries yet
588581
// we just remove the @ for now. This makes sure the editor support
589582
// doesn't break
590583
.join(module_name.to_owned() + ".cmt"),
584+
build_path_abs.to_string() + "/" + &module_name + ".cmt",
591585
);
592586
} else {
593587
let _ = std::fs::copy(
594-
build_path_abs.to_string() + "/" + &module_name + ".cmti",
595-
std::path::Path::new(&package.get_bs_build_path())
588+
std::path::Path::new(&package.get_build_path())
596589
.join(dir)
597590
.join(module_name.to_owned() + ".cmti"),
591+
build_path_abs.to_string() + "/" + &module_name + ".cmti",
592+
);
593+
let _ = std::fs::copy(
594+
std::path::Path::new(&package.get_build_path())
595+
.join(dir)
596+
.join(module_name.to_owned() + ".cmi"),
597+
build_path_abs.to_string() + "/" + &module_name + ".cmi",
598598
);
599599
}
600600
match &module.source_type {
@@ -617,7 +617,7 @@ fn compile_file(
617617

618618
let _ = std::fs::copy(
619619
std::path::Path::new(&package.path).join(path),
620-
std::path::Path::new(&package.get_build_path())
620+
std::path::Path::new(&package.get_bs_build_path())
621621
.join(std::path::Path::new(path).file_name().unwrap()),
622622
)
623623
.expect("copying source file failed");

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/packages.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,6 @@ impl Package {
9494
.expect("namespace should be set for mlmap module")
9595
+ ".cmi"
9696
}
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-
}
10597
}
10698

10799
impl PartialEq for Package {

src/build/parse.rs

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ pub fn generate_asts(
2929
match &module.source_type {
3030
SourceType::MlMap(_mlmap) => {
3131
let path = package.get_mlmap_path();
32-
(module_name.to_owned(), Ok((path, None)), Ok(None), false)
32+
(
33+
module_name.to_owned(),
34+
Ok((Path::new(&path).to_path_buf(), None)),
35+
Ok(None),
36+
false,
37+
)
3338
}
3439

3540
SourceType::SourceFile(source_file) => {
@@ -69,13 +74,20 @@ pub fn generate_asts(
6974
} else {
7075
(
7176
Ok((
72-
helpers::get_basename(&source_file.implementation.path).to_string() + ".ast",
77+
Path::new(
78+
&(helpers::get_basename(&source_file.implementation.path).to_string()
79+
+ ".ast"),
80+
)
81+
.to_path_buf(),
7382
None,
7483
)),
75-
Ok(source_file
76-
.interface
77-
.as_ref()
78-
.map(|i| (helpers::get_basename(&i.path).to_string() + ".iast", None))),
84+
Ok(source_file.interface.as_ref().map(|i| {
85+
(
86+
Path::new(&(helpers::get_basename(&i.path).to_string() + ".iast"))
87+
.to_path_buf(),
88+
None,
89+
)
90+
})),
7991
false,
8092
)
8193
};
@@ -86,8 +98,8 @@ pub fn generate_asts(
8698
})
8799
.collect::<Vec<(
88100
String,
89-
Result<(String, Option<helpers::StdErr>), String>,
90-
Result<Option<(String, Option<helpers::StdErr>)>, String>,
101+
Result<(PathBuf, Option<helpers::StdErr>), String>,
102+
Result<Option<(PathBuf, Option<helpers::StdErr>)>, String>,
91103
bool,
92104
)>>()
93105
.into_iter()
@@ -250,11 +262,14 @@ pub fn parser_args(
250262
workspace_root: &Option<String>,
251263
root_path: &str,
252264
contents: &str,
253-
) -> (String, Vec<String>) {
265+
) -> (PathBuf, Vec<String>) {
254266
let file = &filename.to_string();
255-
let path = PathBuf::from(filename);
256-
let ast_extension = path_to_ast_extension(&path);
257-
let ast_path = (helpers::get_basename(&file.to_string()).to_owned()) + ast_extension;
267+
// let path = PathBuf::from(filename);
268+
// let ast_extension = path_to_ast_extension(&path);
269+
// let ast_path = (helpers::get_basename(&file.to_string()).to_owned()) + ast_extension;
270+
let ast_path = helpers::get_ast_path(file);
271+
// make the dir of the ast_path if it doesn't exist yet:
272+
258273
let ppx_flags = config::flatten_ppx_flags(
259274
&if let Some(workspace_root) = workspace_root {
260275
format!("{}/node_modules", &workspace_root)
@@ -272,7 +287,7 @@ pub fn parser_args(
272287

273288
let file = "../../".to_string() + file;
274289
(
275-
ast_path.to_string(),
290+
ast_path.to_owned(),
276291
[
277292
vec!["-bs-v".to_string(), format!("{}", version)],
278293
ppx_flags,
@@ -285,7 +300,7 @@ pub fn parser_args(
285300
"-absname".to_string(),
286301
"-bs-ast".to_string(),
287302
"-o".to_string(),
288-
ast_path.to_string(),
303+
ast_path.to_string_lossy().to_string(),
289304
file,
290305
],
291306
]
@@ -300,7 +315,7 @@ fn generate_ast(
300315
version: &str,
301316
bsc_path: &str,
302317
workspace_root: &Option<String>,
303-
) -> Result<(String, Option<helpers::StdErr>), String> {
318+
) -> Result<(PathBuf, Option<helpers::StdErr>), String> {
304319
let file_path = PathBuf::from(&package.path).join(filename);
305320
let contents = helpers::read_file(&file_path).expect("Error reading file");
306321

@@ -315,6 +330,11 @@ fn generate_ast(
315330
&contents,
316331
);
317332

333+
// generate the dir of the ast_path (it mirrors the source file dir)
334+
helpers::create_build_path(
335+
&(package.get_build_path() + "/" + &ast_path.parent().unwrap().to_string_lossy()),
336+
);
337+
318338
/* Create .ast */
319339
let result = if let Some(res_to_ast) = Some(
320340
Command::new(bsc_path)
@@ -342,26 +362,15 @@ fn generate_ast(
342362
))
343363
};
344364
if let Ok((ast_path, _)) = &result {
345-
let dir = std::path::Path::new(filename).parent().unwrap();
365+
// let dir = std::path::Path::new(filename).parent().unwrap();
346366
let _ = std::fs::copy(
347-
build_path_abs.to_string() + "/" + ast_path,
348-
std::path::Path::new(&package.get_bs_build_path())
349-
.join(dir)
350-
.join(ast_path),
367+
Path::new(&build_path_abs).join(&ast_path),
368+
std::path::Path::new(&package.get_build_path()).join(ast_path.file_name().unwrap()),
351369
);
352370
}
353371
result
354372
}
355373

356-
fn path_to_ast_extension(path: &Path) -> &str {
357-
let extension = path.extension().unwrap().to_str().unwrap();
358-
if helpers::is_interface_ast_file(extension) {
359-
".iast"
360-
} else {
361-
".ast"
362-
}
363-
}
364-
365374
fn include_ppx(flag: &str, contents: &str) -> bool {
366375
if flag.contains("bisect") {
367376
return std::env::var("BISECT_ENABLE").is_ok();

src/helpers.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,24 @@ pub fn string_ends_with_any(s: &Path, suffixes: &[&str]) -> bool {
184184
.any(|&suffix| s.extension().unwrap_or(&OsString::new()).to_str().unwrap_or("") == suffix)
185185
}
186186

187+
fn path_to_ast_extension(path: &Path) -> &str {
188+
let extension = path.extension().unwrap().to_str().unwrap();
189+
if extension.ends_with("i") {
190+
".iast"
191+
} else {
192+
".ast"
193+
}
194+
}
195+
196+
pub fn get_ast_path(source_file: &str) -> PathBuf {
197+
let source_path = Path::new(source_file);
198+
199+
source_path.parent().unwrap().join(
200+
file_path_to_compiler_asset_basename(source_file, &packages::Namespace::NoNamespace)
201+
+ path_to_ast_extension(source_path),
202+
)
203+
}
204+
187205
pub fn get_compiler_asset(
188206
package: &packages::Package,
189207
namespace: &packages::Namespace,
@@ -201,11 +219,8 @@ pub fn get_compiler_asset(
201219
+ extension
202220
}
203221

204-
pub fn canonicalize_string_path(path: &str) -> Option<String> {
205-
return Path::new(path)
206-
.canonicalize()
207-
.ok()
208-
.map(|path| path.to_str().expect("Could not canonicalize").to_string());
222+
pub fn canonicalize_string_path(path: &str) -> Option<PathBuf> {
223+
return Path::new(path).canonicalize().ok();
209224
}
210225

211226
pub fn get_bs_compiler_asset(

0 commit comments

Comments
 (0)