8000 feat: ignore EACCES on linking by antongolub · Pull Request #121 · npm/bin-links · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: ignore EACCES on linking #121

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

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/link-gently.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

const { resolve, dirname } = require('path')
const { lstat, mkdir, readlink, rm, symlink } = require('fs/promises')
const throwNonEnoent = er => {
if (er.code !== 'ENOENT') {
throw er
const { log } = require('proc-log')
const throwSignificant = er => {
if (er.code === 'ENOENT') {
return
}
if (er.code === 'EACCES') {
log.warn('error adding file', er.message)
return
}
throw er
}

const rmOpts = {
Expand Down Expand Up @@ -37,8 +43,8 @@ const linkGently = async ({ path, to, from, absFrom, force }) => {
// or at least a warning, but npm has always behaved this
// way in the past, so it'd be a breaking change
return Promise.all([
lstat(absFrom).catch(throwNonEnoent),
lstat(to).catch(throwNonEnoent),
lstat(absFrom).catch(throwSigni 8000 ficant),
lstat(to).catch(throwSignificant),
]).then(([stFrom, stTo]) => {
// not present in package, skip it
if (!stFrom) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"cmd-shim": "^6.0.0",
"npm-normalize-package-bin": "^3.0.0",
"proc-log": "^4.2.0",
"read-cmd-shim": "^4.0.0",
"write-file-atomic": "^5.0.0"
},
Expand Down
43 changes: 43 additions & 0 deletions test/link-gently.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const t = require('tap')
const linkGently = require('../lib/link-gently.js')
const fs = require('fs')
const fsp = require('fs/promises')
const requireInject = require('require-inject')

t.test('make links gently', async t => {
Expand Down Expand Up @@ -87,6 +88,48 @@ t.test('make links gently', async t => {
linkGently.resetSeen()
})

t.test('handles link errors', async t => {
const dir = t.testdir({
pkg: {
'hello.js': `#!/usr/bin/env node\nconsole.log('hello')`,
},
})
const fspMock = {
...fsp,
lstat: (ref) => {
const code = (/\/(e\w+)$/.exec(ref) || [])[1]
if (code) {
return Promise.reject(Object.assign(new Error(), { code: code.toUpperCase() }))
}

return fsp.lstat(ref)
},
}
const mockedLinkGently = requireInject('../lib/link-gently.js', {
'fs/promises': fspMock,
})
await mockedLinkGently({
path: `${dir}/pkg`,
to: `${dir}/bin/eacces`,
from: `../pkg/hello.js`,
absFrom: `${dir}/pkg/hello.js`,
})

await mockedLinkGently({
path: `${dir}/pkg`,
to: `${dir}/bin/enoent`,
from: `../pkg/hello.js`,
absFrom: `${dir}/pkg/hello.js`,
})

await t.rejects(mockedLinkGently({
path: `${dir}/pkg`,
to: `${dir}/bin/eperm`,
from: `../pkg/hello.js`,
absFrom: `${dir}/pkg/hello.js`,
}), { code: 'EPERM' })
})

t.test('racey race', async t => {
const fsMock = {
...fs,
Expand Down
Loading
0