diff --git a/crates/oxc_resolver/src/error.rs b/crates/oxc_resolver/src/error.rs index 6dc108c61..2292ef9fb 100644 --- a/crates/oxc_resolver/src/error.rs +++ b/crates/oxc_resolver/src/error.rs @@ -129,6 +129,13 @@ impl PartialEq for IOError { } } +impl From for std::io::Error { + fn from(error: IOError) -> Self { + let io_error = error.0.as_ref(); + Self::new(io_error.kind(), io_error.to_string()) + } +} + impl From for ResolveError { fn from(err: io::Error) -> Self { Self::IOError(IOError(Arc::new(err))) diff --git a/crates/oxc_resolver/src/tests/imports_field.rs b/crates/oxc_resolver/src/tests/imports_field.rs index be6f371f1..79d943fc1 100644 --- a/crates/oxc_resolver/src/tests/imports_field.rs +++ b/crates/oxc_resolver/src/tests/imports_field.rs @@ -5,7 +5,7 @@ use serde_json::json; use crate::{MatchObject, PathUtil, ResolveContext, ResolveError, ResolveOptions, Resolver}; -use std::path::Path; +use std::{io::ErrorKind, path::Path}; #[test] fn test() { @@ -1296,3 +1296,17 @@ fn test_cases() { } } } + +#[test] +fn test_into_io_error() { + let error_string = "IOError occurred"; + let string_error = std::io::Error::new(ErrorKind::Interrupted, error_string.to_string()); + let resolve_io_error: ResolveError = string_error.into(); + + if let ResolveError::IOError(io_error) = resolve_io_error { + // fix for https://github.com/web-infra-dev/rspack/issues/4564 + let std_io_error: std::io::Error = io_error.into(); + assert_eq!(std_io_error.kind(), ErrorKind::Interrupted); + assert_eq!(std_io_error.to_string(), error_string); + } +}