Skip to content

Commit d96bafc

Browse files
authored
Sync rewatch 1.0.9 (#7010)
* Sync rewatch 1.0.9 * CHANGELOG
1 parent 7dd97e7 commit d96bafc

21 files changed

+193
-47
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- Removed empty `default` case from `switch` statement in the generated code
3131
- Optimised the Type Extension runtime code and removed trailing `/1` from `RE_EXN_ID` https://github.com/rescript-lang/rescript-compiler/pull/6958
3232
- Compact output for anonymous functions. https://github.com/rescript-lang/rescript-compiler/pull/6945
33+
- Rewatch 1.0.9. https://github.com/rescript-lang/rescript-compiler/pull/7010
3334

3435
#### :bug: Bug Fix
3536
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949

rewatch/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rewatch/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rewatch"
3-
version = "1.0.6"
3+
version = "1.0.9"
44
edition = "2021"
55

66
[dependencies]

rewatch/src/build.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use console::style;
1818
use indicatif::{ProgressBar, ProgressStyle};
1919
use serde::Serialize;
2020
use std::fmt;
21+
use std::fs::File;
2122
use std::io::{stdout, Write};
2223
use std::path::PathBuf;
2324
use std::time::{Duration, Instant};
@@ -52,7 +53,7 @@ pub struct CompilerArgs {
5253
pub parser_args: Vec<String>,
5354
}
5455

55-
pub fn get_compiler_args(path: &str, rescript_version: Option<String>) -> String {
56+
pub fn get_compiler_args(path: &str, rescript_version: Option<String>, bsc_path: Option<String>) -> String {
5657
let filename = &helpers::get_abs_path(path);
5758
let package_root = helpers::get_abs_path(
5859
&helpers::get_nearest_bsconfig(&std::path::PathBuf::from(path)).expect("Couldn't find package root"),
@@ -64,7 +65,10 @@ pub fn get_compiler_args(path: &str, rescript_version: Option<String>) -> String
6465
let rescript_version = if let Some(rescript_version) = rescript_version {
6566
rescript_version
6667
} else {
67-
let bsc_path = helpers::get_bsc(&package_root, workspace_root.to_owned());
68+
let bsc_path = match bsc_path {
69+
Some(bsc_path) => bsc_path,
70+
None => helpers::get_bsc(&package_root, workspace_root.to_owned()),
71+
};
6872
helpers::get_rescript_version(&bsc_path)
6973
};
7074
// make PathBuf from package root and get the relative path for filename
@@ -134,10 +138,14 @@ pub fn initialize_build(
134138
default_timing: Option<Duration>,
135139
filter: &Option<regex::Regex>,
136140
path: &str,
141+
bsc_path: Option<String>,
137142
) -> Result<BuildState, InitializeBuildError> {
138143
let project_root = helpers::get_abs_path(path);
139144
let workspace_root = helpers::get_workspace_root(&project_root);
140-
let bsc_path = helpers::get_bsc(&project_root, workspace_root.to_owned());
145+
let bsc_path = match bsc_path {
146+
Some(bsc_path) => bsc_path,
147+
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
148+
};
141149
let root_config_name = packages::get_package_name(&project_root);
142150
let rescript_version = helpers::get_rescript_version(&bsc_path);
143151

@@ -407,11 +415,26 @@ impl fmt::Display for BuildError {
407415
}
408416
}
409417

418+
pub fn write_build_ninja(build_state: &BuildState) {
419+
// write build.ninja files in the packages after a non-incremental build
420+
// this is necessary to bust the editor tooling cache. The editor tooling
421+
// is watching this file.
422+
// we don't need to do this in an incremental build because there are no file
423+
// changes (deletes / additions)
424+
for package in build_state.packages.values() {
425+
// write empty file:
426+
let mut f = File::create(std::path::Path::new(&package.get_bs_build_path()).join("build.ninja"))
427+
.expect("Unable to write file");
428+
f.write_all(b"").expect("unable to write to ninja file");
429+
}
430+
}
431+
410432
pub fn build(
411433
filter: &Option<regex::Regex>,
412434
path: &str,
413435
no_timing: bool,
414436
create_sourcedirs: bool,
437+
bsc_path: Option<String>,
415438
) -> Result<BuildState, BuildError> {
416439
let default_timing: Option<std::time::Duration> = if no_timing {
417440
Some(std::time::Duration::new(0.0 as u64, 0.0 as u32))
@@ -420,7 +443,7 @@ pub fn build(
420443
};
421444
let timing_total = Instant::now();
422445
let mut build_state =
423-
initialize_build(default_timing, filter, path).map_err(BuildError::InitializeBuild)?;
446+
initialize_build(default_timing, filter, path, bsc_path).map_err(BuildError::InitializeBuild)?;
424447

425448
match incremental_build(&mut build_state, default_timing, true, false, create_sourcedirs) {
426449
Ok(_) => {
@@ -432,10 +455,12 @@ pub fn build(
432455
default_timing.unwrap_or(timing_total_elapsed).as_secs_f64()
433456
);
434457
clean::cleanup_after_build(&build_state);
458+
write_build_ninja(&build_state);
435459
Ok(build_state)
436460
}
437461
Err(e) => {
438462
clean::cleanup_after_build(&build_state);
463+
write_build_ninja(&build_state);
439464
Err(BuildError::IncrementalBuild(e))
440465
}
441466
}

rewatch/src/build/clean.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn remove_compile_asset(package: &packages::Package, source_file: &str, extensio
5151

5252
pub fn remove_compile_assets(package: &packages::Package, source_file: &str) {
5353
// optimization
54-
// only issue cmti if htere is an interfacce file
54+
// only issue cmti if there is an interfacce file
5555
for extension in &["cmj", "cmi", "cmt", "cmti"] {
5656
remove_compile_asset(package, source_file, extension);
5757
}
@@ -237,10 +237,11 @@ pub fn cleanup_previous_build(
237237
.map(|module_name| {
238238
// if the module is a namespace, we need to mark the whole namespace as dirty when a module has been deleted
239239
if let Some(namespace) = helpers::get_namespace_from_module_name(module_name) {
240-
return namespace;
240+
return vec![namespace, module_name.to_string()];
241241
}
242-
module_name.to_string()
242+
vec![module_name.to_string()]
243243
})
244+
.flatten()
244245
.collect::<AHashSet<String>>();
245246

246247
build_state.deleted_modules = deleted_module_names;
@@ -318,12 +319,16 @@ pub fn cleanup_after_build(build_state: &BuildState) {
318319
});
319320
}
320321

321-
pub fn clean(path: &str) {
322+
pub fn clean(path: &str, bsc_path: Option<String>) {
322323
let project_root = helpers::get_abs_path(path);
323324
let workspace_root = helpers::get_workspace_root(&project_root);
324325
let packages = packages::make(&None, &project_root, &workspace_root);
325326
let root_config_name = packages::get_package_name(&project_root);
326-
let bsc_path = helpers::get_bsc(&project_root, workspace_root.to_owned());
327+
let bsc_path = match bsc_path {
328+
Some(bsc_path) => bsc_path,
329+
None => helpers::get_bsc(&project_root, workspace_root.to_owned()),
330+
};
331+
327332
let rescript_version = helpers::get_rescript_version(&bsc_path);
328333

329334
let timing_clean_compiler_assets = Instant::now();

rewatch/src/build/compile.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ pub fn compile(
4646
// for sure clean modules -- after checking the hash of the cmi
4747
let mut clean_modules = AHashSet::<String>::new();
4848

49-
// TODO: calculate the real dirty modules from the orginal dirty modules in each iteration
49+
// TODO: calculate the real dirty modules from the original dirty modules in each iteration
5050
// taken into account the modules that we know are clean, so they don't propagate through the
5151
// deps graph
52-
// create a hashset of all clean modules form the file-hashes
52+
// create a hashset of all clean modules from the file-hashes
5353
let mut loop_count = 0;
5454
let mut files_total_count = compiled_modules.len();
5555
let mut files_current_loop_count;
@@ -575,7 +575,7 @@ fn compile_file(
575575
// because editor tooling doesn't support namespace entries yet
576576
// we just remove the @ for now. This makes sure the editor support
577577
// doesn't break
578-
.join(module_name.to_owned().replace('@', "") + ".cmi"),
578+
.join(module_name.to_owned() + ".cmi"),
579579
);
580580
let _ = std::fs::copy(
581581
build_path_abs.to_string() + "/" + &module_name + ".cmj",
@@ -590,7 +590,7 @@ fn compile_file(
590590
// because editor tooling doesn't support namespace entries yet
591591
// we just remove the @ for now. This makes sure the editor support
592592
// doesn't break
593-
.join(module_name.to_owned().replace('@', "") + ".cmt"),
593+
.join(module_name.to_owned() + ".cmt"),
594594
);
595595
} else {
596596
let _ = std::fs::copy(

rewatch/src/build/deps.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ fn get_dep_modules(
4444
_ => dep_first,
4545
};
4646
let namespaced_name = dep.to_owned() + "-" + namespace;
47-
if package_modules.contains(&namespaced_name) {
47+
if package_modules.contains(&namespaced_name) || valid_modules.contains(&namespaced_name)
48+
{
4849
namespaced_name
4950
} else {
5051
dep.to_string()

rewatch/src/build/namespaces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn gen_mlmap(
3232
let path = build_path_abs.to_string() + "/" + namespace + ".mlmap";
3333
let mut file = File::create(&path).expect("Unable to create mlmap");
3434

35-
file.write_all(b"randjbuildsystem\n" as &[u8])
35+
file.write_all(b"randjbuildsystem\n")
3636
.expect("Unable to write mlmap");
3737

3838
let mut modules = Vec::from_iter(depending_modules.to_owned());

rewatch/src/build/packages.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn read_folders(
180180
/// Given a projects' root folder and a `bsconfig::Source`, this recursively creates all the
181181
/// sources in a flat list. In the process, it removes the children, as they are being resolved
182182
/// because of the recursiveness. So you get a flat list of files back, retaining the type_ and
183-
/// wether it needs to recurse into all structures
183+
/// whether it needs to recurse into all structures
184184
fn get_source_dirs(source: bsconfig::Source, sub_path: Option<PathBuf>) -> AHashSet<bsconfig::PackageSource> {
185185
let mut source_folders: AHashSet<bsconfig::PackageSource> = AHashSet::new();
186186

@@ -276,7 +276,7 @@ pub fn read_dependency(
276276

277277
/// # Make Package
278278
279-
/// Given a bsconfig, reqursively finds all dependencies.
279+
/// Given a bsconfig, recursively finds all dependencies.
280280
/// 1. It starts with registering dependencies and
281281
/// prevents the operation for the ones which are already
282282
/// registerd for the parent packages. Especially relevant for peerDependencies.
@@ -430,7 +430,7 @@ fn read_packages(project_root: &str, workspace_root: Option<String>) -> AHashMap
430430
/// data from the config and pushes it forwards. Another thing is the 'type_', some files / folders
431431
/// can be marked with the type 'dev'. Which means that they may not be around in the distributed
432432
/// NPM package. The file reader allows for this, just warns when this happens.
433-
/// TODO -> Check wether we actually need the `fs::Metadata`
433+
/// TODO -> Check whether we actually need the `fs::Metadata`
434434
pub fn get_source_files(
435435
package_dir: &Path,
436436
filter: &Option<regex::Regex>,

rewatch/src/build/parse.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,29 @@ pub fn generate_asts(
203203
namespaces::compile_mlmap(package, module_name, &build_state.bsc_path);
204204
let mlmap_hash_after = helpers::compute_file_hash(&compile_path);
205205

206+
let suffix = package
207+
.namespace
208+
.to_suffix()
209+
.expect("namespace should be set for mlmap module");
210+
// copy the mlmap to the bs build path for editor tooling
211+
let base_build_path = package.get_build_path() + "/" + &suffix;
212+
let base_bs_build_path = package.get_bs_build_path() + "/" + &suffix;
213+
let _ = std::fs::copy(
214+
base_build_path.to_string() + ".cmi",
215+
base_bs_build_path.to_string() + ".cmi",
216+
);
217+
let _ = std::fs::copy(
218+
base_build_path.to_string() + ".cmt",
219+
base_bs_build_path.to_string() + ".cmt",
220+
);
221+
let _ = std::fs::copy(
222+
base_build_path.to_string() + ".cmj",
223+
base_bs_build_path.to_string() + ".cmj",
224+
);
225+
let _ = std::fs::copy(
226+
base_build_path.to_string() + ".mlmap",
227+
base_bs_build_path.to_string() + ".mlmap",
228+
);
206229
match (mlmap_hash, mlmap_hash_after) {
207230
(Some(digest), Some(digest_after)) => !digest.eq(&digest_after),
208231
_ => true,
@@ -299,7 +322,7 @@ fn generate_ast(
299322
);
300323

301324
/* Create .ast */
302-
if let Some(res_to_ast) = Some(
325+
let result = if let Some(res_to_ast) = Some(
303326
Command::new(bsc_path)
304327
.current_dir(&build_path_abs)
305328
.args(parser_args)
@@ -322,7 +345,20 @@ fn generate_ast(
322345
"Could not find canonicalize_string_path for file {} in package {}",
323346
filename, package.name
324347
))
348+
};
349+
match &result {
350+
Ok((ast_path, _)) => {
351+
let dir = std::path::Path::new(filename).parent().unwrap();
352+
let _ = std::fs::copy(
353+
build_path_abs.to_string() + "/" + ast_path,
354+
std::path::Path::new(&package.get_bs_build_path())
355+
.join(dir)
356+
.join(ast_path),
357+
);
358+
}
359+
Err(_) => (),
325360
}
361+
result
326362
}
327363

328364
fn path_to_ast_extension(path: &Path) -> &str {

rewatch/src/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,18 @@ struct Args {
4545
#[arg(short, long)]
4646
create_sourcedirs: Option<bool>,
4747

48+
/// This prints the compiler arguments. It expects the path to a rescript.json file.
49+
/// This also requires --bsc-path and --rescript-version to be present
4850
#[arg(long)]
4951
compiler_args: Option<String>,
5052

53+
/// To be used in conjunction with compiler_args
5154
#[arg(long)]
5255
rescript_version: Option<String>,
56+
57+
/// A custom path to bsc
58+
#[arg(long)]
59+
bsc_path: Option<String>,
5360
}
5461

5562
fn main() {
@@ -65,7 +72,10 @@ fn main() {
6572
match args.compiler_args {
6673
None => (),
6774
Some(path) => {
68-
println!("{}", build::get_compiler_args(&path, args.rescript_version));
75+
println!(
76+
"{}",
77+
build::get_compiler_args(&path, args.rescript_version, args.bsc_path)
78+
);
6979
std::process::exit(0);
7080
}
7181
}
@@ -76,13 +86,14 @@ fn main() {
7686
std::process::exit(1)
7787
}
7888
lock::Lock::Aquired(_) => match command {
79-
Command::Clean => build::clean::clean(&folder),
89+
Command::Clean => build::clean::clean(&folder, args.bsc_path),
8090
Command::Build => {
8191
match build::build(
8292
&filter,
8393
&folder,
8494
args.no_timing.unwrap_or(false),
8595
args.create_sourcedirs.unwrap_or(false),
96+
args.bsc_path,
8697
) {
8798
Err(e) => {
8899
eprintln!("Error Building: {e}");

rewatch/src/watcher.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async fn async_watch(
5353
after_build: Option<String>,
5454
create_sourcedirs: bool,
5555
) -> notify::Result<()> {
56-
let mut build_state = build::initialize_build(None, filter, path).expect("Can't initialize build");
56+
let mut build_state = build::initialize_build(None, filter, path, None).expect("Can't initialize build");
5757
let mut needs_compile_type = CompileType::Incremental;
5858
// create a mutex to capture if ctrl-c was pressed
5959
let ctrlc_pressed = Arc::new(Mutex::new(false));
@@ -205,12 +205,16 @@ async fn async_watch(
205205
}
206206
CompileType::Full => {
207207
let timing_total = Instant::now();
208-
build_state = build::initialize_build(None, filter, path).expect("Can't initialize build");
208+
build_state =
209+
build::initialize_build(None, filter, path, None).expect("Can't initialize build");
209210
let _ =
210211
build::incremental_build(&mut build_state, None, initial_build, false, create_sourcedirs);
211212
if let Some(a) = after_build.clone() {
212213
cmd::run(a)
213214
}
215+
216+
build::write_build_ninja(&build_state);
217+
214218
let timing_total_elapsed = timing_total.elapsed();
215219
println!(
216220
"\n{}{}Finished compilation in {:.2}s\n",

0 commit comments

Comments
 (0)