From 484f8ca9c30909914eb3348b2df10d44e2753248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 28 Oct 2024 23:55:51 +0000 Subject: [PATCH] fix: provide hints in terminal errors for Node.js globals (#26610) Add info/hint for terminal errors related to Node.js globals: - __filename - __dirname - Buffer - global - setImmediate - clearImmediate Closes https://github.com/denoland/deno/issues/17494 --- runtime/fmt_errors.rs | 42 +++++++++++++++++++ .../main.out | 3 ++ .../run/node_globals_hints/__test__.jsonc | 34 +++++++++++++++ tests/specs/run/node_globals_hints/buffer.js | 1 + tests/specs/run/node_globals_hints/buffer.out | 7 ++++ .../run/node_globals_hints/clear_immediate.js | 1 + .../node_globals_hints/clear_immediate.out | 7 ++++ tests/specs/run/node_globals_hints/dirname.js | 1 + .../specs/run/node_globals_hints/dirname.out | 7 ++++ .../specs/run/node_globals_hints/filename.js | 1 + .../specs/run/node_globals_hints/filename.out | 7 ++++ tests/specs/run/node_globals_hints/global.js | 1 + tests/specs/run/node_globals_hints/global.out | 7 ++++ .../run/node_globals_hints/set_immediate.js | 1 + .../run/node_globals_hints/set_immediate.out | 7 ++++ 15 files changed, 127 insertions(+) create mode 100644 tests/specs/run/node_globals_hints/__test__.jsonc create mode 100644 tests/specs/run/node_globals_hints/buffer.js create mode 100644 tests/specs/run/node_globals_hints/buffer.out create mode 100644 tests/specs/run/node_globals_hints/clear_immediate.js create mode 100644 tests/specs/run/node_globals_hints/clear_immediate.out create mode 100644 tests/specs/run/node_globals_hints/dirname.js create mode 100644 tests/specs/run/node_globals_hints/dirname.out create mode 100644 tests/specs/run/node_globals_hints/filename.js create mode 100644 tests/specs/run/node_globals_hints/filename.out create mode 100644 tests/specs/run/node_globals_hints/global.js create mode 100644 tests/specs/run/node_globals_hints/global.out create mode 100644 tests/specs/run/node_globals_hints/set_immediate.js create mode 100644 tests/specs/run/node_globals_hints/set_immediate.out diff --git a/runtime/fmt_errors.rs b/runtime/fmt_errors.rs index 7c6cf6d397..4cd8a06345 100644 --- a/runtime/fmt_errors.rs +++ b/runtime/fmt_errors.rs @@ -321,6 +321,48 @@ fn get_suggestions_for_terminal_errors(e: &JsError) -> Vec { ]), FixSuggestion::docs("https://docs.deno.com/go/commonjs"), ]; + } else if msg.contains("__filename is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "__filename global is not available in ES modules." + )), + FixSuggestion::hint(cstr!("Use import.meta.filename instead.")), + ]; + } else if msg.contains("__dirname is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "__dirname global is not available in ES modules." + )), + FixSuggestion::hint(cstr!("Use import.meta.dirname instead.")), + ]; + } else if msg.contains("Buffer is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "Buffer is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Import it explicitly with import { Buffer } from \"node:buffer\";.")), + ]; + } else if msg.contains("clearImmediate is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "clearImmediate is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Import it explicitly with import { clearImmediate } from \"node:timers\";.")), + ]; + } else if msg.contains("setImmediate is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "setImmediate is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Import it explicitly with import { setImmediate } from \"node:timers\";.")), + ]; + } else if msg.contains("global is not defined") { + return vec![ + FixSuggestion::info(cstr!( + "global is not available in the global scope in Deno." + )), + FixSuggestion::hint(cstr!("Use globalThis instead, or assign globalThis.global = globalThis.")), + ]; } else if msg.contains("openKv is not a function") { return vec![ FixSuggestion::info("Deno.openKv() is an unstable API."), diff --git a/tests/specs/lint/node_globals_no_duplicate_imports/main.out b/tests/specs/lint/node_globals_no_duplicate_imports/main.out index 56df10eba4..058b80795d 100644 --- a/tests/specs/lint/node_globals_no_duplicate_imports/main.out +++ b/tests/specs/lint/node_globals_no_duplicate_imports/main.out @@ -2,3 +2,6 @@ error: Uncaught (in promise) ReferenceError: setImmediate is not defined const _foo = setImmediate; ^ at [WILDCARD]main.ts:3:14 + + info: setImmediate is not available in the global scope in Deno. + hint: Import it explicitly with import { setImmediate } from "node:timers";. diff --git a/tests/specs/run/node_globals_hints/__test__.jsonc b/tests/specs/run/node_globals_hints/__test__.jsonc new file mode 100644 index 0000000000..c5c5d6e42e --- /dev/null +++ b/tests/specs/run/node_globals_hints/__test__.jsonc @@ -0,0 +1,34 @@ +{ + "tests": { + "__dirname": { + "args": "run dirname.js", + "output": "dirname.out", + "exitCode": 1 + }, + "__filename": { + "args": "run filename.js", + "output": "filename.out", + "exitCode": 1 + }, + "clearImmediate": { + "args": "run clear_immediate.js", + "output": "clear_immediate.out", + "exitCode": 1 + }, + "buffer": { + "args": "run buffer.js", + "output": "buffer.out", + "exitCode": 1 + }, + "global": { + "args": "run global.js", + "output": "global.out", + "exitCode": 1 + }, + "setImmediate": { + "args": "run set_immediate.js", + "output": "set_immediate.out", + "exitCode": 1 + } + } +} diff --git a/tests/specs/run/node_globals_hints/buffer.js b/tests/specs/run/node_globals_hints/buffer.js new file mode 100644 index 0000000000..9809e1656e --- /dev/null +++ b/tests/specs/run/node_globals_hints/buffer.js @@ -0,0 +1 @@ +Buffer; diff --git a/tests/specs/run/node_globals_hints/buffer.out b/tests/specs/run/node_globals_hints/buffer.out new file mode 100644 index 0000000000..4980e6d129 --- /dev/null +++ b/tests/specs/run/node_globals_hints/buffer.out @@ -0,0 +1,7 @@ +error: Uncaught (in promise) ReferenceError: Buffer is not defined +Buffer; +^ + at [WILDCARD]buffer.js:1:1 + + info: Buffer is not available in the global scope in Deno. + hint: Import it explicitly with import { Buffer } from "node:buffer";. diff --git a/tests/specs/run/node_globals_hints/clear_immediate.js b/tests/specs/run/node_globals_hints/clear_immediate.js new file mode 100644 index 0000000000..94b26ae682 --- /dev/null +++ b/tests/specs/run/node_globals_hints/clear_immediate.js @@ -0,0 +1 @@ +clearImmediate; diff --git a/tests/specs/run/node_globals_hints/clear_immediate.out b/tests/specs/run/node_globals_hints/clear_immediate.out new file mode 100644 index 0000000000..ecd34babef --- /dev/null +++ b/tests/specs/run/node_globals_hints/clear_immediate.out @@ -0,0 +1,7 @@ +error: Uncaught (in promise) ReferenceError: clearImmediate is not defined +clearImmediate; +^ + at [WILDCARD]clear_immediate.js:1:1 + + info: clearImmediate is not available in the global scope in Deno. + hint: Import it explicitly with import { clearImmediate } from "node:timers";. diff --git a/tests/specs/run/node_globals_hints/dirname.js b/tests/specs/run/node_globals_hints/dirname.js new file mode 100644 index 0000000000..58d9e93794 --- /dev/null +++ b/tests/specs/run/node_globals_hints/dirname.js @@ -0,0 +1 @@ +__dirname; diff --git a/tests/specs/run/node_globals_hints/dirname.out b/tests/specs/run/node_globals_hints/dirname.out new file mode 100644 index 0000000000..0aa3e4f51b --- /dev/null +++ b/tests/specs/run/node_globals_hints/dirname.out @@ -0,0 +1,7 @@ +error: Uncaught (in promise) ReferenceError: __dirname is not defined +__dirname; +^ + at [WILDCARD]dirname.js:1:1 + + info: __dirname global is not available in ES modules. + hint: Use import.meta.dirname instead. diff --git a/tests/specs/run/node_globals_hints/filename.js b/tests/specs/run/node_globals_hints/filename.js new file mode 100644 index 0000000000..31770bb141 --- /dev/null +++ b/tests/specs/run/node_globals_hints/filename.js @@ -0,0 +1 @@ +__filename; diff --git a/tests/specs/run/node_globals_hints/filename.out b/tests/specs/run/node_globals_hints/filename.out new file mode 100644 index 0000000000..47b4264fbd --- /dev/null +++ b/tests/specs/run/node_globals_hints/filename.out @@ -0,0 +1,7 @@ +error: Uncaught (in promise) ReferenceError: __filename is not defined +__filename; +^ + at [WILDCARD]filename.js:1:1 + + info: __filename global is not available in ES modules. + hint: Use import.meta.filename instead. diff --git a/tests/specs/run/node_globals_hints/global.js b/tests/specs/run/node_globals_hints/global.js new file mode 100644 index 0000000000..56676d726a --- /dev/null +++ b/tests/specs/run/node_globals_hints/global.js @@ -0,0 +1 @@ +global; diff --git a/tests/specs/run/node_globals_hints/global.out b/tests/specs/run/node_globals_hints/global.out new file mode 100644 index 0000000000..e090d32e29 --- /dev/null +++ b/tests/specs/run/node_globals_hints/global.out @@ -0,0 +1,7 @@ +error: Uncaught (in promise) ReferenceError: global is not defined +global; +^ + at [WILDCARD]global.js:1:1 + + info: global is not available in the global scope in Deno. + hint: Use globalThis instead, or assign globalThis.global = globalThis. diff --git a/tests/specs/run/node_globals_hints/set_immediate.js b/tests/specs/run/node_globals_hints/set_immediate.js new file mode 100644 index 0000000000..fa29d1e714 --- /dev/null +++ b/tests/specs/run/node_globals_hints/set_immediate.js @@ -0,0 +1 @@ +setImmediate; diff --git a/tests/specs/run/node_globals_hints/set_immediate.out b/tests/specs/run/node_globals_hints/set_immediate.out new file mode 100644 index 0000000000..38859ff5d7 --- /dev/null +++ b/tests/specs/run/node_globals_hints/set_immediate.out @@ -0,0 +1,7 @@ +error: Uncaught (in promise) ReferenceError: setImmediate is not defined +setImmediate; +^ + at [WILDCARD]set_immediate.js:1:1 + + info: setImmediate is not available in the global scope in Deno. + hint: Import it explicitly with import { setImmediate } from "node:timers";.