Skip to content

Commit

Permalink
fix(linter) Report error instead of panicing if the file fails to open
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Oct 29, 2023
1 parent bd34dc7 commit 36e4155
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ use thiserror::Error;
#[error("File is too long to fit on the screen")]
#[diagnostic(help("{0:?} seems like a minified file"))]
pub struct MinifiedFileError(pub PathBuf);

#[derive(Debug, Error, Diagnostic)]
#[error("Failed to open file")]
#[diagnostic(help("Failed to open file {0:?} with error \"{1}\""))]
pub struct FailedToOpenFileError(pub PathBuf, pub std::io::Error);
16 changes: 13 additions & 3 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rayon::{iter::ParallelBridge, prelude::ParallelIterator};
use rustc_hash::FxHashSet;

use oxc_allocator::Allocator;
use oxc_diagnostics::{DiagnosticSender, DiagnosticService};
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, Error, FailedToOpenFileError};
use oxc_parser::Parser;
use oxc_resolver::{ResolveOptions, Resolver};
use oxc_semantic::{ModuleRecord, SemanticBuilder};
Expand Down Expand Up @@ -140,8 +140,18 @@ impl Runtime {
}

let allocator = Allocator::default();
let source_text =
fs::read_to_string(path).unwrap_or_else(|_| panic!("Failed to read {path:?}"));
let source_text = match fs::read_to_string(path) {
Ok(source_text) => source_text,
Err(e) => {
tx_error
.send(Some((
path.to_path_buf(),
vec![Error::new(FailedToOpenFileError(path.to_path_buf(), e))],
)))
.unwrap();
return;
}
};

let mut messages =
self.process_source(path, &allocator, &source_text, source_type, true, tx_error);
Expand Down

0 comments on commit 36e4155

Please sign in to comment.