1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor: update to use deno_package_json (#24688)

This is in preparation for extracting out node resolution code from
ext/node (which is something I'm going to do gradually over time).

Uses https://github.com/denoland/deno_package_json
This commit is contained in:
David Sherret 2024-07-23 17:34:46 -04:00 committed by GitHub
parent f0df54fc70
commit 6055629ee7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 123 additions and 101 deletions

23
Cargo.lock generated
View file

@ -1131,6 +1131,7 @@ dependencies = [
"deno_lint",
"deno_lockfile",
"deno_npm",
"deno_package_json",
"deno_runtime",
"deno_semver",
"deno_task_shell",
@ -1312,11 +1313,12 @@ dependencies = [
[[package]]
name = "deno_config"
version = "0.25.0"
version = "0.26.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64e7a65603a3aa52216021a0abd401770ac6ed284e5b885dadf40ce35b2a0d9e"
checksum = "8188c39699541affc0c0f89bbba07d31212385fa0c4e1a5a9e530b0f3cbc776f"
dependencies = [
"anyhow",
"deno_package_json",
"deno_semver",
"glob",
"ignore",
@ -1513,7 +1515,6 @@ version = "0.72.0"
dependencies = [
"async-trait",
"base32",
"deno_config",
"deno_core",
"deno_io",
"deno_permissions",
@ -1733,13 +1734,13 @@ dependencies = [
"cbc",
"const-oid",
"data-encoding",
"deno_config",
"deno_core",
"deno_fetch",
"deno_fs",
"deno_io",
"deno_media_type",
"deno_net",
"deno_package_json",
"deno_permissions",
"deno_whoami",
"digest",
@ -1831,6 +1832,20 @@ dependencies = [
"thiserror",
]
[[package]]
name = "deno_package_json"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38cf6ea5cc98ea7ad58b0e84593773bea03fc0431071a296017bed4151e3dc1d"
dependencies = [
"deno_semver",
"indexmap",
"serde",
"serde_json",
"thiserror",
"url",
]
[[package]]
name = "deno_permissions"
version = "0.22.0"

View file

@ -101,7 +101,7 @@ console_static_text = "=0.8.1"
data-encoding = "2.3.3"
data-url = "=0.3.0"
deno_cache_dir = "=0.10.2"
deno_config = { version = "=0.25.0", default-features = false }
deno_package_json = { version = "=0.1.1", default-features = false }
dlopen2 = "0.6.1"
ecb = "=0.1.2"
elliptic-curve = { version = "0.13.4", features = ["alloc", "arithmetic", "ecdh", "std", "pem"] }

View file

@ -65,7 +65,7 @@ winres.workspace = true
[dependencies]
deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_cache_dir = { workspace = true }
deno_config = { workspace = true, features = ["workspace"] }
deno_config = { version = "=0.26.1", features = ["workspace", "sync"] }
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = { version = "=0.142.0", features = ["html", "syntect"] }
deno_emit = "=0.43.1"
@ -73,6 +73,7 @@ deno_graph = { version = "=0.80.1", features = ["tokio_executor"] }
deno_lint = { version = "=0.60.1", features = ["docs"] }
deno_lockfile.workspace = true
deno_npm = "=0.21.4"
deno_package_json.workspace = true
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_semver = "=0.5.7"
deno_task_shell = "=0.17.0"

View file

@ -7,6 +7,62 @@ use deno_semver::jsr::JsrDepPackageReq;
use deno_semver::jsr::JsrPackageReqReference;
use deno_semver::npm::NpmPackageReqReference;
#[cfg(test)] // happens to only be used by the tests at the moment
pub struct DenoConfigFsAdapter<'a>(
pub &'a dyn deno_runtime::deno_fs::FileSystem,
);
#[cfg(test)]
impl<'a> deno_config::fs::DenoConfigFs for DenoConfigFsAdapter<'a> {
fn read_to_string_lossy(
&self,
path: &std::path::Path,
) -> Result<String, std::io::Error> {
self
.0
.read_text_file_lossy_sync(path, None)
.map_err(|err| err.into_io_error())
}
fn stat_sync(
&self,
path: &std::path::Path,
) -> Result<deno_config::fs::FsMetadata, std::io::Error> {
self
.0
.stat_sync(path)
.map(|stat| deno_config::fs::FsMetadata {
is_file: stat.is_file,
is_directory: stat.is_directory,
is_symlink: stat.is_symlink,
})
.map_err(|err| err.into_io_error())
}
fn read_dir(
&self,
path: &std::path::Path,
) -> Result<Vec<deno_config::fs::FsDirEntry>, std::io::Error> {
self
.0
.read_dir_sync(path)
.map_err(|err| err.into_io_error())
.map(|entries| {
entries
.into_iter()
.map(|e| deno_config::fs::FsDirEntry {
path: path.join(e.name),
metadata: deno_config::fs::FsMetadata {
is_file: e.is_file,
is_directory: e.is_directory,
is_symlink: e.is_symlink,
},
})
.collect()
})
}
}
pub fn deno_json_deps(
config: &deno_config::deno_json::ConfigFile,
) -> HashSet<JsrDepPackageReq> {

View file

@ -25,8 +25,6 @@ use deno_npm::npm_rc::NpmRc;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
use deno_npm::NpmSystemInfo;
use deno_runtime::deno_fs::DenoConfigFsAdapter;
use deno_runtime::deno_fs::RealFs;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_semver::npm::NpmPackageReqReference;
@ -835,7 +833,6 @@ impl CliOptions {
pub fn from_flags(flags: Flags) -> Result<Self, AnyError> {
let initial_cwd =
std::env::current_dir().with_context(|| "Failed getting cwd.")?;
let config_fs_adapter = DenoConfigFsAdapter::new(&RealFs);
let maybe_vendor_override = flags.vendor.map(|v| match v {
true => VendorEnablement::Enable { cwd: &initial_cwd },
false => VendorEnablement::Disable,
@ -860,7 +857,7 @@ impl CliOptions {
log::debug!("package.json auto-discovery is disabled");
}
WorkspaceDiscoverOptions {
fs: &config_fs_adapter,
fs: Default::default(), // use real fs
deno_json_cache: None,
pkg_json_cache: Some(
&deno_runtime::deno_node::PackageJsonThreadLocalCache,

View file

@ -3,8 +3,8 @@
use std::path::PathBuf;
use std::sync::Arc;
use deno_config::package_json::PackageJsonDepValue;
use deno_config::workspace::Workspace;
use deno_package_json::PackageJsonDepValue;
use deno_semver::package::PackageReq;
#[derive(Debug)]

View file

@ -55,12 +55,12 @@ use std::collections::BTreeSet;
use std::path::PathBuf;
use deno_config::deno_json::ConfigFile;
use deno_config::package_json::PackageJsonDepValue;
use deno_config::workspace::PackageJsonDepResolution;
use deno_config::workspace::WorkspaceResolver;
use deno_core::error::AnyError;
use deno_core::futures::FutureExt;
use deno_core::FeatureChecker;
use deno_package_json::PackageJsonDepValue;
use deno_lockfile::WorkspaceMemberConfig;
use deno_runtime::deno_fs;

View file

@ -11,7 +11,6 @@ use deno_config::fs::DenoConfigFs;
use deno_config::fs::RealDenoConfigFs;
use deno_config::glob::FilePatterns;
use deno_config::glob::PathOrPatternSet;
use deno_config::package_json::PackageJsonCache;
use deno_config::workspace::CreateResolverOptions;
use deno_config::workspace::PackageJsonDepResolution;
use deno_config::workspace::SpecifiedImportMap;
@ -34,6 +33,7 @@ use deno_core::serde_json::Value;
use deno_core::ModuleSpecifier;
use deno_lint::linter::LintConfig as DenoLintConfig;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_package_json::PackageJsonCache;
use deno_runtime::deno_node::PackageJson;
use deno_runtime::deno_permissions::PermissionsContainer;
use deno_runtime::fs_util::specifier_to_file_path;
@ -1560,9 +1560,7 @@ impl ConfigData {
self.member_dir.maybe_deno_json()
}
pub fn maybe_pkg_json(
&self,
) -> Option<&Arc<deno_config::package_json::PackageJson>> {
pub fn maybe_pkg_json(&self) -> Option<&Arc<deno_package_json::PackageJson>> {
self.member_dir.maybe_pkg_json()
}
}
@ -1793,7 +1791,7 @@ impl ConfigTree {
&config_path,
),
&deno_config::workspace::WorkspaceDiscoverOptions {
fs: &deno_runtime::deno_fs::DenoConfigFsAdapter::new(&test_fs),
fs: &crate::args::deno_json::DenoConfigFsAdapter(&test_fs),
..Default::default()
},
)
@ -1888,7 +1886,7 @@ impl deno_config::deno_json::DenoJsonCache for DenoJsonMemCache {
#[derive(Default)]
struct PackageJsonMemCache(Mutex<HashMap<PathBuf, Arc<PackageJson>>>);
impl deno_config::package_json::PackageJsonCache for PackageJsonMemCache {
impl deno_package_json::PackageJsonCache for PackageJsonMemCache {
fn get(&self, path: &Path) -> Option<Arc<PackageJson>> {
self.0.lock().get(path).cloned()
}

View file

@ -15,7 +15,6 @@ use deno_core::url;
use deno_core::ModuleSpecifier;
use deno_graph::GraphKind;
use deno_graph::Resolution;
use deno_runtime::deno_fs::DenoConfigFsAdapter;
use deno_runtime::deno_tls::rustls::RootCertStore;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_semver::jsr::JsrPackageReqReference;
@ -3539,7 +3538,7 @@ impl Inner {
initial_cwd.clone()
]),
&WorkspaceDiscoverOptions {
fs: &DenoConfigFsAdapter::new(&deno_runtime::deno_fs::RealFs),
fs: Default::default(), // use real fs,
deno_json_cache: None,
pkg_json_cache: None,
workspace_cache: None,

View file

@ -6,10 +6,10 @@ use std::path::PathBuf;
use std::sync::Arc;
use deno_ast::ModuleSpecifier;
use deno_config::package_json::PackageJsonDepValue;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_package_json::PackageJsonDepValue;
use deno_runtime::deno_fs::FileSystem;
use deno_runtime::deno_node::errors::PackageFolderResolveError;
use deno_runtime::deno_node::errors::PackageFolderResolveErrorKind;

View file

@ -4,7 +4,6 @@ use async_trait::async_trait;
use dashmap::DashMap;
use dashmap::DashSet;
use deno_ast::MediaType;
use deno_config::package_json::PackageJsonDepValue;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::MappedResolutionError;
use deno_config::workspace::WorkspaceResolver;
@ -21,6 +20,7 @@ use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
use deno_graph::NpmLoadError;
use deno_graph::NpmResolvePkgReqsResult;
use deno_npm::resolution::NpmResolutionError;
use deno_package_json::PackageJsonDepValue;
use deno_runtime::deno_fs;
use deno_runtime::deno_fs::FileSystem;
use deno_runtime::deno_node::errors::ClosestPkgJsonError;

View file

@ -34,7 +34,6 @@ use crate::worker::CliMainWorkerOptions;
use crate::worker::ModuleLoaderAndSourceMapGetter;
use crate::worker::ModuleLoaderFactory;
use deno_ast::MediaType;
use deno_config::package_json::PackageJsonDepValue;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::MappedResolutionError;
use deno_config::workspace::WorkspaceResolver;
@ -52,6 +51,7 @@ use deno_core::ModuleType;
use deno_core::RequestedModuleType;
use deno_core::ResolutionKind;
use deno_npm::npm_rc::ResolvedNpmRc;
use deno_package_json::PackageJsonDepValue;
use deno_runtime::deno_fs;
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
use deno_runtime::deno_node::NodeResolutionMode;
@ -591,7 +591,7 @@ pub async fn run(
.to_file_path()
.unwrap();
let pkg_json =
deno_config::package_json::PackageJson::load_from_value(path, json);
deno_package_json::PackageJson::load_from_value(path, json);
Arc::new(pkg_json)
})
.collect();

View file

@ -3,7 +3,6 @@
use deno_ast::ParsedSource;
use deno_ast::SourceRange;
use deno_ast::SourceTextInfo;
use deno_config::package_json::PackageJsonDepValue;
use deno_config::workspace::MappedResolution;
use deno_config::workspace::PackageJsonDepResolution;
use deno_config::workspace::WorkspaceResolver;
@ -12,6 +11,7 @@ use deno_graph::DependencyDescriptor;
use deno_graph::DynamicTemplatePart;
use deno_graph::ParserModuleAnalyzer;
use deno_graph::TypeScriptReference;
use deno_package_json::PackageJsonDepValue;
use deno_runtime::deno_node::is_builtin_node_module;
use crate::resolver::SloppyImportsResolver;

View file

@ -14,12 +14,11 @@ description = "Ops for interacting with the file system"
path = "lib.rs"
[features]
sync_fs = ["deno_config/sync"]
sync_fs = []
[dependencies]
async-trait.workspace = true
base32.workspace = true
deno_config = { workspace = true, default-features = false }
deno_core.workspace = true
deno_io.workspace = true
deno_permissions.workspace = true

View file

@ -337,64 +337,6 @@ pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
}
}
pub struct DenoConfigFsAdapter<'a>(&'a dyn FileSystem);
impl<'a> DenoConfigFsAdapter<'a> {
pub fn new(fs: &'a dyn FileSystem) -> Self {
Self(fs)
}
}
impl<'a> deno_config::fs::DenoConfigFs for DenoConfigFsAdapter<'a> {
fn read_to_string_lossy(
&self,
path: &Path,
) -> Result<String, std::io::Error> {
self
.0
.read_text_file_lossy_sync(path, None)
.map_err(|err| err.into_io_error())
}
fn stat_sync(
&self,
path: &Path,
) -> Result<deno_config::fs::FsMetadata, std::io::Error> {
self
.0
.stat_sync(path)
.map(|stat| deno_config::fs::FsMetadata {
is_file: stat.is_file,
is_directory: stat.is_directory,
is_symlink: stat.is_symlink,
})
.map_err(|err| err.into_io_error())
}
fn read_dir(
&self,
path: &Path,
) -> Result<Vec<deno_config::fs::FsDirEntry>, std::io::Error> {
self
.0
.read_dir_sync(path)
.map_err(|err| err.into_io_error())
.map(|entries| {
entries
.into_iter()
.map(|e| deno_config::fs::FsDirEntry {
path: path.join(e.name),
metadata: deno_config::fs::FsMetadata {
is_file: e.is_file,
is_directory: e.is_directory,
is_symlink: e.is_symlink,
},
})
.collect()
})
}
}
// Like String::from_utf8_lossy but operates on owned values
#[inline(always)]
fn string_from_utf8_lossy(buf: Vec<u8>) -> String {

View file

@ -9,7 +9,6 @@ pub mod sync;
pub use crate::in_memory_fs::InMemoryFs;
pub use crate::interface::AccessCheckCb;
pub use crate::interface::AccessCheckFn;
pub use crate::interface::DenoConfigFsAdapter;
pub use crate::interface::FileSystem;
pub use crate::interface::FileSystemRc;
pub use crate::interface::FsDirEntry;

View file

@ -13,6 +13,9 @@ description = "Node compatibility for Deno"
[lib]
path = "lib.rs"
[features]
sync_fs = ["deno_package_json/sync"]
[dependencies]
aead-gcm-stream = "0.1"
aes.workspace = true
@ -23,13 +26,13 @@ bytes.workspace = true
cbc.workspace = true
const-oid = "0.9.5"
data-encoding.workspace = true
deno_config = { workspace = true, default-features = false, features = ["package_json"] }
deno_core.workspace = true
deno_fetch.workspace = true
deno_fs.workspace = true
deno_io.workspace = true
deno_media_type.workspace = true
deno_net.workspace = true
deno_package_json.workspace = true
deno_permissions.workspace = true
deno_whoami = "0.1.0"
digest = { version = "0.10.5", features = ["core-api", "std"] }

View file

@ -315,7 +315,7 @@ impl NodeJsErrorCoded for PathToDeclarationUrlError {
pub struct PackageJsonLoadError(
#[source]
#[from]
pub deno_config::package_json::PackageJsonLoadError,
pub deno_package_json::PackageJsonLoadError,
);
impl NodeJsErrorCoded for PackageJsonLoadError {

View file

@ -32,7 +32,7 @@ mod path;
mod polyfill;
mod resolution;
pub use deno_config::package_json::PackageJson;
pub use deno_package_json::PackageJson;
pub use ops::ipc::ChildPipeFd;
pub use ops::ipc::IpcJsonStreamResource;
use ops::vm;

View file

@ -1,8 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_config::package_json::PackageJson;
use deno_config::package_json::PackageJsonRc;
use deno_fs::DenoConfigFsAdapter;
use deno_package_json::PackageJson;
use deno_package_json::PackageJsonRc;
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::ErrorKind;
@ -24,9 +23,7 @@ impl PackageJsonThreadLocalCache {
}
}
impl deno_config::package_json::PackageJsonCache
for PackageJsonThreadLocalCache
{
impl deno_package_json::PackageJsonCache for PackageJsonThreadLocalCache {
fn get(&self, path: &Path) -> Option<PackageJsonRc> {
CACHE.with(|cache| cache.borrow().get(path).cloned())
}
@ -36,6 +33,20 @@ impl deno_config::package_json::PackageJsonCache
}
}
pub struct DenoPkgJsonFsAdapter<'a>(pub &'a dyn deno_fs::FileSystem);
impl<'a> deno_package_json::fs::DenoPkgJsonFs for DenoPkgJsonFsAdapter<'a> {
fn read_to_string_lossy(
&self,
path: &Path,
) -> Result<String, std::io::Error> {
self
.0
.read_text_file_lossy_sync(path, None)
.map_err(|err| err.into_io_error())
}
}
/// Helper to load a package.json file using the thread local cache
/// in deno_node.
pub fn load_pkg_json(
@ -44,14 +55,16 @@ pub fn load_pkg_json(
) -> Result<Option<PackageJsonRc>, PackageJsonLoadError> {
let result = PackageJson::load_from_path(
path,
&DenoConfigFsAdapter::new(fs),
&DenoPkgJsonFsAdapter(fs),
Some(&PackageJsonThreadLocalCache),
);
match result {
Ok(pkg_json) => Ok(Some(pkg_json)),
Err(deno_config::package_json::PackageJsonLoadError::Io {
source, ..
}) if source.kind() == ErrorKind::NotFound => Ok(None),
Err(deno_package_json::PackageJsonLoadError::Io { source, .. })
if source.kind() == ErrorKind::NotFound =>
{
Ok(None)
}
Err(err) => Err(PackageJsonLoadError(err)),
}
}

View file

@ -5,7 +5,6 @@ use std::collections::HashMap;
use std::path::Path;
use std::path::PathBuf;
use deno_config::package_json::PackageJsonRc;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::serde_json::Map;
@ -14,6 +13,7 @@ use deno_core::url::Url;
use deno_core::ModuleSpecifier;
use deno_fs::FileSystemRc;
use deno_media_type::MediaType;
use deno_package_json::PackageJsonRc;
use crate::errors;
use crate::errors::CanonicalizingPkgJsonDirError;
@ -56,7 +56,7 @@ use crate::PathClean;
pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"];
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
pub type NodeModuleKind = deno_config::package_json::NodeModuleKind;
pub type NodeModuleKind = deno_package_json::NodeModuleKind;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeResolutionMode {