Skip to content

Commit

Permalink
improve n2 error display (#266)
Browse files Browse the repository at this point in the history
* refactor: improve error messages for opening db failures

* update test
  • Loading branch information
lijunchen authored Sep 6, 2024
1 parent 69e9a62 commit db845e3
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 16 deletions.
26 changes: 26 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6202,3 +6202,29 @@ fn test_moon_doc() {
"#]],
);
}

#[test]
fn test_failed_to_fill_whole_buffer() {
let dir = TestDir::new("hello.in");
check(
&get_stderr_on_success_with_args_and_replace_dir(&dir, ["check"]),
expect![[r#"
Finished. moon: ran 1 task, now up to date
"#]],
);
let moon_db_path = dir.join("./target/wasm-gc/release/check/check.moon_db");
if moon_db_path.exists() {
std::fs::remove_file(&moon_db_path).unwrap();
}
std::fs::write(&moon_db_path, "").unwrap();
check(
&get_stderr_with_args_and_replace_dir(&dir, ["check"]),
expect![[r#"
error: internal build state error: $ROOT/target/wasm-gc/release/check/check.moon_db
Caused by:
0: failed to open build database
1: failed to fill whole buffer
"#]],
);
}
13 changes: 9 additions & 4 deletions crates/moonbuild/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use n2::smallmap::SmallMap;
use std::rc::Rc;

use crate::gen::cmd_builder::CommandBuilder;
use crate::gen::n2_errors::{N2Error, N2ErrorKind};

pub fn format_package(dir: &Path) -> anyhow::Result<i32> {
let mut errors = vec![];
Expand Down Expand Up @@ -213,8 +214,10 @@ pub fn gen_n2_fmt_state(

let mut hashes = n2graph::Hashes::default();
let n2_db_path = &moonbuild_opt.target_dir.join("format.db");
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes)
.with_context(|| format!("failed to process n2 db: {}", n2_db_path.display()))?;
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes).map_err(|e| N2Error {
path: n2_db_path.to_path_buf(),
source: N2ErrorKind::OpenDataBaseError(e),
})?;

Ok(State {
graph,
Expand Down Expand Up @@ -310,8 +313,10 @@ pub fn gen_n2_fmt_check_state(

let mut hashes = n2graph::Hashes::default();
let n2_db_path = &moonbuild_opt.target_dir.join("format.db");
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes)
.with_context(|| format!("failed to process n2 db: {}", n2_db_path.display()))?;
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes).map_err(|e| N2Error {
path: n2_db_path.to_path_buf(),
source: N2ErrorKind::OpenDataBaseError(e),
})?;

Ok(State {
graph,
Expand Down
9 changes: 6 additions & 3 deletions crates/moonbuild/src/gen/gen_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
//
// For inquiries, you can contact us via e-mail at [email protected].

use anyhow::{bail, Context, Ok};
use anyhow::{bail, Ok};
use moonutil::common::TargetBackend::*;
use moonutil::module::ModuleDB;
use moonutil::package::{JsFormat, Package};

use super::cmd_builder::CommandBuilder;
use super::n2_errors::{N2Error, N2ErrorKind};
use crate::gen::MiAlias;
use std::path::{Path, PathBuf};
use std::rc::Rc;
Expand Down Expand Up @@ -444,8 +445,10 @@ pub fn gen_n2_build_state(

let mut hashes = n2graph::Hashes::default();
let n2_db_path = &target_dir.join("build.moon_db");
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes)
.with_context(|| format!("failed to process n2 db: {}", n2_db_path.display()))?;
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes).map_err(|e| N2Error {
path: n2_db_path.to_path_buf(),
source: N2ErrorKind::OpenDataBaseError(e),
})?;

Ok(State {
graph,
Expand Down
9 changes: 6 additions & 3 deletions crates/moonbuild/src/gen/gen_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
// For inquiries, you can contact us via e-mail at [email protected].

use anyhow::{bail, Context};
use anyhow::bail;
use indexmap::IndexMap;
use moonutil::module::ModuleDB;
use moonutil::package::Package;
Expand All @@ -29,6 +29,7 @@ use std::rc::Rc;
use moonutil::common::{MoonbuildOpt, MooncOpt, MOON_PKG_JSON};

use super::cmd_builder::CommandBuilder;
use super::n2_errors::{N2Error, N2ErrorKind};
use crate::gen::MiAlias;

#[derive(Debug)]
Expand Down Expand Up @@ -307,8 +308,10 @@ pub fn gen_n2_bundle_state(

let mut hashes = n2graph::Hashes::default();
let n2_db_path = &target_dir.join("build.moon_db");
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes)
.with_context(|| format!("failed to process n2 db: {}", n2_db_path.display()))?;
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes).map_err(|e| N2Error {
path: n2_db_path.to_path_buf(),
source: N2ErrorKind::OpenDataBaseError(e),
})?;

Ok(State {
graph,
Expand Down
9 changes: 6 additions & 3 deletions crates/moonbuild/src/gen/gen_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
// For inquiries, you can contact us via e-mail at [email protected].

use super::cmd_builder::CommandBuilder;
use super::n2_errors::{N2Error, N2ErrorKind};
use crate::gen::MiAlias;
use anyhow::{bail, Context};
use anyhow::bail;
use indexmap::map::IndexMap;
use moonutil::module::ModuleDB;
use moonutil::package::Package;
Expand Down Expand Up @@ -368,8 +369,10 @@ pub fn gen_n2_check_state(

let mut hashes = n2graph::Hashes::default();
let n2_db_path = &target_dir.join("check.moon_db");
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes)
.with_context(|| format!("failed to process n2 db: {}", n2_db_path.display()))?;
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes).map_err(|e| N2Error {
path: n2_db_path.to_path_buf(),
source: N2ErrorKind::OpenDataBaseError(e),
})?;

let default = graph.get_start_nodes();

Expand Down
9 changes: 6 additions & 3 deletions crates/moonbuild/src/gen/gen_runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//
// For inquiries, you can contact us via e-mail at [email protected].

use anyhow::{bail, Context, Ok};
use anyhow::{bail, Ok};
use colored::Colorize;
use moonutil::common::{DriverKind, GeneratedTestDriver, MOONBITLANG_CORE};
use moonutil::module::ModuleDB;
Expand All @@ -32,6 +32,7 @@ use n2::graph::{self as n2graph, Build, BuildIns, BuildOuts, FileLoc};
use n2::load::State;
use n2::smallmap::SmallMap;

use crate::gen::n2_errors::{N2Error, N2ErrorKind};
use crate::gen::MiAlias;

#[derive(Debug)]
Expand Down Expand Up @@ -918,8 +919,10 @@ pub fn gen_n2_runtest_state(

let mut hashes = n2graph::Hashes::default();
let n2_db_path = &moonbuild_opt.target_dir.join("build.moon_db");
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes)
.with_context(|| format!("failed to process n2 db: {}", n2_db_path.display()))?;
let db = n2::db::open(n2_db_path, &mut graph, &mut hashes).map_err(|e| N2Error {
path: n2_db_path.to_path_buf(),
source: N2ErrorKind::OpenDataBaseError(e),
})?;

Ok(State {
graph,
Expand Down
1 change: 1 addition & 0 deletions crates/moonbuild/src/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod gen_build;
pub mod gen_bundle;
pub mod gen_check;
pub mod gen_runtest;
pub mod n2_errors;
pub mod util;

// WORKAROUND for do not test coverage on coverage library itself
Expand Down
33 changes: 33 additions & 0 deletions crates/moonbuild/src/gen/n2_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// moon: The build system and package manager for MoonBit.
// Copyright (C) 2024 International Digital Economy Academy
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// For inquiries, you can contact us via e-mail at [email protected].

use std::path::PathBuf;

#[derive(Debug, thiserror::Error)]
#[error("internal build state error: {path}")]
pub struct N2Error {
pub path: PathBuf,
#[source]
pub(crate) source: N2ErrorKind,
}

#[derive(Debug, thiserror::Error)]
pub enum N2ErrorKind {
#[error("failed to open build database")]
OpenDataBaseError(#[from] anyhow::Error),
}

0 comments on commit db845e3

Please sign in to comment.