Skip to content

Commit

Permalink
🐛 - Fix error reporting with language server
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 26, 2023
1 parent 5bce515 commit 7a00ab0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 42 deletions.
48 changes: 35 additions & 13 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ fn generate_asts(version: &str, build_state: &mut BuildState, pb: &ProgressBar)
}
_ => (),
}
logs::append(&package.package_dir, &err);
logs::append(&build_state.project_root, package.is_root, &package.name, &err);
stderr.push_str(&err);
}
}
Expand All @@ -465,7 +465,7 @@ fn generate_asts(version: &str, build_state: &mut BuildState, pb: &ProgressBar)
}
_ => (),
}
logs::append(&package.package_dir, &err);
logs::append(&build_state.project_root, package.is_root, &package.name, &err);
has_failure = true;
stderr.push_str(&err);
}
Expand All @@ -484,7 +484,7 @@ fn generate_asts(version: &str, build_state: &mut BuildState, pb: &ProgressBar)
}
_ => (),
}
logs::append(&package.package_dir, &err);
logs::append(&build_state.project_root, package.is_root, &package.name, &err);
stderr.push_str(&err);
}
}
Expand All @@ -500,7 +500,7 @@ fn generate_asts(version: &str, build_state: &mut BuildState, pb: &ProgressBar)
}
_ => (),
}
logs::append(&package.package_dir, &err);
logs::append(&build_state.project_root, package.is_root, &package.name, &err);
has_failure = true;
stderr.push_str(&err);
}
Expand Down Expand Up @@ -599,12 +599,15 @@ pub fn parse_packages(build_state: &mut BuildState) {
Some(package_modules) => build_state.module_names.extend(package_modules),
None => (),
}
let build_path_abs = helpers::get_build_path(
let build_path_abs =
helpers::get_build_path(&build_state.project_root, &package.bsconfig.name, package.is_root);
let bs_build_path = helpers::get_bs_build_path(
&build_state.project_root,
&package.bsconfig.name,
package.is_root,
);
helpers::create_build_path(&build_path_abs);
helpers::create_build_path(&bs_build_path);

package.namespace.to_suffix().iter().for_each(|namespace| {
// generate the mlmap "AST" file for modules that have a namespace configured
Expand Down Expand Up @@ -1251,7 +1254,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
println!("{}", &err);
}
Err(err) => {
logs::finalize(&build_state.packages);
logs::finalize(&build_state.project_root, &build_state.packages);
println!(
"{}\r{} {}Error parsing source files in {:.2}s",
LINE_CLEAR,
Expand Down Expand Up @@ -1538,30 +1541,49 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
SourceType::SourceFile(ref mut source_file) => {
match result {
Ok(Some(err)) => {
source_file.implementation.compile_state =
CompileState::Warning;
logs::append(&package.package_dir, &err);
source_file.implementation.compile_state = CompileState::Warning;
logs::append(
&build_state.project_root,
package.is_root,
&package.name,
&err,
);
compile_warnings.push_str(&err);
}
Ok(None) => (),
Err(err) => {
source_file.implementation.compile_state = CompileState::Error;
logs::append(&package.package_dir, &err);
logs::append(
&build_state.project_root,
package.is_root,
&package.name,
&err,
);
compile_errors.push_str(&err);
}
};
match interface_result {
Some(Ok(Some(err))) => {
source_file.interface.as_mut().unwrap().compile_state =
CompileState::Warning;
logs::append(&package.package_dir, &err);
logs::append(
&build_state.project_root,
package.is_root,
&package.name,
&err,
);
compile_warnings.push_str(&err);
}
Some(Ok(None)) => (),
Some(Err(err)) => {
source_file.interface.as_mut().unwrap().compile_state =
CompileState::Error;
logs::append(&package.package_dir, &err);
logs::append(
&build_state.project_root,
package.is_root,
&package.name,
&err,
);
compile_errors.push_str(&err);
}
_ => (),
Expand All @@ -1587,7 +1609,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Resu
}
let compile_duration = start_compiling.elapsed();

logs::finalize(&build_state.packages);
logs::finalize(&build_state.project_root, &build_state.packages);
pb.finish();
clean::cleanup_after_build(&build_state);
if compile_errors.len() > 0 {
Expand Down
66 changes: 37 additions & 29 deletions src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ enum Location {
Ocaml,
}

fn get_log_file_path(subfolder: Location, name: &str) -> String {
let subfolder_str = match subfolder {
Location::Bs => "bs",
Location::Ocaml => "ocaml",
fn get_log_file_path(project_root: &str, subfolder: Location, name: &str, is_root: bool) -> String {
let build_folder = match subfolder {
Location::Bs => helpers::get_bs_build_path(project_root, name, is_root),
Location::Ocaml => helpers::get_build_path(project_root, name, is_root),
};
name.to_owned() + "/lib/" + subfolder_str + "/.compiler.log"

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

fn escape_colours(str: &str) -> String {
Expand All @@ -43,38 +44,45 @@ fn write_to_log_file(mut file: File, package_name: &str, content: &str) {
}
}

pub fn initialize(packages: &AHashMap<String, Package>) {
packages.par_iter().for_each(|(name, _)| {
let _ = File::create(get_log_file_path(Location::Bs, name)).map(|file| {
write_to_log_file(
file,
&name,
&format!("#Start({})\n", helpers::get_system_time()),
)
});
pub fn initialize(project_root: &str, packages: &AHashMap<String, Package>) {
packages.par_iter().for_each(|(name, package)| {
let _ = File::create(get_log_file_path(
project_root,
Location::Bs,
name,
package.is_root,
))
.map(|file| write_to_log_file(file, &name, &format!("#Start({})\n", helpers::get_system_time())))
.expect(&("Cannot create compiler log for package ".to_owned() + name));
})
}

pub fn append(name: &str, str: &str) {
let _ = File::options()
pub fn append(project_root: &str, is_root: bool, name: &str, str: &str) {
File::options()
.append(true)
.open(get_log_file_path(Location::Bs, name))
.map(|file| write_to_log_file(file, &name, str));
.open(get_log_file_path(project_root, Location::Bs, name, is_root))
.map(|file| write_to_log_file(file, &name, str))
.expect(
&("Cannot write compilerlog: ".to_owned()
+ &get_log_file_path(project_root, Location::Bs, name, is_root)),
);
}

pub fn finalize(packages: &AHashMap<String, Package>) {
packages.par_iter().for_each(|(name, _)| {
pub fn finalize(project_root: &str, packages: &AHashMap<String, Package>) {
packages.par_iter().for_each(|(name, package)| {
let _ = File::options()
.append(true)
.open(get_log_file_path(Location::Bs, name))
.map(|file| {
write_to_log_file(
file,
&name,
&format!("#Done({})\n", helpers::get_system_time()),
)
});
.open(get_log_file_path(
project_root,
Location::Bs,
name,
package.is_root,
))
.map(|file| write_to_log_file(file, &name, &format!("#Done({})\n", helpers::get_system_time())));

let _ = std::fs::copy(get_log_file_path(Location::Bs, name), get_log_file_path(Location::Ocaml, name));
let _ = std::fs::copy(
get_log_file_path(project_root, Location::Bs, name, package.is_root),
get_log_file_path(project_root, Location::Ocaml, name, package.is_root),
);
})
}

0 comments on commit 7a00ab0

Please sign in to comment.