deno: [node-compat] Getting `BadResource: Bad resource ID` on accessing console or process.stdout

Hey folks, getting this error when trying to make vitepress work with Deno.

error: Uncaught (in promise) BadResource: Bad resource ID
    at Object.isatty (ext:runtime/40_tty.js:19:7)
    at Writable.get [as isTTY] (ext:deno_node/_process/streams.mjs:57:31)
    at createLogger (file:///Users/divyansh/foo/node_modules/.deno/vitepress@1.0.0-beta.1/node_modules/vite/dist/node/chunks/dep-4d3eff22.js:12736:63)
    at file:///Users/divyansh/foo/node_modules/.deno/vitepress@1.0.0-beta.1/node_modules/vitepress/dist/node/cli.js:397:5
    at Object.runMicrotasks (ext:core/01_core.js:836:30)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:51:14)
    at runNextTicks (ext:deno_node/_next_tick.ts:69:5)
    at eventLoopTick (ext:core/01_core.js:188:21)

Reproduction: https://github.com/brc-dd/vitepress-on-deno (run deno task dev)

Removing console logging things seems to prevent the issue. Not completely sure what the cause is.

image

Quick checking suggests that any console.log statements before the createServer call work fine, but after that invocation, they tend to fail. Likely is due to something that createServer is doing.

Version details:

deno 1.34.0 (release, aarch64-apple-darwin)
v8 11.5.150.1
typescript 5.0.4

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 31 (20 by maintainers)

Commits related to this issue

Most upvoted comments

Disabled stdin-discarder at vitepress to unblock. Feel free to close this if it can’t be fixed at deno level (considering stdin-discarder is itself a bit hacky implementation).

Thanks for the heads up @brc-dd. We are still trying to debug the problem with signals. Let’s keep this issue open.

Something at vitepress started causing hydration errors between beta-5 and beta-6 on deno (seems to work fine on node, beta-5 also works fine on deno). Will try to figure that and create a separate issue if that’s something with deno.

🙏 thanks, I’d appreciate a separate issue. A reproduction would be enough for us to figure out the problem.

Thanks! The problem seems to be with the ora spinner module.

Here is a smaller reproduction:

import ora from "npm:ora";

const spinner = ora();
spinner.start('rendering...');

console.log('hello world');

spinner.stopAndPersist({ text: 'done' });
$ deno run test.mjs
⠋ rendering...hello world
  done
# hangs...

Does vitepress explicitly call process.exit()?

No. Directly calling process.exit works, but it terminates the process before streams can finish (if the user has something in build hooks).

I think I just found a test case in Node that describes the behavior:

// test/pseudo-tty/test-set-raw-mode-reset-process-exit.js
'use strict';
require('../common');
const child_process = require('child_process');

// Tests that exiting through process.exit() resets the TTY mode.

child_process.spawnSync(process.execPath, [
  '-e', 'process.stdin.setRawMode(true); process.exit(0)',
], { stdio: 'inherit' });

const { stdout } = child_process.spawnSync('stty', {
  stdio: ['inherit', 'pipe', 'inherit'],
  encoding: 'utf8',
});

if (stdout.match(/-echo\b/)) {
  console.log(stdout);
}

Does vitepress explicitly call process.exit()?

EDIT: Actually it seems that raw mode should be reset even if existing cleanly without process.exit() (test/pseudo-tty/test-set-raw-mode-reset.js)

You should bump vitepress version. That matchMedia thing was fixed by checking typeof document !== 'undefined' instead of checking window one.

After the errors are raised the process doesn’t finish until I press Enter key. Which I assume is the bug you pointed out?

Yeah. I hadn’t checked by pressing enter though 😅

@brc-dd this fix will be available in Deno v1.34.2 to be released next week.

I’ve got a tentative fix ready that makes vitepress work.

Something is closing rid 1 (which is stdout). I will dig deeper to find what causes that.