Skip to content

Commit 760a9f9

Browse files
committed
fix(linter): report errors when writing to the filesystem (#22881)
`oxlint --fix` panicked when a filesystem write failed while applying fixes. With this change, write failures are reported as diagnostics instead of unwrapping the result. fixes #22858
1 parent e4b1f46 commit 760a9f9

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

crates/oxc_linter/src/service/runtime.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,15 @@ impl Runtime {
718718

719719
// If the new source text is owned, that means it was modified,
720720
// so we write the new source text to the file.
721-
if let Cow::Owned(new_source_text) = &new_source_text {
722-
file_system.write_file(path, new_source_text).unwrap();
721+
if let Cow::Owned(new_source_text) = &new_source_text
722+
&& let Err(error) = file_system.write_file(path, new_source_text)
723+
{
724+
tx_error
725+
.send(vec![Error::new(OxcDiagnostic::error(format!(
726+
"Failed to write file {} with error \"{error}\"",
727+
path.display()
728+
)))])
729+
.unwrap();
723730
}
724731
});
725732
},

crates/oxc_linter/src/tsgolint.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use oxc_allocator::Allocator;
1212
use rustc_hash::FxHashMap;
1313
use serde::{Deserialize, Serialize};
1414

15-
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, OxcDiagnostic, Severity};
15+
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, Error, OxcDiagnostic, Severity};
1616
use oxc_span::{SourceType, Span};
1717

1818
use super::{AllowWarnDeny, ConfigStore, DisableDirectives, ResolvedLinterState, read_to_string};
@@ -257,10 +257,15 @@ impl TsGoLintState {
257257
.map(|st| if st.is_javascript() { st.with_jsx(true) } else { st });
258258
let fix_result = Fixer::new(&source_text, messages, source_type).fix();
259259

260-
if fix_result.fixed {
261-
file_system
262-
.write_file(&path, &fix_result.fixed_code)
263-
.expect("Failed to write fixed file");
260+
if fix_result.fixed
261+
&& let Err(error) = file_system.write_file(&path, &fix_result.fixed_code)
262+
{
263+
sender_for_fixes
264+
.send(vec![Error::new(OxcDiagnostic::error(format!(
265+
"Failed to write file {} with error \"{error}\"",
266+
path.display()
267+
)))])
268+
.expect("Failed to send diagnostics");
264269
}
265270

266271
if fix_result.messages.is_empty() {

0 commit comments

Comments
 (0)