mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(resolver): return package json error immediately instead of saving it (#702)
The error is propagated so there is no need to save it.
This commit is contained in:
parent
89b49bdb07
commit
91fd375a3b
4 changed files with 21 additions and 19 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1756,6 +1756,7 @@ dependencies = [
|
|||
"jemallocator",
|
||||
"mimalloc",
|
||||
"nodejs-resolver",
|
||||
"once_cell",
|
||||
"rayon",
|
||||
"rustc-hash",
|
||||
"serde",
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ rustc-hash = { workspace = true }
|
|||
indexmap = { workspace = true, features = ["serde"] }
|
||||
dunce = "1.0.4"
|
||||
identity-hash = "0.1.0"
|
||||
# Use `std::sync::OnceLock::get_or_try_init` when it is stable.
|
||||
once_cell = "1.18.0"
|
||||
|
||||
[dev-dependencies]
|
||||
static_assertions = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
use once_cell::sync::OnceCell as OnceLock;
|
||||
use std::{
|
||||
convert::AsRef,
|
||||
hash::{Hash, Hasher},
|
||||
ops::Deref,
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, OnceLock},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use dashmap::DashMap;
|
||||
|
|
@ -68,7 +69,7 @@ pub struct CachedPathImpl {
|
|||
parent: Option<CachedPath>,
|
||||
meta: OnceLock<Option<FileMetadata>>,
|
||||
symlink: OnceLock<Option<PathBuf>>,
|
||||
package_json: OnceLock<Option<Result<Arc<PackageJson>, ResolveError>>>,
|
||||
package_json: OnceLock<Option<Arc<PackageJson>>>,
|
||||
}
|
||||
|
||||
impl CachedPathImpl {
|
||||
|
|
@ -124,7 +125,7 @@ impl CachedPathImpl {
|
|||
}
|
||||
let mut cache_value = Some(cache_value);
|
||||
while let Some(cv) = cache_value {
|
||||
if let Some(package_json) = cv.package_json(fs).transpose()? {
|
||||
if let Some(package_json) = cv.package_json(fs)? {
|
||||
return Ok(Some(Arc::clone(&package_json)));
|
||||
}
|
||||
cache_value = cv.parent.as_deref();
|
||||
|
|
@ -140,19 +141,19 @@ impl CachedPathImpl {
|
|||
pub fn package_json<Fs: FileSystem>(
|
||||
&self,
|
||||
fs: &Fs,
|
||||
) -> Option<Result<Arc<PackageJson>, ResolveError>> {
|
||||
// Change to `get_or_try_init` once it is stable
|
||||
) -> Result<Option<Arc<PackageJson>>, ResolveError> {
|
||||
// Change to `std::sync::OnceLock::get_or_try_init` when it is stable.
|
||||
self.package_json
|
||||
.get_or_init(|| {
|
||||
.get_or_try_init(|| {
|
||||
let package_json_path = self.path.join("package.json");
|
||||
fs.read_to_string(&package_json_path).ok().map(|package_json_string| {
|
||||
PackageJson::parse(package_json_path.clone(), &package_json_string)
|
||||
.map(Arc::new)
|
||||
.map_err(|error| {
|
||||
ResolveError::from_serde_json_error(package_json_path, &error)
|
||||
})
|
||||
})
|
||||
let Ok(package_json_string) = fs.read_to_string(&package_json_path) else {
|
||||
return Ok(None)
|
||||
};
|
||||
PackageJson::parse(package_json_path.clone(), &package_json_string)
|
||||
.map(Arc::new)
|
||||
.map(Some)
|
||||
.map_err(|error| ResolveError::from_serde_json_error(package_json_path, &error))
|
||||
})
|
||||
.clone()
|
||||
.cloned()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -435,7 +435,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
// 1. If X/package.json is a file,
|
||||
if !self.options.description_files.is_empty() {
|
||||
// a. Parse X/package.json, and look for "main" field.
|
||||
if let Some(package_json) = cached_path.package_json(&self.cache.fs).transpose()? {
|
||||
if let Some(package_json) = cached_path.package_json(&self.cache.fs)? {
|
||||
// b. If "main" is a falsy value, GOTO 2.
|
||||
if let Some(main_field) = &package_json.main {
|
||||
// c. let M = X + (json main field)
|
||||
|
|
@ -512,7 +512,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
// return.
|
||||
let (name, subpath) = Self::parse_package_specifier(specifier);
|
||||
let cached_path = self.cache.value(&path.join(name));
|
||||
let Some(package_json) = cached_path.package_json(&self.cache.fs).transpose()? else {
|
||||
let Some(package_json) = cached_path.package_json(&self.cache.fs)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
// 3. Parse DIR/NAME/package.json, and look for "exports" field.
|
||||
|
|
@ -705,9 +705,7 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
// 1. Continue the next loop iteration.
|
||||
if cached_path.is_dir(&self.cache.fs) {
|
||||
// 4. Let pjson be the result of READ_PACKAGE_JSON(packageURL).
|
||||
if let Some(package_json) =
|
||||
cached_path.package_json(&self.cache.fs).transpose()?
|
||||
{
|
||||
if let Some(package_json) = cached_path.package_json(&self.cache.fs)? {
|
||||
// 5. If pjson is not null and pjson.exports is not null or undefined, then
|
||||
if !package_json.exports.is_none() {
|
||||
// 1. Return the result of PACKAGE_EXPORTS_RESOLVE(packageURL, packageSubpath, pjson.exports, defaultConditions).
|
||||
|
|
|
|||
Loading…
Reference in a new issue