From ee904ec06c1a3b3d4e4a87898e777e2f9b587b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 15 Oct 2024 22:51:39 +0100 Subject: [PATCH] fix: add hint for missing `document` global in terminal error (#26218) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This came up on Discord as a question so I thought it's worth adding a hint for this as it might be a common pitfall. --------- Signed-off-by: Bartek IwaƄczuk Co-authored-by: David Sherret --- Cargo.lock | 1 + Cargo.toml | 1 + cli/Cargo.toml | 2 +- runtime/Cargo.toml | 1 + runtime/fmt_errors.rs | 11 +++++++++++ tests/specs/run/document/__test__.jsonc | 9 +++++++++ tests/specs/run/document/document.js | 1 + tests/specs/run/document/document.out | 8 ++++++++ tests/specs/test/document/__test__.jsonc | 9 +++++++++ tests/specs/test/document/document_test.js | 3 +++ tests/specs/test/document/document_test.out | 22 +++++++++++++++++++++ 11 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/specs/run/document/__test__.jsonc create mode 100644 tests/specs/run/document/document.js create mode 100644 tests/specs/run/document/document.out create mode 100644 tests/specs/test/document/__test__.jsonc create mode 100644 tests/specs/test/document/document_test.js create mode 100644 tests/specs/test/document/document_test.out diff --git a/Cargo.lock b/Cargo.lock index 6e47123886..8cffe6eb49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1997,6 +1997,7 @@ dependencies = [ name = "deno_runtime" version = "0.180.0" dependencies = [ + "color-print", "deno_ast", "deno_broadcast_channel", "deno_cache", diff --git a/Cargo.toml b/Cargo.toml index e83d4239ea..25842134a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,6 +106,7 @@ cbc = { version = "=0.1.2", features = ["alloc"] } # Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS. # Instead use util::time::utc_now() chrono = { version = "0.4", default-features = false, features = ["std", "serde"] } +color-print = "0.3.5" console_static_text = "=0.8.1" dashmap = "5.5.3" data-encoding = "2.3.3" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 8d1d1d1241..f1a8ca0a25 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -94,7 +94,7 @@ chrono = { workspace = true, features = ["now"] } clap = { version = "=4.5.16", features = ["env", "string", "wrap_help", "error-context"] } clap_complete = "=4.5.24" clap_complete_fig = "=4.5.2" -color-print = "0.3.5" +color-print.workspace = true console_static_text.workspace = true dashmap.workspace = true data-encoding.workspace = true diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index ba9dc6243f..6cb00a97e0 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -100,6 +100,7 @@ deno_websocket.workspace = true deno_webstorage.workspace = true node_resolver = { workspace = true, features = ["sync"] } +color-print.workspace = true dlopen2.workspace = true encoding_rs.workspace = true fastwebsockets.workspace = true diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs index 0d4274e8ab..2d9d09a298 100644 --- a/runtime/fmt_errors.rs +++ b/runtime/fmt_errors.rs @@ -1,5 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. //! This mod provides DenoError to unify errors across Deno. +use color_print::cstr; use deno_core::error::format_frame; use deno_core::error::JsError; use deno_terminal::colors::cyan; @@ -367,6 +368,16 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec { ] ) ]; + } else if msg.contains("document is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "document global is not available in Deno." + )), + FixSuggestion::hint_multiline(&[ + cstr!("Use a library like happy-dom, deno_dom, linkedom or JSDom"), + cstr!("and setup the document global according to the library documentation."), + ]), + ]; } } diff --git a/tests/specs/run/document/__test__.jsonc b/tests/specs/run/document/__test__.jsonc new file mode 100644 index 0000000000..cf20f9e1b2 --- /dev/null +++ b/tests/specs/run/document/__test__.jsonc @@ -0,0 +1,9 @@ +{ + "tests": { + "document": { + "args": "run document.js", + "exitCode": 1, + "output": "document.out" + } + } +} diff --git a/tests/specs/run/document/document.js b/tests/specs/run/document/document.js new file mode 100644 index 0000000000..63e43b72f0 --- /dev/null +++ b/tests/specs/run/document/document.js @@ -0,0 +1 @@ +document.querySelector("div"); diff --git a/tests/specs/run/document/document.out b/tests/specs/run/document/document.out new file mode 100644 index 0000000000..eefedb8f2b --- /dev/null +++ b/tests/specs/run/document/document.out @@ -0,0 +1,8 @@ +error: Uncaught (in promise) ReferenceError: document is not defined +document.querySelector("div"); +^ + at [WILDCARD]document.js:1:1 + + info: document global is not available in Deno. + hint: Use a library like happy-dom, deno_dom, linkedom or JSDom + and setup the document global according to the library documentation. diff --git a/tests/specs/test/document/__test__.jsonc b/tests/specs/test/document/__test__.jsonc new file mode 100644 index 0000000000..cf73b4bab8 --- /dev/null +++ b/tests/specs/test/document/__test__.jsonc @@ -0,0 +1,9 @@ +{ + "tests": { + "document": { + "args": "test document_test.js", + "exitCode": 1, + "output": "document_test.out" + } + } +} diff --git a/tests/specs/test/document/document_test.js b/tests/specs/test/document/document_test.js new file mode 100644 index 0000000000..d60d6893cc --- /dev/null +++ b/tests/specs/test/document/document_test.js @@ -0,0 +1,3 @@ +Deno.test("document query selector", () => { + document.querySelector("div"); +}); diff --git a/tests/specs/test/document/document_test.out b/tests/specs/test/document/document_test.out new file mode 100644 index 0000000000..342cee0e47 --- /dev/null +++ b/tests/specs/test/document/document_test.out @@ -0,0 +1,22 @@ +running 1 test from ./document_test.js +document query selector ... FAILED [WILDCARD] + + ERRORS + +document query selector => ./document_test.js:1:6 +error: ReferenceError: document is not defined + document.querySelector("div"); + ^ + at [WILDCARD]document_test.js:2:3 + + info: document global is not available in Deno. + hint: Use a library like happy-dom, deno_dom, linkedom or JSDom + and setup the document global according to the library documentation. + + FAILURES + +document query selector => ./document_test.js:1:6 + +FAILED | 0 passed | 1 failed ([WILDCARD]) + +error: Test failed