Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ag runs without stdin with Bun shell #9510

Closed
gaute opened this issue Mar 19, 2024 · 2 comments
Closed

ag runs without stdin with Bun shell #9510

gaute opened this issue Mar 19, 2024 · 2 comments
Assignees
Labels
bug Something isn't working shell Something to do with Bun as a shell

Comments

@gaute
Copy link

gaute commented Mar 19, 2024

What version of Bun is running?

1.0.33+9e91e137f

What platform is your computer?

Darwin 23.3.0 arm64 arm

What steps can reproduce the bug?

I use ag, a faster alternative to grep, in a Bun shell command that pipes stdout from one command to ag´s stdin. It worked fine up to Bun 1.0.31, but fails in Bun 1.0.32 and 1.0.33.

In an empty directory, make these two files:

catfile.txt

cat cat cat
cat cat
cat

bunbug.test.js

import {expect, test} from "bun:test";
import {$} from 'bun';

test('grep and ag produces same output', async () => {
  const grepOutput = await $`cat catfile.txt | grep 'cat'`.text();
  const agOutput = await $`cat catfile.txt | ag 'cat'`.text();

  expect(agOutput).toEqual(grepOutput);
});

What is the expected behavior?

In the session below, I first run the commands cat catfile.txt | grep 'cat' and cat catfile.txt | ag 'cat'. They do the same thing, as they should.

Then, I install Bun 1.0.31, and run the test that confirms the output is equal.

$ cat catfile.txt | grep 'cat'
cat cat cat cat
cat cat
cat
$ cat catfile.txt | ag 'cat'
cat cat cat cat
cat cat
cat
$ curl -fsSL https://bun.sh/install | bash -s "bun-v1.0.31"
######################################################################## 100.0%
bun was installed successfully to ~/.bun/bin/bun 
Run 'bun --help' to get started
$ ~/.bun/bin/bun test
bun test v1.0.31 (e2567512)

bunbug.test.js:
✓ grep and ag produces same output [45.90ms]

 1 pass
 0 fail
 1 expect() calls
Ran 1 tests across 1 files. [63.00ms]

What do you see instead?

When I install Bun 1.0.32, the test fails. It's now as if ag runs without stdin, it finds matches in both files.

$ curl -fsSL https://bun.sh/install | bash -s "bun-v1.0.32"
######################################################################## 100.0%
bun was installed successfully to ~/.bun/bin/bun 
Run 'bun --help' to get started
$ ~/.bun/bin/bun test
bun test v1.0.32 (5fec71bd)

bunbug.test.js:
3 | 
4 | test('grep and ag produces same output', async () => {
5 |   const grepOutput = await $`cat catfile.txt | grep 'cat'`.text();
6 |   const agOutput = await $`cat catfile.txt | ag 'cat'`.text();
7 | 
8 |   expect(agOutput).toEqual(grepOutput);
      ^
error: expect(received).toEqual(expected)

Expected: "cat cat cat cat\ncat cat\ncat\n"
Received: "catfile.txt:1:cat cat cat cat\ncatfile.txt:2:cat cat\ncatfile.txt:3:cat\nbunbug.test.js:5:  const grepOutput = await $`cat catfile.txt | grep 'cat'`.text();\nbunbug.test.js:6:  const agOutput = await $`cat catfile.txt | ag 'cat'`.text();\n"

      at /Users/gaute/Arbeid/Test/bunbug/bunbug.test.js:8:3
✗ grep and ag produces same output [60.20ms]

 0 pass
 1 fail
 1 expect() calls
Ran 1 tests across 1 files. [88.00ms]

The same happens in 1.0.33.

Additional information

No response

@gaute gaute added the bug Something isn't working label Mar 19, 2024
@Jarred-Sumner
Copy link
Collaborator

This can be reproduced outside of JavaScript and directly in the bun shell via running a .bun.sh file:

image

@zackradisic are we now inheriting stdin instead of creating a pipe? When .text() or similar is used, we should not inherit stdin

@zackradisic
Copy link
Contributor

Closing because this is actually caused by ag not supporting sockets for stdin (see this). Sockets are used in pipelines to ensure IO is not blocking without breaking process behavior by setting O_NONBLOCK

In this particular case, you could instead redirect catfile.txt to ag like so: ag 'cat' < catfile.txt. This will not use sockets and rather just pass a file descriptor pointing to the catfile.txt file

@zackradisic zackradisic changed the title Shell pipe regression ag runs without stdin with Bun shell May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working shell Something to do with Bun as a shell
Projects
None yet
Development

No branches or pull requests

4 participants