summaryrefslogtreecommitdiff
path: root/cli/main.rs
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /cli/main.rs
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'cli/main.rs')
-rw-r--r--cli/main.rs148
1 files changed, 36 insertions, 112 deletions
diff --git a/cli/main.rs b/cli/main.rs
index ddb6078af..7d3ef0e6a 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -15,7 +15,6 @@ mod js;
mod jsr;
mod lsp;
mod module_loader;
-mod napi;
mod node;
mod npm;
mod ops;
@@ -38,6 +37,7 @@ use crate::util::v8::init_v8_flags;
use args::TaskFlags;
use deno_resolver::npm::ByonmResolvePkgFolderFromDenoReqError;
+use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
use deno_runtime::WorkerExecutionMode;
pub use deno_runtime::UNSTABLE_GRANULAR_FLAGS;
@@ -47,12 +47,10 @@ use deno_core::error::JsError;
use deno_core::futures::FutureExt;
use deno_core::unsync::JoinHandle;
use deno_npm::resolution::SnapshotFromLockfileError;
-use deno_runtime::fmt_errors::format_js_error_with_suggestions;
-use deno_runtime::fmt_errors::FixSuggestion;
+use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::tokio_util::create_and_run_current_thread_with_maybe_metrics;
use deno_terminal::colors;
use factory::CliFactory;
-use npm::ResolvePkgFolderFromDenoReqError;
use standalone::MODULE_NOT_FOUND;
use standalone::UNSUPPORTED_SCHEME;
use std::env;
@@ -62,6 +60,10 @@ use std::ops::Deref;
use std::path::PathBuf;
use std::sync::Arc;
+#[cfg(feature = "dhat-heap")]
+#[global_allocator]
+static ALLOC: dhat::Alloc = dhat::Alloc;
+
/// Ensures that all subcommands return an i32 exit code and an [`AnyError`] error type.
trait SubcommandOutput {
fn output(self) -> Result<i32, AnyError>;
@@ -133,7 +135,7 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
tools::compile::compile(flags, compile_flags).await
}),
DenoSubcommand::Coverage(coverage_flags) => spawn_subcommand(async {
- tools::coverage::cover_files(flags, coverage_flags).await
+ tools::coverage::cover_files(flags, coverage_flags)
}),
DenoSubcommand::Fmt(fmt_flags) => {
spawn_subcommand(
@@ -166,10 +168,10 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
if std::io::stderr().is_terminal() {
log::warn!(
"{} command is intended to be run by text editors and IDEs and shouldn't be run manually.
-
+
Visit https://docs.deno.com/runtime/getting_started/setup_your_environment/ for instruction
how to setup your favorite text editor.
-
+
Press Ctrl+C to exit.
", colors::cyan("deno lsp"));
}
@@ -348,109 +350,17 @@ fn setup_panic_hook() {
eprintln!("Args: {:?}", env::args().collect::<Vec<_>>());
eprintln!();
orig_hook(panic_info);
- std::process::exit(1);
+ deno_runtime::exit(1);
}));
}
-#[allow(clippy::print_stderr)]
fn exit_with_message(message: &str, code: i32) -> ! {
- eprintln!(
+ log::error!(
"{}: {}",
colors::red_bold("error"),
message.trim_start_matches("error: ")
);
- std::process::exit(code);
-}
-
-fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec<FixSuggestion> {
- if let Some(msg) = &e.message {
- if msg.contains("module is not defined")
- || msg.contains("exports is not defined")
- {
- return vec![
- FixSuggestion::info(
- "Deno does not support CommonJS modules without `.cjs` extension.",
- ),
- FixSuggestion::hint(
- "Rewrite this module to ESM or change the file extension to `.cjs`.",
- ),
- ];
- } else if msg.contains("openKv is not a function") {
- return vec![
- FixSuggestion::info("Deno.openKv() is an unstable API."),
- FixSuggestion::hint(
- "Run again with `--unstable-kv` flag to enable this API.",
- ),
- ];
- } else if msg.contains("cron is not a function") {
- return vec![
- FixSuggestion::info("Deno.cron() is an unstable API."),
- FixSuggestion::hint(
- "Run again with `--unstable-cron` flag to enable this API.",
- ),
- ];
- } else if msg.contains("WebSocketStream is not defined") {
- return vec![
- FixSuggestion::info("new WebSocketStream() is an unstable API."),
- FixSuggestion::hint(
- "Run again with `--unstable-net` flag to enable this API.",
- ),
- ];
- } else if msg.contains("Temporal is not defined") {
- return vec![
- FixSuggestion::info("Temporal is an unstable API."),
- FixSuggestion::hint(
- "Run again with `--unstable-temporal` flag to enable this API.",
- ),
- ];
- } else if msg.contains("BroadcastChannel is not defined") {
- return vec![
- FixSuggestion::info("BroadcastChannel is an unstable API."),
- FixSuggestion::hint(
- "Run again with `--unstable-broadcast-channel` flag to enable this API.",
- ),
- ];
- } else if msg.contains("window is not defined") {
- return vec![
- FixSuggestion::info("window global is not available in Deno 2."),
- FixSuggestion::hint("Replace `window` with `globalThis`."),
- ];
- } else if msg.contains("UnsafeWindowSurface is not a constructor") {
- return vec![
- FixSuggestion::info("Deno.UnsafeWindowSurface is an unstable API."),
- FixSuggestion::hint(
- "Run again with `--unstable-webgpu` flag to enable this API.",
- ),
- ];
- // Try to capture errors like:
- // ```
- // Uncaught Error: Cannot find module '../build/Release/canvas.node'
- // Require stack:
- // - /.../deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js
- // - /.../.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/canvas.js
- // ```
- } else if msg.contains("Cannot find module")
- && msg.contains("Require stack")
- && msg.contains(".node'")
- {
- return vec![
- FixSuggestion::info_multiline(
- &[
- "Trying to execute an npm package using Node-API addons,",
- "these packages require local `node_modules` directory to be present."
- ]
- ),
- FixSuggestion::hint_multiline(
- &[
- "Add `\"nodeModulesDir\": \"auto\" option to `deno.json`, and then run",
- "`deno install --allow-scripts=npm:<package> --entrypoint <script>` to setup `node_modules` directory."
- ]
- )
- ];
- }
- }
-
- vec![]
+ deno_runtime::exit(code);
}
fn exit_for_error(error: AnyError) -> ! {
@@ -458,8 +368,7 @@ fn exit_for_error(error: AnyError) -> ! {
let mut error_code = 1;
if let Some(e) = error.downcast_ref::<JsError>() {
- let suggestions = get_suggestions_for_terminal_errors(e);
- error_string = format_js_error_with_suggestions(e, suggestions);
+ error_string = format_js_error(e);
} else if let Some(SnapshotFromLockfileError::IntegrityCheckFailed(e)) =
error.downcast_ref::<SnapshotFromLockfileError>()
{
@@ -470,16 +379,18 @@ fn exit_for_error(error: AnyError) -> ! {
exit_with_message(&error_string, error_code);
}
-#[allow(clippy::print_stderr)]
pub(crate) fn unstable_exit_cb(feature: &str, api_name: &str) {
- eprintln!(
+ log::error!(
"Unstable API '{api_name}'. The `--unstable-{}` flag must be provided.",
feature
);
- std::process::exit(70);
+ deno_runtime::exit(70);
}
pub fn main() {
+ #[cfg(feature = "dhat-heap")]
+ let profiler = dhat::Profiler::new_heap();
+
setup_panic_hook();
util::unix::raise_fd_limit();
@@ -500,8 +411,13 @@ pub fn main() {
run_subcommand(Arc::new(flags)).await
};
- match create_and_run_current_thread_with_maybe_metrics(future) {
- Ok(exit_code) => std::process::exit(exit_code),
+ let result = create_and_run_current_thread_with_maybe_metrics(future);
+
+ #[cfg(feature = "dhat-heap")]
+ drop(profiler);
+
+ match result {
+ Ok(exit_code) => deno_runtime::exit(exit_code),
Err(err) => exit_for_error(err),
}
}
@@ -515,12 +431,21 @@ fn resolve_flags_and_init(
if err.kind() == clap::error::ErrorKind::DisplayVersion =>
{
// Ignore results to avoid BrokenPipe errors.
+ util::logger::init(None);
let _ = err.print();
- std::process::exit(0);
+ deno_runtime::exit(0);
+ }
+ Err(err) => {
+ util::logger::init(None);
+ exit_for_error(AnyError::from(err))
}
- Err(err) => exit_for_error(AnyError::from(err)),
};
+ if let Some(otel_config) = flags.otel_config() {
+ deno_runtime::ops::otel::init(otel_config)?;
+ }
+ util::logger::init(flags.log_level);
+
// TODO(bartlomieju): remove in Deno v2.5 and hard error then.
if flags.unstable_config.legacy_flag_enabled {
log::warn!(
@@ -549,7 +474,6 @@ fn resolve_flags_and_init(
deno_core::JsRuntime::init_platform(
None, /* import assertions enabled */ false,
);
- util::logger::init(flags.log_level);
Ok(flags)
}