2024-01-01 19:58:21 +00:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2019-09-04 02:12:21 +00:00
2022-06-27 20:54:09 +00:00
mod args ;
2021-02-16 02:50:27 +00:00
mod auth_tokens ;
2021-10-10 21:26:22 +00:00
mod cache ;
2023-11-05 22:58:59 +00:00
mod cdp ;
2021-10-10 21:26:22 +00:00
mod emit ;
2020-10-12 11:25:25 +00:00
mod errors ;
2023-05-01 18:35:23 +00:00
mod factory ;
2020-05-11 21:33:36 +00:00
mod file_fetcher ;
2024-05-16 07:09:35 +00:00
mod graph_container ;
2021-12-16 10:45:41 +00:00
mod graph_util ;
2020-05-11 21:33:36 +00:00
mod http_util ;
2022-11-21 13:36:26 +00:00
mod js ;
2024-03-01 21:34:13 +00:00
mod jsr ;
2020-12-07 10:46:39 +00:00
mod lsp ;
2020-10-13 11:35:35 +00:00
mod module_loader ;
2022-08-20 15:31:33 +00:00
mod node ;
2022-08-10 19:23:58 +00:00
mod npm ;
2020-10-12 11:25:25 +00:00
mod ops ;
2021-10-10 21:26:22 +00:00
mod resolver ;
2024-08-15 19:59:16 +00:00
mod shared ;
2020-11-30 19:35:12 +00:00
mod standalone ;
2024-07-10 03:06:08 +00:00
mod task_runner ;
2020-11-19 18:19:34 +00:00
mod tools ;
2020-11-02 19:41:20 +00:00
mod tsc ;
2022-11-28 22:28:54 +00:00
mod util ;
2020-10-12 11:25:25 +00:00
mod version ;
2022-08-11 20:59:12 +00:00
mod worker ;
2020-05-11 21:33:36 +00:00
2022-06-27 20:54:09 +00:00
use crate ::args ::flags_from_vec ;
use crate ::args ::DenoSubcommand ;
use crate ::args ::Flags ;
2022-11-28 22:28:54 +00:00
use crate ::util ::display ;
2023-01-25 04:03:03 +00:00
use crate ::util ::v8 ::get_v8_flags_from_env ;
use crate ::util ::v8 ::init_v8_flags ;
2022-06-27 20:54:09 +00:00
2024-08-06 15:05:30 +00:00
use args ::TaskFlags ;
2024-10-02 20:17:39 +00:00
use deno_resolver ::npm ::ByonmResolvePkgFolderFromDenoReqError ;
2024-11-14 20:24:25 +00:00
use deno_resolver ::npm ::ResolvePkgFolderFromDenoReqError ;
2024-04-24 19:45:49 +00:00
use deno_runtime ::WorkerExecutionMode ;
2024-01-23 14:33:07 +00:00
pub use deno_runtime ::UNSTABLE_GRANULAR_FLAGS ;
2022-12-09 14:40:48 +00:00
use deno_core ::anyhow ::Context ;
2020-09-14 16:48:57 +00:00
use deno_core ::error ::AnyError ;
2022-04-26 23:06:10 +00:00
use deno_core ::error ::JsError ;
2023-05-17 21:49:57 +00:00
use deno_core ::futures ::FutureExt ;
2023-08-23 23:03:05 +00:00
use deno_core ::unsync ::JoinHandle ;
2024-01-22 21:31:12 +00:00
use deno_npm ::resolution ::SnapshotFromLockfileError ;
2024-10-15 16:59:28 +00:00
use deno_runtime ::fmt_errors ::format_js_error ;
2023-06-09 11:52:51 +00:00
use deno_runtime ::tokio_util ::create_and_run_current_thread_with_maybe_metrics ;
2024-02-07 16:25:14 +00:00
use deno_terminal ::colors ;
2023-05-01 18:35:23 +00:00
use factory ::CliFactory ;
2024-08-06 15:05:30 +00:00
use standalone ::MODULE_NOT_FOUND ;
2024-08-27 09:27:10 +00:00
use standalone ::UNSUPPORTED_SCHEME ;
2020-05-11 21:33:36 +00:00
use std ::env ;
2023-05-17 21:49:57 +00:00
use std ::future ::Future ;
2024-10-07 16:45:50 +00:00
use std ::io ::IsTerminal ;
2024-08-06 15:05:30 +00:00
use std ::ops ::Deref ;
2020-05-11 21:33:36 +00:00
use std ::path ::PathBuf ;
2024-07-23 23:00:48 +00:00
use std ::sync ::Arc ;
2022-02-16 18:14:19 +00:00
2024-10-16 14:03:42 +00:00
#[ cfg(feature = " dhat-heap " ) ]
#[ global_allocator ]
static ALLOC : dhat ::Alloc = dhat ::Alloc ;
2023-05-17 21:49:57 +00:00
/// Ensures that all subcommands return an i32 exit code and an [`AnyError`] error type.
trait SubcommandOutput {
fn output ( self ) -> Result < i32 , AnyError > ;
}
impl SubcommandOutput for Result < i32 , AnyError > {
fn output ( self ) -> Result < i32 , AnyError > {
self
}
}
impl SubcommandOutput for Result < ( ) , AnyError > {
fn output ( self ) -> Result < i32 , AnyError > {
self . map ( | _ | 0 )
}
}
impl SubcommandOutput for Result < ( ) , std ::io ::Error > {
fn output ( self ) -> Result < i32 , AnyError > {
self . map ( | _ | 0 ) . map_err ( | e | e . into ( ) )
}
}
/// Ensure that the subcommand runs in a task, rather than being directly executed. Since some of these
/// futures are very large, this prevents the stack from getting blown out from passing them by value up
/// the callchain (especially in debug mode when Rust doesn't have a chance to elide copies!).
#[ inline(always) ]
fn spawn_subcommand < F : Future < Output = T > + 'static , T : SubcommandOutput > (
f : F ,
) -> JoinHandle < Result < i32 , AnyError > > {
2023-09-30 16:06:38 +00:00
// the boxed_local() is important in order to get windows to not blow the stack in debug
deno_core ::unsync ::spawn (
async move { f . map ( | r | r . output ( ) ) . await } . boxed_local ( ) ,
)
2023-05-17 21:49:57 +00:00
}
2024-07-23 23:00:48 +00:00
async fn run_subcommand ( flags : Arc < Flags > ) -> Result < i32 , AnyError > {
2023-05-17 21:49:57 +00:00
let handle = match flags . subcommand . clone ( ) {
2024-02-29 19:12:04 +00:00
DenoSubcommand ::Add ( add_flags ) = > spawn_subcommand ( async {
2024-08-09 14:29:11 +00:00
tools ::registry ::add ( flags , add_flags , tools ::registry ::AddCommandName ::Add ) . await
2024-02-29 19:12:04 +00:00
} ) ,
2024-08-12 20:17:25 +00:00
DenoSubcommand ::Remove ( remove_flags ) = > spawn_subcommand ( async {
tools ::registry ::remove ( flags , remove_flags ) . await
} ) ,
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Bench ( bench_flags ) = > spawn_subcommand ( async {
2023-06-15 17:09:37 +00:00
if bench_flags . watch . is_some ( ) {
2023-06-14 22:29:19 +00:00
tools ::bench ::run_benchmarks_with_watch ( flags , bench_flags ) . await
2022-12-09 14:40:48 +00:00
} else {
2023-06-14 22:29:19 +00:00
tools ::bench ::run_benchmarks ( flags , bench_flags ) . await
2022-12-09 14:40:48 +00:00
}
2023-05-17 21:49:57 +00:00
} ) ,
2024-09-02 15:27:37 +00:00
DenoSubcommand ::Bundle = > exit_with_message ( " ⚠️ `deno bundle` was removed in Deno 2. \n \n See the Deno 1.x to 2.x Migration Guide for migration instructions: https://docs.deno.com/runtime/manual/advanced/migrate_deprecations " , 1 ) ,
2021-09-03 23:33:35 +00:00
DenoSubcommand ::Doc ( doc_flags ) = > {
2023-11-01 15:25:05 +00:00
spawn_subcommand ( async { tools ::doc ::doc ( flags , doc_flags ) . await } )
2021-09-03 23:33:35 +00:00
}
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Eval ( eval_flags ) = > spawn_subcommand ( async {
2022-12-09 14:40:48 +00:00
tools ::run ::eval_command ( flags , eval_flags ) . await
2023-05-17 21:49:57 +00:00
} ) ,
DenoSubcommand ::Cache ( cache_flags ) = > spawn_subcommand ( async move {
2024-09-04 20:06:16 +00:00
tools ::installer ::install_from_entrypoints ( flags , & cache_flags . files ) . await
2023-05-17 21:49:57 +00:00
} ) ,
DenoSubcommand ::Check ( check_flags ) = > spawn_subcommand ( async move {
feat(cli): evaluate code snippets in JSDoc and markdown (#25220)
This commit lets `deno test --doc` command actually evaluate code snippets in
JSDoc and markdown files.
## How it works
1. Extract code snippets from JSDoc or code fences
2. Convert them into pseudo files by wrapping them in `Deno.test(...)`
3. Register the pseudo files as in-memory files
4. Run type-check and evaluation
We apply some magic at the step 2 - let's say we have the following file named
`mod.ts` as an input:
````ts
/**
* ```ts
* import { assertEquals } from "jsr:@std/assert/equals";
*
* assertEquals(add(1, 2), 3);
* ```
*/
export function add(a: number, b: number) {
return a + b;
}
````
This is virtually transformed into:
```ts
import { assertEquals } from "jsr:@std/assert/equals";
import { add } from "files:///path/to/mod.ts";
Deno.test("mod.ts$2-7.ts", async () => {
assertEquals(add(1, 2), 3);
});
```
Note that a new import statement is inserted here to make `add` function
available. In a nutshell, all items exported from `mod.ts` become available in
the generated pseudo file with this automatic import insertion.
The intention behind this design is that, from library user's standpoint, it
should be very obvious that this `add` function is what this example code is
attached to. Also, if there is an explicit import statement like
`import { add } from "./mod.ts"`, this import path `./mod.ts` is not helpful for
doc readers because they will need to import it in a different way.
The automatic import insertion has some edge cases, in particular where there is
a local variable in a snippet with the same name as one of the exported items.
This case is addressed by employing swc's scope analysis (see test cases for
more details).
## "type-checking only" mode stays around
This change will likely impact a lot of existing doc tests in the ecosystem
because some doc tests rely on the fact that they are not evaluated - some cause
side effects if executed, some throw errors at runtime although they do pass the
type check, etc. To help those tests gradually transition to the ones runnable
with the new `deno test --doc`, we will keep providing the ability to run
type-checking only via `deno check --doc`. Additionally there is a `--doc-only`
option added to the `check` subcommand too, which is useful when you want to
type-check on code snippets in markdown files, as normal `deno check` command
doesn't accept markdown.
## Demo
https://github.com/user-attachments/assets/47e9af73-d16e-472d-b09e-1853b9e8f5ce
---
Closes #4716
2024-09-18 04:35:48 +00:00
tools ::check ::check ( flags , check_flags ) . await
2023-05-17 21:49:57 +00:00
} ) ,
2024-08-08 13:22:18 +00:00
DenoSubcommand ::Clean = > spawn_subcommand ( async move {
2024-08-14 11:04:07 +00:00
tools ::clean ::clean ( )
2024-08-08 13:22:18 +00:00
} ) ,
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Compile ( compile_flags ) = > spawn_subcommand ( async {
tools ::compile ::compile ( flags , compile_flags ) . await
} ) ,
DenoSubcommand ::Coverage ( coverage_flags ) = > spawn_subcommand ( async {
2024-11-01 16:27:00 +00:00
tools ::coverage ::cover_files ( flags , coverage_flags )
2023-05-17 21:49:57 +00:00
} ) ,
2023-06-14 22:29:19 +00:00
DenoSubcommand ::Fmt ( fmt_flags ) = > {
spawn_subcommand (
async move { tools ::fmt ::format ( flags , fmt_flags ) . await } ,
)
}
2022-08-19 23:37:05 +00:00
DenoSubcommand ::Init ( init_flags ) = > {
2024-03-12 03:48:00 +00:00
spawn_subcommand ( async {
2024-11-21 00:03:11 +00:00
tools ::init ::init_project ( init_flags ) . await
2024-03-12 03:48:00 +00:00
} )
2022-08-19 23:37:05 +00:00
}
2021-09-03 23:33:35 +00:00
DenoSubcommand ::Info ( info_flags ) = > {
2023-05-17 21:49:57 +00:00
spawn_subcommand ( async { tools ::info ::info ( flags , info_flags ) . await } )
}
DenoSubcommand ::Install ( install_flags ) = > spawn_subcommand ( async {
tools ::installer ::install_command ( flags , install_flags ) . await
} ) ,
2024-08-12 15:55:33 +00:00
DenoSubcommand ::JSONReference ( json_reference ) = > spawn_subcommand ( async move {
display ::write_to_stdout_ignore_sigpipe ( & deno_core ::serde_json ::to_vec_pretty ( & json_reference . json ) . unwrap ( ) )
} ) ,
2023-09-16 00:42:09 +00:00
DenoSubcommand ::Jupyter ( jupyter_flags ) = > spawn_subcommand ( async {
tools ::jupyter ::kernel ( flags , jupyter_flags ) . await
} ) ,
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Uninstall ( uninstall_flags ) = > spawn_subcommand ( async {
2024-09-05 11:51:37 +00:00
tools ::installer ::uninstall ( flags , uninstall_flags ) . await
2023-05-17 21:49:57 +00:00
} ) ,
2024-10-07 16:45:50 +00:00
DenoSubcommand ::Lsp = > spawn_subcommand ( async {
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.
2024-10-24 07:13:54 +00:00
2024-10-07 16:45:50 +00:00
Visit https ://docs.deno.com/runtime/getting_started/setup_your_environment/ for instruction
how to setup your favorite text editor .
2024-10-24 07:13:54 +00:00
2024-10-07 16:45:50 +00:00
Press Ctrl + C to exit .
" , colors::cyan( " deno lsp " ));
}
lsp ::start ( ) . await
} ) ,
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Lint ( lint_flags ) = > spawn_subcommand ( async {
2022-12-09 14:40:48 +00:00
if lint_flags . rules {
2023-07-25 21:24:06 +00:00
tools ::lint ::print_rules_list (
lint_flags . json ,
lint_flags . maybe_rules_tags ,
) ;
2023-05-17 21:49:57 +00:00
Ok ( ( ) )
2022-12-09 14:40:48 +00:00
} else {
2023-06-14 22:29:19 +00:00
tools ::lint ::lint ( flags , lint_flags ) . await
2022-12-09 14:40:48 +00:00
}
2023-05-17 21:49:57 +00:00
} ) ,
2024-11-20 23:22:15 +00:00
DenoSubcommand ::Outdated ( update_flags ) = > {
spawn_subcommand ( async move {
tools ::registry ::outdated ( flags , update_flags ) . await
} )
}
2021-09-03 23:33:35 +00:00
DenoSubcommand ::Repl ( repl_flags ) = > {
2023-05-17 21:49:57 +00:00
spawn_subcommand ( async move { tools ::repl ::run ( flags , repl_flags ) . await } )
2021-09-03 23:33:35 +00:00
}
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Run ( run_flags ) = > spawn_subcommand ( async move {
2022-12-09 14:40:48 +00:00
if run_flags . is_stdin ( ) {
2024-08-06 15:05:30 +00:00
tools ::run ::run_from_stdin ( flags . clone ( ) ) . await
2022-12-09 14:40:48 +00:00
} else {
2024-08-06 15:05:30 +00:00
let result = tools ::run ::run_script ( WorkerExecutionMode ::Run , flags . clone ( ) , run_flags . watch ) . await ;
2024-08-12 15:55:33 +00:00
match result {
Ok ( v ) = > Ok ( v ) ,
Err ( script_err ) = > {
2024-10-02 20:17:39 +00:00
if let Some ( ResolvePkgFolderFromDenoReqError ::Byonm ( ByonmResolvePkgFolderFromDenoReqError ::UnmatchedReq ( _ ) ) ) = script_err . downcast_ref ::< ResolvePkgFolderFromDenoReqError > ( ) {
if flags . node_modules_dir . is_none ( ) {
let mut flags = flags . deref ( ) . clone ( ) ;
let watch = match & flags . subcommand {
DenoSubcommand ::Run ( run_flags ) = > run_flags . watch . clone ( ) ,
_ = > unreachable! ( ) ,
} ;
flags . node_modules_dir = Some ( deno_config ::deno_json ::NodeModulesDirMode ::None ) ;
// use the current lockfile, but don't write it out
if flags . frozen_lockfile . is_none ( ) {
flags . internal . lockfile_skip_write = true ;
}
return tools ::run ::run_script ( WorkerExecutionMode ::Run , Arc ::new ( flags ) , watch ) . await ;
}
}
2024-08-27 09:27:10 +00:00
let script_err_msg = script_err . to_string ( ) ;
if script_err_msg . starts_with ( MODULE_NOT_FOUND ) | | script_err_msg . starts_with ( UNSUPPORTED_SCHEME ) {
2024-08-12 15:55:33 +00:00
if run_flags . bare {
let mut cmd = args ::clap_root ( ) ;
cmd . build ( ) ;
let command_names = cmd . get_subcommands ( ) . map ( | command | command . get_name ( ) ) . collect ::< Vec < _ > > ( ) ;
let suggestions = args ::did_you_mean ( & run_flags . script , command_names ) ;
if ! suggestions . is_empty ( ) {
let mut error = clap ::error ::Error ::< clap ::error ::DefaultFormatter > ::new ( clap ::error ::ErrorKind ::InvalidSubcommand ) . with_cmd ( & cmd ) ;
error . insert (
clap ::error ::ContextKind ::SuggestedSubcommand ,
clap ::error ::ContextValue ::Strings ( suggestions ) ,
) ;
Err ( error . into ( ) )
} else {
2024-08-06 15:05:30 +00:00
Err ( script_err )
2024-08-12 15:55:33 +00:00
}
} else {
let mut new_flags = flags . deref ( ) . clone ( ) ;
let task_flags = TaskFlags {
cwd : None ,
task : Some ( run_flags . script . clone ( ) ) ,
2024-08-21 13:54:59 +00:00
is_run : true ,
2024-11-20 23:35:12 +00:00
recursive : false ,
filter : None ,
2024-11-20 01:23:20 +00:00
eval : false ,
2024-08-12 15:55:33 +00:00
} ;
new_flags . subcommand = DenoSubcommand ::Task ( task_flags . clone ( ) ) ;
2024-08-21 13:54:59 +00:00
let result = tools ::task ::execute_script ( Arc ::new ( new_flags ) , task_flags . clone ( ) ) . await ;
2024-08-12 15:55:33 +00:00
match result {
Ok ( v ) = > Ok ( v ) ,
Err ( _ ) = > {
// Return script error for backwards compatibility.
Err ( script_err )
}
}
2024-08-06 15:05:30 +00:00
}
2024-08-12 15:55:33 +00:00
} else {
Err ( script_err )
2024-08-06 15:05:30 +00:00
}
}
2024-08-12 15:55:33 +00:00
}
2022-12-09 14:40:48 +00:00
}
2023-05-17 21:49:57 +00:00
} ) ,
2024-04-24 19:45:49 +00:00
DenoSubcommand ::Serve ( serve_flags ) = > spawn_subcommand ( async move {
2024-08-14 22:26:21 +00:00
tools ::serve ::serve ( flags , serve_flags ) . await
2024-04-24 19:45:49 +00:00
} ) ,
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Task ( task_flags ) = > spawn_subcommand ( async {
2024-08-21 13:54:59 +00:00
tools ::task ::execute_script ( flags , task_flags ) . await
2023-05-17 21:49:57 +00:00
} ) ,
2021-09-03 23:33:35 +00:00
DenoSubcommand ::Test ( test_flags ) = > {
2023-05-17 21:49:57 +00:00
spawn_subcommand ( async {
2023-06-15 17:09:37 +00:00
if let Some ( ref coverage_dir ) = test_flags . coverage_dir {
2024-05-23 03:04:12 +00:00
if test_flags . clean {
let _ = std ::fs ::remove_dir_all ( coverage_dir ) ;
}
2023-05-17 21:49:57 +00:00
std ::fs ::create_dir_all ( coverage_dir )
. with_context ( | | format! ( " Failed creating: {coverage_dir} " ) ) ? ;
// this is set in order to ensure spawned processes use the same
// coverage directory
env ::set_var (
" DENO_UNSTABLE_COVERAGE_DIR " ,
PathBuf ::from ( coverage_dir ) . canonicalize ( ) ? ,
) ;
}
2022-12-09 14:40:48 +00:00
2023-06-15 17:09:37 +00:00
if test_flags . watch . is_some ( ) {
2023-06-14 22:29:19 +00:00
tools ::test ::run_tests_with_watch ( flags , test_flags ) . await
2023-05-17 21:49:57 +00:00
} else {
2023-06-14 22:29:19 +00:00
tools ::test ::run_tests ( flags , test_flags ) . await
2023-05-17 21:49:57 +00:00
}
} )
2021-09-03 23:33:35 +00:00
}
2021-12-21 14:49:27 +00:00
DenoSubcommand ::Completions ( completions_flags ) = > {
2023-05-17 21:49:57 +00:00
spawn_subcommand ( async move {
display ::write_to_stdout_ignore_sigpipe ( & completions_flags . buf )
} )
2022-12-09 14:40:48 +00:00
}
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Types = > spawn_subcommand ( async move {
2024-01-14 17:29:17 +00:00
let types = tsc ::get_types_declaration_file_text ( ) ;
2023-05-17 21:49:57 +00:00
display ::write_to_stdout_ignore_sigpipe ( types . as_bytes ( ) )
} ) ,
2023-11-29 18:52:25 +00:00
#[ cfg(feature = " upgrade " ) ]
2023-05-17 21:49:57 +00:00
DenoSubcommand ::Upgrade ( upgrade_flags ) = > spawn_subcommand ( async {
tools ::upgrade ::upgrade ( flags , upgrade_flags ) . await
} ) ,
2023-11-29 18:52:25 +00:00
#[ cfg(not(feature = " upgrade " )) ]
DenoSubcommand ::Upgrade ( _ ) = > exit_with_message (
" This deno was built without the \" upgrade \" feature. Please upgrade using the installation method originally used to install Deno. " ,
1 ,
) ,
2024-09-03 07:00:57 +00:00
DenoSubcommand ::Vendor = > exit_with_message ( " ⚠️ `deno vendor` was removed in Deno 2. \n \n See the Deno 1.x to 2.x Migration Guide for migration instructions: https://docs.deno.com/runtime/manual/advanced/migrate_deprecations " , 1 ) ,
2023-11-23 23:38:07 +00:00
DenoSubcommand ::Publish ( publish_flags ) = > spawn_subcommand ( async {
tools ::registry ::publish ( flags , publish_flags ) . await
} ) ,
2024-08-12 15:55:33 +00:00
DenoSubcommand ::Help ( help_flags ) = > spawn_subcommand ( async move {
2024-08-21 13:54:59 +00:00
use std ::io ::Write ;
let mut stream = anstream ::AutoStream ::new ( std ::io ::stdout ( ) , if colors ::use_color ( ) {
anstream ::ColorChoice ::Auto
} else {
anstream ::ColorChoice ::Never
} ) ;
match stream . write_all ( help_flags . help . ansi ( ) . to_string ( ) . as_bytes ( ) ) {
Ok ( ( ) ) = > Ok ( ( ) ) ,
Err ( e ) = > match e . kind ( ) {
std ::io ::ErrorKind ::BrokenPipe = > Ok ( ( ) ) ,
_ = > Err ( e ) ,
} ,
}
2024-08-12 15:55:33 +00:00
} ) ,
2023-05-17 21:49:57 +00:00
} ;
handle . await ?
2020-11-26 14:17:45 +00:00
}
2024-05-09 02:45:06 +00:00
#[ allow(clippy::print_stderr) ]
2021-12-20 13:49:05 +00:00
fn setup_panic_hook ( ) {
// This function does two things inside of the panic hook:
// - Tokio does not exit the process when a task panics, so we define a custom
// panic hook to implement this behaviour.
// - We print a message to stderr to indicate that this is a bug in Deno, and
// should be reported to us.
2021-09-07 23:34:27 +00:00
let orig_hook = std ::panic ::take_hook ( ) ;
std ::panic ::set_hook ( Box ::new ( move | panic_info | {
2021-12-20 13:49:05 +00:00
eprintln! ( " \n ============================================================ " ) ;
eprintln! ( " Deno has panicked. This is a bug in Deno. Please report this " ) ;
eprintln! ( " at https://github.com/denoland/deno/issues/new. " ) ;
eprintln! ( " If you can reliably reproduce this panic, include the " ) ;
eprintln! ( " reproduction steps and re-run with the RUST_BACKTRACE=1 env " ) ;
eprintln! ( " var set and include the backtrace in your report. " ) ;
eprintln! ( ) ;
2022-07-14 21:52:44 +00:00
eprintln! ( " Platform: {} {} " , env ::consts ::OS , env ::consts ::ARCH ) ;
2024-08-15 21:47:16 +00:00
eprintln! ( " Version: {} " , version ::DENO_VERSION_INFO . deno ) ;
2022-07-14 21:52:44 +00:00
eprintln! ( " Args: {:?} " , env ::args ( ) . collect ::< Vec < _ > > ( ) ) ;
2021-12-20 13:49:05 +00:00
eprintln! ( ) ;
2021-09-07 23:34:27 +00:00
orig_hook ( panic_info ) ;
2024-11-14 12:16:28 +00:00
deno_runtime ::exit ( 1 ) ;
2021-09-07 23:34:27 +00:00
} ) ) ;
}
2023-11-29 18:52:25 +00:00
fn exit_with_message ( message : & str , code : i32 ) -> ! {
2024-11-14 12:16:28 +00:00
log ::error! (
2023-11-29 18:52:25 +00:00
" {}: {} " ,
colors ::red_bold ( " error " ) ,
message . trim_start_matches ( " error: " )
) ;
2024-11-14 12:16:28 +00:00
deno_runtime ::exit ( code ) ;
2023-11-29 18:52:25 +00:00
}
2024-03-31 14:58:19 +00:00
fn exit_for_error ( error : AnyError ) -> ! {
let mut error_string = format! ( " {error:?} " ) ;
let mut error_code = 1 ;
2022-10-25 16:20:07 +00:00
2024-03-31 14:58:19 +00:00
if let Some ( e ) = error . downcast_ref ::< JsError > ( ) {
2024-10-15 16:59:28 +00:00
error_string = format_js_error ( e ) ;
2024-03-31 14:58:19 +00:00
} else if let Some ( SnapshotFromLockfileError ::IntegrityCheckFailed ( e ) ) =
error . downcast_ref ::< SnapshotFromLockfileError > ( )
{
error_string = e . to_string ( ) ;
error_code = 10 ;
2021-01-07 18:06:08 +00:00
}
2024-03-31 14:58:19 +00:00
exit_with_message ( & error_string , error_code ) ;
2021-01-07 18:06:08 +00:00
}
2024-01-23 14:33:07 +00:00
pub ( crate ) fn unstable_exit_cb ( feature : & str , api_name : & str ) {
2024-11-14 12:16:28 +00:00
log ::error! (
2024-01-23 14:33:07 +00:00
" Unstable API '{api_name}'. The `--unstable-{}` flag must be provided. " ,
feature
) ;
2024-11-14 12:16:28 +00:00
deno_runtime ::exit ( 70 ) ;
2023-10-12 15:55:50 +00:00
}
2020-11-26 14:17:45 +00:00
pub fn main ( ) {
2024-10-16 14:03:42 +00:00
#[ cfg(feature = " dhat-heap " ) ]
let profiler = dhat ::Profiler ::new_heap ( ) ;
2021-12-20 13:49:05 +00:00
setup_panic_hook ( ) ;
2021-10-25 15:16:16 +00:00
2022-11-28 22:28:54 +00:00
util ::unix ::raise_fd_limit ( ) ;
util ::windows ::ensure_stdio_open ( ) ;
2020-11-26 14:17:45 +00:00
#[ cfg(windows) ]
colors ::enable_ansi ( ) ; // For Windows 10
2024-06-07 03:37:53 +00:00
deno_runtime ::deno_permissions ::set_prompt_callbacks (
2022-12-19 19:31:19 +00:00
Box ::new ( util ::draw_thread ::DrawThread ::hide ) ,
Box ::new ( util ::draw_thread ::DrawThread ::show ) ,
) ;
2020-11-26 14:17:45 +00:00
2024-03-22 21:03:56 +00:00
let args : Vec < _ > = env ::args_os ( ) . collect ( ) ;
2022-12-09 14:40:48 +00:00
let future = async move {
2024-05-26 04:13:20 +00:00
// NOTE(lucacasonato): due to new PKU feature introduced in V8 11.6 we need to
// initialize the V8 platform on a parent thread of all threads that will spawn
// V8 isolates.
let flags = resolve_flags_and_init ( args ) ? ;
2024-07-23 23:00:48 +00:00
run_subcommand ( Arc ::new ( flags ) ) . await
2024-03-31 14:58:19 +00:00
} ;
2024-01-23 14:33:07 +00:00
2024-10-16 14:03:42 +00:00
let result = create_and_run_current_thread_with_maybe_metrics ( future ) ;
#[ cfg(feature = " dhat-heap " ) ]
drop ( profiler ) ;
match result {
2024-11-14 12:16:28 +00:00
Ok ( exit_code ) = > deno_runtime ::exit ( exit_code ) ,
2024-03-31 14:58:19 +00:00
Err ( err ) = > exit_for_error ( err ) ,
}
}
2021-05-11 04:54:10 +00:00
2024-03-31 14:58:19 +00:00
fn resolve_flags_and_init (
args : Vec < std ::ffi ::OsString > ,
) -> Result < Flags , AnyError > {
let flags = match flags_from_vec ( args ) {
Ok ( flags ) = > flags ,
Err ( err @ clap ::Error { .. } )
2024-08-18 22:39:53 +00:00
if err . kind ( ) = = clap ::error ::ErrorKind ::DisplayVersion = >
2024-03-31 14:58:19 +00:00
{
2024-07-09 22:57:00 +00:00
// Ignore results to avoid BrokenPipe errors.
2024-11-14 12:16:28 +00:00
util ::logger ::init ( None ) ;
2024-07-09 22:57:00 +00:00
let _ = err . print ( ) ;
2024-11-14 12:16:28 +00:00
deno_runtime ::exit ( 0 ) ;
}
Err ( err ) = > {
util ::logger ::init ( None ) ;
exit_for_error ( AnyError ::from ( err ) )
2024-03-31 14:58:19 +00:00
}
} ;
2024-11-14 12:16:28 +00:00
if let Some ( otel_config ) = flags . otel_config ( ) {
deno_runtime ::ops ::otel ::init ( otel_config ) ? ;
}
util ::logger ::init ( flags . log_level ) ;
2024-09-09 21:44:29 +00:00
// TODO(bartlomieju): remove in Deno v2.5 and hard error then.
2024-03-31 14:58:19 +00:00
if flags . unstable_config . legacy_flag_enabled {
2024-09-09 21:44:29 +00:00
log ::warn! (
" ⚠️ {} " ,
colors ::yellow (
" The `--unstable` flag has been removed in Deno 2.0. Use granular `--unstable-*` flags instead. \n Learn more at: https://docs.deno.com/runtime/manual/tools/unstable_flags "
)
) ;
2024-03-31 14:58:19 +00:00
}
2022-02-15 12:33:46 +00:00
2024-03-31 14:58:19 +00:00
let default_v8_flags = match flags . subcommand {
// Using same default as VSCode:
// https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214
DenoSubcommand ::Lsp = > vec! [ " --max-old-space-size=3072 " . to_string ( ) ] ,
2024-04-29 20:43:05 +00:00
_ = > {
2024-08-30 21:58:24 +00:00
// TODO(bartlomieju): I think this can be removed as it's handled by `deno_core`
// and its settings.
// deno_ast removes TypeScript `assert` keywords, so this flag only affects JavaScript
// TODO(petamoriken): Need to check TypeScript `assert` keywords in deno_ast
vec! [ " --no-harmony-import-assertions " . to_string ( ) ]
2024-04-29 20:43:05 +00:00
}
2022-02-15 12:33:46 +00:00
} ;
2020-05-11 21:33:36 +00:00
2024-03-31 14:58:19 +00:00
init_v8_flags ( & default_v8_flags , & flags . v8_flags , get_v8_flags_from_env ( ) ) ;
2024-09-16 23:43:36 +00:00
// TODO(bartlomieju): remove last argument once Deploy no longer needs it
2024-08-30 21:58:24 +00:00
deno_core ::JsRuntime ::init_platform (
None , /* import assertions enabled */ false ,
) ;
2021-11-27 23:45:38 +00:00
2024-03-31 14:58:19 +00:00
Ok ( flags )
2018-06-15 23:43:23 +00:00
}