summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-05-10 20:06:59 -0400
committerGitHub <noreply@github.com>2023-05-10 20:06:59 -0400
commit28aa489de9cd4f995ec2fc02e2c9d224e89f4c01 (patch)
treeb316937a47fe9c8f9f6768bc13b9a686c07cf42f /runtime
parent5fd74bfa1c5ed514c3e19fdb2e8590fe251d3ee6 (diff)
feat(compile): unstable npm and node specifier support (#19005)
This is the initial support for npm and node specifiers in `deno compile`. The npm packages are included in the binary and read from it via a virtual file system. This also supports the `--node-modules-dir` flag, dependencies specified in a package.json, and npm binary commands (ex. `deno compile --unstable npm:cowsay`) Closes #16632
Diffstat (limited to 'runtime')
-rw-r--r--runtime/build.rs1
-rw-r--r--runtime/clippy.toml45
-rw-r--r--runtime/examples/hello_runtime.rs6
-rw-r--r--runtime/fs_util.rs25
-rw-r--r--runtime/ops/os/mod.rs1
-rw-r--r--runtime/ops/os/sys_info.rs1
6 files changed, 62 insertions, 17 deletions
diff --git a/runtime/build.rs b/runtime/build.rs
index 412257f12..bd141d297 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -358,6 +358,7 @@ fn main() {
if env::var_os("DOCS_RS").is_some() {
let snapshot_slice = &[];
#[allow(clippy::needless_borrow)]
+ #[allow(clippy::disallowed_methods)]
std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap();
}
diff --git a/runtime/clippy.toml b/runtime/clippy.toml
new file mode 100644
index 000000000..53676a90e
--- /dev/null
+++ b/runtime/clippy.toml
@@ -0,0 +1,45 @@
+disallowed-methods = [
+ { path = "std::env::current_dir", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::path::Path::canonicalize", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::is_dir", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::is_file", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::is_symlink", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::metadata", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::read_dir", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::read_link", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::symlink_metadata", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::Path::try_exists", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::exists", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::canonicalize", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::is_dir", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::is_file", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::is_symlink", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::metadata", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::read_dir", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::read_link", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::symlink_metadata", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::path::PathBuf::try_exists", reason = "File system operations should be done using NodeFs trait" },
+ { path = "std::env::set_current_dir", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::env::temp_dir", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::canonicalize", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::copy", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::create_dir_all", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::create_dir", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::DirBuilder::new", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::hard_link", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::metadata", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::OpenOptions::new", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::read_dir", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::read_link", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::read_to_string", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::read", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::remove_dir_all", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::remove_dir", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::remove_file", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::rename", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::set_permissions", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::symlink_metadata", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::fs::write", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::path::Path::canonicalize", reason = "File system operations should be done using FileSystem trait" },
+ { path = "std::path::Path::exists", reason = "File system operations should be done using FileSystem trait" },
+]
diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs
index 157a200f4..2bc371b68 100644
--- a/runtime/examples/hello_runtime.rs
+++ b/runtime/examples/hello_runtime.rs
@@ -2,6 +2,7 @@
use deno_core::error::AnyError;
use deno_core::FsModuleLoader;
+use deno_core::ModuleSpecifier;
use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
@@ -14,10 +15,7 @@ deno_core::extension!(hello_runtime, esm = ["hello_runtime_bootstrap.js"]);
async fn main() -> Result<(), AnyError> {
let js_path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js");
- let main_module = deno_core::resolve_path(
- &js_path.to_string_lossy(),
- &std::env::current_dir()?,
- )?;
+ let main_module = ModuleSpecifier::from_file_path(js_path).unwrap();
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
PermissionsContainer::allow_all(),
diff --git a/runtime/fs_util.rs b/runtime/fs_util.rs
index eb4a2f899..204b0e4e8 100644
--- a/runtime/fs_util.rs
+++ b/runtime/fs_util.rs
@@ -3,23 +3,17 @@
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
pub use deno_core::normalize_path;
-use std::env::current_dir;
-use std::io::Error;
use std::path::Path;
use std::path::PathBuf;
-/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows.
-pub fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
- Ok(deno_core::strip_unc_prefix(path.canonicalize()?))
-}
-
#[inline]
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
if path.is_absolute() {
Ok(normalize_path(path))
} else {
- let cwd =
- current_dir().context("Failed to get current working directory")?;
+ #[allow(clippy::disallowed_methods)]
+ let cwd = std::env::current_dir()
+ .context("Failed to get current working directory")?;
Ok(normalize_path(cwd.join(path)))
}
}
@@ -28,21 +22,26 @@ pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
mod tests {
use super::*;
+ fn current_dir() -> PathBuf {
+ #[allow(clippy::disallowed_methods)]
+ std::env::current_dir().unwrap()
+ }
+
#[test]
fn resolve_from_cwd_child() {
- let cwd = current_dir().unwrap();
+ let cwd = current_dir();
assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a"));
}
#[test]
fn resolve_from_cwd_dot() {
- let cwd = current_dir().unwrap();
+ let cwd = current_dir();
assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd);
}
#[test]
fn resolve_from_cwd_parent() {
- let cwd = current_dir().unwrap();
+ let cwd = current_dir();
assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd);
}
@@ -66,7 +65,7 @@ mod tests {
#[test]
fn resolve_from_cwd_absolute() {
let expected = Path::new("a");
- let cwd = current_dir().unwrap();
+ let cwd = current_dir();
let absolute_expected = cwd.join(expected);
assert_eq!(resolve_from_cwd(expected).unwrap(), absolute_expected);
}
diff --git a/runtime/ops/os/mod.rs b/runtime/ops/os/mod.rs
index 911cd327c..b997a89d9 100644
--- a/runtime/ops/os/mod.rs
+++ b/runtime/ops/os/mod.rs
@@ -339,6 +339,7 @@ fn rss() -> usize {
(out, idx)
}
+ #[allow(clippy::disallowed_methods)]
let statm_content = if let Ok(c) = std::fs::read_to_string("/proc/self/statm")
{
c
diff --git a/runtime/ops/os/sys_info.rs b/runtime/ops/os/sys_info.rs
index 1a9358dc0..795e6bb0a 100644
--- a/runtime/ops/os/sys_info.rs
+++ b/runtime/ops/os/sys_info.rs
@@ -48,6 +48,7 @@ pub fn loadavg() -> LoadAvg {
pub fn os_release() -> String {
#[cfg(target_os = "linux")]
{
+ #[allow(clippy::disallowed_methods)]
match std::fs::read_to_string("/proc/sys/kernel/osrelease") {
Ok(mut s) => {
s.pop(); // pop '\n'