oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.83k stars 2.55k forks source link

[bug]: lstat: No such file or directory #11823

Open Bellisario opened 3 weeks ago

Bellisario commented 3 weeks ago

What version of Bun is running?

1.1.13+bd6a60512

What platform is your computer?

Darwin 23.4.0 arm64 arm

What steps can reproduce the bug?

Run the following code (fzf needs to be installed):

// index.ts
import { $ } from 'bun'
import { join } from 'path'
import { cwd } from 'process'

// function on the same file for demo purpose
// import { isDir } from './utils'

console.log(import.meta.dir)

// check if fzf is installed
try {
    await $`fzf --version`.quiet()
} catch (error) {
    console.error('"fzf" is not installed')
    process.exit(1)
}

const relativePath = await $`fzf`.text()

if (!relativePath) {
    console.log('No path selected')
    process.exit(0)
}

const absolutePath = join(cwd(), relativePath)

console.log('Selected path:', absolutePath)

if (await isDir(absolutePath)) {
    console.log('Directory')
} else {
    console.log('Not a directory')
}

// utils.ts
import { lstat } from 'fs/promises'

async function isDir(path: string) {
    console.log('Checking:', path)
    return (await lstat(path)).isDirectory()
}

Select any of the file/directory fzf shows. Your output should look like this:

/Users/giorgio/Developer/fzf-smart-actions
Selected path: /Users/giorgio/Developer/fzf-smart-actions/index.ts

Checking: /Users/giorgio/Developer/fzf-smart-actions/index.ts

ENOENT: No such file or directory
   errno: -2
 syscall: "lstat"

Bun v1.1.13 (macOS arm64)

Because fzf shows files found on the running dir, it's impossible that those files are inexistent... in this case I've even chosen the same file where the code runs.

What is the expected behavior?

No error should be thrown and the code should execute without any issue.

What do you see instead?

Code fails to be executed.

Additional information

With the following example, the code runs as expected:

// considering the following code as "index.ts", running with "bun ."

import { $ } from 'bun'
import { join } from 'path'
import { cwd } from 'process'

// function on the same file for demo purpose
// import { isDir } from './utils'

console.log(import.meta.dir)

// check if fzf is installed
try {
    await $`fzf --version`.quiet()
} catch (error) {
    console.error('"fzf" is not installed')
    process.exit(1)
}

const relativePath = await $`fzf`.text()

if (!relativePath) {
    console.log('No path selected')
    process.exit(0)
}

// NOTICE: an existing path not determined by a variable
const absolutePath = join(cwd(), 'index.ts')

console.log('Selected path:', absolutePath)

if (await isDir(absolutePath)) {
    console.log('Directory')
} else {
    console.log('Not a directory')
}

// utils.ts
import { lstat } from 'fs/promises'

async function isDir(path: string) {
    console.log('Checking:', path)
    return (await lstat(path)).isDirectory()
}

I just changed absolutePath to a "static" path instead of a path determined by the execution result of fzf, meaning (on my opinion) that the problem is on relativePath assignment from the result of Bun Shell run.

Bellisario commented 3 weeks ago

Just found coming back to the issues section that someone posted a possible related issue while I was writing mine: #11822