fix(link-bins): ensure permission of bin file when symlinked (#5913)

close #5822
close #5890
This commit is contained in:
bohan
2023-01-12 09:43:05 +08:00
committed by GitHub
parent 2c615198f9
commit 4008a52361
4 changed files with 37 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/link-bins": patch
"pnpm": patch
---
Ensure the permission of bin file when `prefer-symlinked-executables` is set to `true` [#5913](https://github.com/pnpm/pnpm/pull/5913).

View File

@@ -210,6 +210,7 @@ async function linkBin (cmd: CommandInfo, binsDir: string, opts?: LinkBinOptions
if (opts?.preferSymlinkedExecutables && !IS_WINDOWS && cmd.nodeExecPath == null) {
await symlinkDir(cmd.path, externalBinPath)
await fixBin(cmd.path, 0o755)
return
}

View File

@@ -1 +1,2 @@
#!/usr/bin/env node
console.log('hello_world')

View File

@@ -12,6 +12,7 @@ import isWindows from 'is-windows'
import normalizePath from 'normalize-path'
import exists from 'path-exists'
import tempy from 'tempy'
import { spawnSync } from 'child_process'
jest.mock('@pnpm/logger', () => {
const debug = jest.fn()
@@ -71,6 +72,34 @@ test('linkBins()', async () => {
}
})
test('linkBins() should work when prefer-symlinked-executables enabled', async () => {
const binTarget = tempy.directory()
const warn = jest.fn()
const simpleFixture = f.prepare('simple-fixture')
await linkBins(path.join(simpleFixture, 'node_modules'), binTarget, { warn, preferSymlinkedExecutables: true })
expect(warn).not.toHaveBeenCalled()
expect(await fs.readdir(binTarget)).toEqual(getExpectedBins(['simple']))
const binLocation = path.join(binTarget, 'simple')
expect(await exists(binLocation)).toBe(true)
const content = await fs.readFile(binLocation, 'utf8')
if (IS_WINDOWS) {
expect(content).toMatch('node_modules/simple/index.js')
} else {
expect(content).toMatch('console.log(\'hello_world\')')
}
if (EXECUTABLE_SHEBANG_SUPPORTED) {
const binFile = path.join(binTarget, 'simple')
const stat = await fs.stat(binFile)
expect(stat.mode).toBe(parseInt('100755', 8))
expect(stat.isFile()).toBe(true)
const stdout = spawnSync(binFile).stdout.toString('utf-8')
expect(stdout).toMatch('hello_world')
}
})
test('linkBins() never creates a PowerShell shim for the pnpm CLI', async () => {
const binTarget = tempy.directory()
const fixture = f.prepare('pnpm-cli')