Skip to content

Commit

Permalink
fix: don't break when injecting a dependency with broken symlinks
Browse files Browse the repository at this point in the history
close #5598
  • Loading branch information
zkochan committed Nov 7, 2022
1 parent 46852d4 commit 015da63
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/directory-fetcher/src/index.ts
@@ -1,4 +1,4 @@
import { promises as fs } from 'fs'
import { promises as fs, Stats } from 'fs'
import path from 'path'
import type { DirectoryFetcher, DirectoryFetcherOptions } from '@pnpm/fetcher-base'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
Expand Down Expand Up @@ -65,7 +65,14 @@ async function _fetchAllFilesFromDir (
.filter((file) => file !== 'node_modules')
.map(async (file) => {
const filePath = path.join(dir, file)
const stat = await fs.stat(filePath)
let stat: Stats
try {
stat = await fs.stat(filePath)
} catch (err: any) { // eslint-disable-line @typescript-eslint/no-explicit-any
// Broken symlinks are skipped
if (err.code === 'ENOENT') return
throw err
}
const relativeSubdir = `${relativeDir}${relativeDir ? '/' : ''}${file}`
if (stat.isDirectory()) {
const subFilesIndex = await _fetchAllFilesFromDir(filePath, relativeSubdir)
Expand Down
Empty file.
@@ -0,0 +1,4 @@
{
"name": "simple-pkg",
"version": "0.0.0"
}
23 changes: 23 additions & 0 deletions packages/directory-fetcher/test/index.ts
Expand Up @@ -79,3 +79,26 @@ test('fetch a directory that has no package.json', async () => {
'index.js',
])
})

test('fetch does not fail on package with broken symlink', async () => {
process.chdir(f.find('pkg-with-broken-symlink'))
const fetcher = createDirectoryFetcher()

// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
})

expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesIndex['package.json']).toBe(path.resolve('package.json'))

// Only those files are included which would get published
expect(Object.keys(fetchResult.filesIndex).sort()).toStrictEqual([
'index.js',
'package.json',
])
})

0 comments on commit 015da63

Please sign in to comment.