From 613aeb5db04a86fd4444ae65ff940e9937e69a33 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 10 Apr 2022 01:12:32 +0100 Subject: [PATCH 1/3] fix(node): Make global.ts evaluate synchronously --- node/global_test.ts | 22 ++++++++++++++++++++++ node/internal/util/debuglog.ts | 23 ++++++++++------------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/node/global_test.ts b/node/global_test.ts index 400d67a65f69..f7f12031c18b 100644 --- a/node/global_test.ts +++ b/node/global_test.ts @@ -2,6 +2,7 @@ import "./global.ts"; import { assert, + assertEquals, assertNotEquals, assertStrictEquals, } from "../testing/asserts.ts"; @@ -86,3 +87,24 @@ Deno.test("clearImmediate is correctly defined", () => { assertStrictEquals(globalThis.clearImmediate, timers.clearImmediate); assertStrictEquals(window.clearImmediate, timers.clearImmediate); }); + +Deno.test("global.ts evaluates synchronously", async () => { + const tempPath = await Deno.makeTempFile({ suffix: ".ts" }); + await Deno.writeTextFile( + tempPath, + `\ + import "data:application/javascript,import '${ + new URL("global.ts", import.meta.url).href + }'; console.log(globalThis.async ? 'async' : 'sync')"; + import "data:application/javascript,globalThis.async = true";`, + ); + const process = Deno.run({ + cmd: [Deno.execPath(), "run", "--no-check", tempPath], + stdin: "null", + stdout: "piped", + stderr: "null", + }); + assertEquals((await process.status()).code, 0); + assertEquals(new TextDecoder().decode(await process.output()).trim(), "sync"); + process.close(); +}); diff --git a/node/internal/util/debuglog.ts b/node/internal/util/debuglog.ts index e1aa0756db1b..36ebdd8ba34f 100644 --- a/node/internal/util/debuglog.ts +++ b/node/internal/util/debuglog.ts @@ -103,19 +103,16 @@ export function debuglog( return logger; } -let state = ""; - -if (Deno.permissions) { - state = (await Deno.permissions.query({ - name: "env", - variable: "NODE_DEBUG", - })).state; -} - -if (state === "granted") { - initializeDebugEnv(Deno.env.get("NODE_DEBUG") ?? ""); -} else { - initializeDebugEnv(""); +let debugEnv; +try { + debugEnv = Deno.env.get("NODE_DEBUG") ?? ""; +} catch (error) { + if (error instanceof Deno.errors.PermissionDenied) { + debugEnv = ""; + } else { + throw error; + } } +initializeDebugEnv(debugEnv); export default { debuglog }; From 14dacd6a1bc8537f68a4b950e8ff4560ef71078f Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 10 Apr 2022 01:15:34 +0100 Subject: [PATCH 2/3] Add comment --- node/global_test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/node/global_test.ts b/node/global_test.ts index f7f12031c18b..adca8d7f0c41 100644 --- a/node/global_test.ts +++ b/node/global_test.ts @@ -88,6 +88,7 @@ Deno.test("clearImmediate is correctly defined", () => { assertStrictEquals(window.clearImmediate, timers.clearImmediate); }); +// https://github.com/denoland/deno_std/issues/2097 Deno.test("global.ts evaluates synchronously", async () => { const tempPath = await Deno.makeTempFile({ suffix: ".ts" }); await Deno.writeTextFile( From 84dd3696a56ec8ce0b86ac7437e453690c7b3fc6 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 10 Apr 2022 01:20:37 +0100 Subject: [PATCH 3/3] Delete temp file in test --- node/global_test.ts | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/node/global_test.ts b/node/global_test.ts index adca8d7f0c41..9eea2c3b8998 100644 --- a/node/global_test.ts +++ b/node/global_test.ts @@ -91,21 +91,26 @@ Deno.test("clearImmediate is correctly defined", () => { // https://github.com/denoland/deno_std/issues/2097 Deno.test("global.ts evaluates synchronously", async () => { const tempPath = await Deno.makeTempFile({ suffix: ".ts" }); - await Deno.writeTextFile( - tempPath, - `\ - import "data:application/javascript,import '${ - new URL("global.ts", import.meta.url).href - }'; console.log(globalThis.async ? 'async' : 'sync')"; - import "data:application/javascript,globalThis.async = true";`, - ); - const process = Deno.run({ - cmd: [Deno.execPath(), "run", "--no-check", tempPath], - stdin: "null", - stdout: "piped", - stderr: "null", - }); - assertEquals((await process.status()).code, 0); - assertEquals(new TextDecoder().decode(await process.output()).trim(), "sync"); - process.close(); + try { + await Deno.writeTextFile( + tempPath, + `\ + import "data:application/javascript,import '${ + new URL("global.ts", import.meta.url).href + }'; console.log(globalThis.async ? 'async' : 'sync')"; + import "data:application/javascript,globalThis.async = true";`, + ); + const process = Deno.run({ + cmd: [Deno.execPath(), "run", "--no-check", tempPath], + stdin: "null", + stdout: "piped", + stderr: "null", + }); + assertEquals((await process.status()).code, 0); + const stdout = new TextDecoder().decode(await process.output()); + assertEquals(stdout.trim(), "sync"); + process.close(); + } finally { + await Deno.remove(tempPath).catch(() => {}); + } });