Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle compiler errors in Rust. #2310

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -16,3 +16,6 @@ node_modules
/website/data.json
/website/recent.json
/js/gen
# cached for some of the tests
/js/deps/https/deno.land/std@v*/
/js/deps/https/raw.githubusercontent.com/
1 change: 1 addition & 0 deletions cli/BUILD.gn
Expand Up @@ -73,6 +73,7 @@ ts_sources = [
"../js/copy_file.ts",
"../js/core.ts",
"../js/custom_event.ts",
"../js/diagnostics.ts",
"../js/deno.ts",
"../js/dir.ts",
"../js/dispatch.ts",
Expand Down
26 changes: 22 additions & 4 deletions cli/ansi.rs
@@ -1,6 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use ansi_term::Color::Black;
use ansi_term::Color::Fixed;
use ansi_term::Color::Red;
use ansi_term::Color::White;
use ansi_term::Style;
use regex::Regex;
use std::env;
Expand All @@ -27,18 +29,34 @@ pub fn use_color() -> bool {
!(*NO_COLOR)
}

pub fn red_bold(s: String) -> impl fmt::Display {
pub fn italic_bold(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.bold().fg(Red);
style = style.italic().bold();
}
style.paint(s)
}

pub fn italic_bold(s: String) -> impl fmt::Display {
pub fn black_on_white(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.italic().bold();
style = style.on(White).fg(Black);
}
style.paint(s)
}

pub fn red(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.fg(Red);
}
style.paint(s)
}

pub fn grey(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.fg(Fixed(8));
}
style.paint(s)
}
Expand Down
28 changes: 19 additions & 9 deletions cli/compiler.rs
Expand Up @@ -7,9 +7,10 @@ use crate::tokio_util;
use crate::worker::Worker;
use deno::js_check;
use deno::Buf;
use deno::JSError;
use deno::Diagnostic;
use futures::Future;
use futures::Stream;
use serde_json;
use std::str;
use std::sync::atomic::Ordering;

Expand Down Expand Up @@ -87,7 +88,7 @@ pub fn compile_async(
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> impl Future<Item = ModuleMetaData, Error = JSError> {
) -> impl Future<Item = ModuleMetaData, Error = Diagnostic> {
debug!(
"Running rust part of compile_sync. specifier: {}, referrer: {}",
&specifier, &referrer
Expand Down Expand Up @@ -136,14 +137,23 @@ pub fn compile_async(
first_msg_fut
.map_err(|_| panic!("not handled"))
.and_then(move |maybe_msg: Option<Buf>| {
let _res_msg = maybe_msg.unwrap();

debug!("Received message from worker");

// TODO res is EmitResult, use serde_derive to parse it. Errors from the
// worker or Diagnostics should be somehow forwarded to the caller!
// Currently they are handled inside compiler.ts with os.exit(1) and above
// with std::process::exit(1). This bad.
if maybe_msg.is_some() {
let msg = &maybe_msg.unwrap();
let res_json = std::str::from_utf8(msg).unwrap();
debug!("Message: {}", res_json);
let res = serde_json::from_str::<serde_json::Value>(res_json)
.expect("Error decoding compiler response");
let diagnostics_o = res.get("diagnostics");
if let Some(diagnostics_v) = diagnostics_o {
if let Some(diagnostics) =
Diagnostic::from_compiler_json_value(diagnostics_v)
{
return Err(diagnostics);
}
}
}

let r = state.dir.fetch_module_meta_data(
&module_meta_data_.module_name,
Expand All @@ -169,7 +179,7 @@ pub fn compile_sync(
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> Result<ModuleMetaData, JSError> {
) -> Result<ModuleMetaData, Diagnostic> {
tokio_util::block_on(compile_async(
state,
specifier,
Expand Down
2 changes: 1 addition & 1 deletion cli/deno_dir.rs
@@ -1,12 +1,12 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::compiler::ModuleMetaData;
use crate::diagnostics::SourceMapGetter;
use crate::errors;
use crate::errors::DenoError;
use crate::errors::DenoResult;
use crate::errors::ErrorKind;
use crate::fs as deno_fs;
use crate::http_util;
use crate::js_errors::SourceMapGetter;
use crate::msg;
use crate::progress::Progress;
use crate::tokio_util;
Expand Down