bun icon indicating copy to clipboard operation
bun copied to clipboard

Bun.stdin/Bun.stdout results in Unseekable for read/write

Open fnky opened this issue 2 years ago • 9 comments

Version

0.1.3

Platform

Darwin fnky 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64

What steps will reproduce the bug?

Failing to write to stdout:

await Bun.write(Bun.stdout, "output");

Failing to read from stdin:

console.log(await Bun.stdin.text());

How often does it reproduce? Is there a required condition?

Can be reproduced at least on both my MacBooks using macOS Mojave 10.14.6. Someone did confirm it worked for them using Ubuntu.

What is the expected behavior?

To write "output" to stdout for the first example.

To await for stdin and console.log the awaited string input.

What do you see instead?

The stdout example outputs

Unseekable
syscall: "write"

While the stdin example outputs

Unseekable
syscall: "read"

Additional information

No response

fnky avatar Jul 12 '22 19:07 fnky

I tried using the node API to write to stdout and got some more output from the error in the callback:

import fs from "node:fs";
fs.write(1, "output", 0, "utf8", (err, written) => {
  console.log(err, written);
});

produced the following output:

106 |   queueMicrotask(function () {
107 |     try {
108 |       args[args.length - 1](
109 |         null,
110 |
111 |         fsFunction.apply(fs, args.slice(0, args.length - 1))
           ^
 ENXIO: Device not configured
syscall: "pwrite"
  errno: -6

      at node:fs:111:8
 undefined

as well as for reading from stdin:

import fs from "node:fs";
import { Buffer } from "node:buffer";

const buf = Buffer.alloc(16);
fs.read(0, buf, 0, 16, 0, (err, bytesRead) => {
  console.log(err, bytesRead);
});

produces

106 |   queueMicrotask(function () {
107 |     try {
108 |       args[args.length - 1](
109 |         null,
110 |
111 |         fsFunction.apply(fs, args.slice(0, args.length - 1))
           ^
 ENXIO: Device not configured
syscall: "pread"
  errno: -6

      at node:fs:111:8
 undefined

The line error report at queueMicrotask is a red flag, since using writeSync/readSync will place it at the correct call in the code.

fnky avatar Jul 12 '22 21:07 fnky

Writing directly to /dev/stdout//dev/tty/stdout file descriptor with node FS works:

fs.writeFileSync("/dev/stdout", "output", "utf-8"); // => output
fs.writeFileSync(1, "output", "utf-8"); // => output

but using Bun.file fails with the same error:

await Bun.write(Bun.file("/dev/stdout"), "output");

fnky avatar Jul 12 '22 22:07 fnky

This is a macOS bug. It shouldn't be using pwrite. It should use write

Jarred-Sumner avatar Jul 13 '22 11:07 Jarred-Sumner

@Jarred-Sumner do you mean that AsyncIO#write should be using write instead of pwrite (https://github.com/oven-sh/bun/blob/main/src/io/io_darwin.zig#L1427)? Or that Bun.write should use write instead of pwrite when destination_blob points to stdout or stderr (https://github.com/oven-sh/bun/blob/main/src/io/io_darwin.zig#L1427)? Or something else?

TzviPM avatar Jul 25 '22 18:07 TzviPM

I'm also getting this; I'm on the MacOS Ventura beta fwiw

tbjgolden avatar Aug 21 '22 15:08 tbjgolden

I'm guessing a fix to this dependends on https://github.com/oven-sh/bun/pull/1115

as the fix might involve checking which os and they might wanna reuse the code from it

tbjgolden avatar Aug 21 '22 15:08 tbjgolden

I had a fix for this actually and then lost the code after a git reset HEAD —hard

Will fix it this week

On Sun, Aug 21, 2022 at 8:18 AM Tom Golden @.***> wrote:

I'm guessing a fix to this dependends on #1115 https://github.com/oven-sh/bun/pull/1115

as the fix might involve checking which os and they might wanna reuse the code from it

— Reply to this email directly, view it on GitHub https://github.com/oven-sh/bun/issues/646#issuecomment-1221565648, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFNGS4KDDL4IXMDAUFXHJLV2JCCTANCNFSM53METVMQ . You are receiving this because you were mentioned.Message ID: @.***>

Jarred-Sumner avatar Aug 21 '22 15:08 Jarred-Sumner

Thanks @Jarred-Sumner! Waiting on this to continue with https://github.com/oven-sh/bun/issues/894, I believe I'm pretty close to polyfill process.stdin/stdout :)

franciscop avatar Aug 23 '22 02:08 franciscop

Thanks @Jarred-Sumner - need access to a stream to replicate ora-like spinner, chalk equivalent currently available at bun-style.

ctjlewis avatar Aug 25 '22 09:08 ctjlewis

I don't think reading from/writing stdin/stdout works on *nix for several cases. Tests will provide empirical evidence.

guest271314 avatar Oct 10 '22 01:10 guest271314

This was fixed long ago

Jarred-Sumner avatar Dec 10 '23 06:12 Jarred-Sumner

@Jarred-Sumner, I'm still having trouble with Bun.stdin on macOS:

$ bun --revision
1.0.15+b3bdf22eb

$ uname -mprs
Darwin 21.6.0 arm64 arm

$ cat stdin.js
console.log(JSON.stringify(await Bun.stdin.text()));

$ echo hello | bun run stdin.js
""

davidchambers avatar Dec 10 '23 07:12 davidchambers

@davidchambers

Please run bun upgrade --canary and try again

Jarred-Sumner avatar Dec 10 '23 07:12 Jarred-Sumner

Works now! :tada:

davidchambers avatar Dec 10 '23 07:12 davidchambers