Skip to content

Commit

Permalink
fix: fix some missing diagnostics in parser. (#677)
Browse files Browse the repository at this point in the history
* fix: fix some missing diagnostics in parser.

* fix: fix test cases.
  • Loading branch information
zong-zhe authored Aug 22, 2023
1 parent 6ce46eb commit 09cb631
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions kclvm/cmd/src/test_data/plugin/plugin_not_found/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "plugin_not_found"
edition = "0.0.1"
version = "0.0.1"

3 changes: 3 additions & 0 deletions kclvm/cmd/src/test_data/plugin/plugin_not_found/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import kcl_plugin.not_exist

The_first_kcl_program = 'Hello World!'
16 changes: 16 additions & 0 deletions kclvm/cmd/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ fn test_run_command() {
test_main_pkg_not_found();
test_conflict_mod_file();
test_instances_with_yaml();
test_plugin_not_found();
}

fn test_run_command_with_import() {
Expand Down Expand Up @@ -542,3 +543,18 @@ fn test_conflict_mod_file() {
Err(msg) => assert!(msg.contains("conflict kcl.mod file paths")),
}
}

fn test_plugin_not_found() {
let test_case_path = PathBuf::from("./src/test_data/plugin/plugin_not_found");
let matches = app().arg_required_else_help(true).get_matches_from(&[
ROOT_CMD,
"run",
test_case_path.as_path().display().to_string().as_str(),
]);
let settings = must_build_settings(matches.subcommand_matches("run").unwrap());
let sess = Arc::new(ParseSession::default());
match exec_program(sess.clone(), &settings.try_into().unwrap()) {
Ok(_) => panic!("unreachable code."),
Err(msg) => assert!(msg.contains("the plugin package `kcl_plugin.not_exist` is not found, please confirm if plugin mode is enabled")),
}
}
29 changes: 27 additions & 2 deletions kclvm/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use kclvm_error::{Diagnostic, Handler};
use kclvm_parser::{load_program, ParseSession};
use kclvm_query::apply_overrides;
use kclvm_runtime::{PanicInfo, PlanOptions, ValueRef};
use kclvm_sema::resolver::resolve_program;
use kclvm_sema::resolver::{resolve_program, scope::ProgramScope};
use linker::Command;
pub use runner::ExecProgramArgs;
use runner::{ExecProgramResult, KclvmRunner, KclvmRunnerOptions};
Expand Down Expand Up @@ -194,7 +194,7 @@ pub fn execute(
) -> Result<String, String> {
// Resolve ast
let scope = resolve_program(&mut program);
scope.emit_diagnostics_to_string(sess.0.clone())?;
emit_compile_diag_to_string(sess, &scope)?;

// Create a temp entry file and the temp dir will be delete automatically
let temp_dir = tempdir().map_err(|e| e.to_string())?;
Expand Down Expand Up @@ -298,3 +298,28 @@ fn temp_file(dir: &str) -> Result<String> {
.ok_or(anyhow::anyhow!("{dir} not found"))?
.to_string())
}

// [`emit_compile_diag_to_string`] will emit compile diagnostics to string, including parsing and resolving diagnostics.
fn emit_compile_diag_to_string(
sess: Arc<ParseSession>,
scope: &ProgramScope,
) -> Result<(), String> {
let mut res_str = sess
.1
.borrow_mut()
.emit_to_string()
.map_err(|err| err.to_string())?;
let sema_err = scope.emit_diagnostics_to_string(sess.0.clone());
if sema_err.is_err() {
#[cfg(not(target_os = "windows"))]
res_str.push_str("\n");
#[cfg(target_os = "windows")]
res_str.push_str("\r\n");
res_str.push_str(&sema_err.unwrap_err());
}

res_str
.is_empty()
.then(|| Ok(()))
.unwrap_or_else(|| Err(res_str))
}

0 comments on commit 09cb631

Please sign in to comment.