vercel / pkg

Package your Node.js project into an executable
https://npmjs.com/pkg
MIT License
24.28k stars 1.01k forks source link

fs.promises.access doesn't work in /snapshot #2020

Closed yuchiXiong closed 8 months ago

yuchiXiong commented 10 months ago

What version of pkg are you using?

5.8.1

What version of Node.js are you using?

v16.20.2

What operating system are you using?

Windows 10 22H2 (19045.3570)

What CPU architecture are you using?

AMD64

What Node versions, OSs and CPU architectures are you building for?

node16-win-x64

Describe the Bug

A tool library I'm using uses the fs.promises.access API.

When the argument is the address of a file located in /snapshot, it always throws the following error:

[Error: ENOENT: no such file or directory, access 'C:\snapshot\src\app.js'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'access',
  path: 'C:\\snapshot\\src\\app.js'
}

I was looking through the issues list and noticed that a similar issue had appeared in the past with the fs.promises.stat API, which worked fine in my code, but the fs.promises.access API still had the same issue.

Here is the simple code I used to reproduce the problem and it works fine in Node.

const fs = require('fs');
const path = require('path');

const filename = path.join(__dirname, 'app.js')
console.log(filename);
console.log(fs.accessSync(filename))

fs.promises.access(filename).then(() => {
  console.log('access success')
}, err => {
  console.log('access fail', err)
})

Expected Behavior

It should work fine like the fs.promises.stat API.

To Reproduce

src/app.js

const fs = require('fs');
const path = require('path');

const filename = path.join(__dirname, 'app.js')
console.log(filename);
console.log(fs.accessSync(filename))

fs.promises.access(filename).then(() => {
  console.log('access success')
}, err => {
  console.log('access fail', err)
})

package.json

{
  "name": "pkg-with-access-promise",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "bin": "src/app.js",
  "pkg": {
    "src": [
      "data.json"
    ]
  },
  "scripts": {
    "package": "pkg src/app.js --output output/app"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "pkg": "^5.8.1"
  }
}

then…… run it

PS D:\codes\demo\pkg-with-access-promise> node .\src\app.js
D:\codes\demo\pkg-with-access-promise\src\app.js
undefined
access success

PS D:\codes\demo\pkg-with-access-promise> yarn package
yarn run v1.22.19
$ pkg src/app.js --output output/app
> pkg@5.8.1
> Targets not specified. Assuming:
  node16-win-x64
Done in 2.47s.

PS D:\codes\demo\pkg-with-access-promise> .\output\app.exe
C:\snapshot\src\app.js
undefined
access fail [Error: ENOENT: no such file or directory, access 'C:\snapshot\src\app.js'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'access',
  path: 'C:\\snapshot\\src\\app.js'
}
mountaindude commented 8 months ago

I've noted the same thing.

My use case was to determine if a file in the snapshot exists or not.

Instead of using fs.promises.access I solved this by using the following code:

let exists = false;
try {
    await fs.promises.stat(filePath);
    exists = true;
} catch (error) {
    logger.verbose(`File does not exist: ${filePath}`);
}
logger.verbose(`File exists: ${exists}`);
skaneprime commented 8 months ago

facing same issue with fs.promises.opendir