Skip to content
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

fix(hard-link-dir): prevent fatal error on ENOENT #6396

Merged
merged 3 commits into from Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .changeset/fuzzy-parents-juggle.md
@@ -1,5 +1,6 @@
---
"@pnpm/fs.hard-link-dir": patch
"pnpm": patch
---

Warn user when publishConfig.directory of an injected workspace dependency does not exist
Warn user when `publishConfig.directory` of an injected workspace dependency does not exist [#6396](https://github.com/pnpm/pnpm/pull/6396).
6 changes: 3 additions & 3 deletions fs/hard-link-dir/package.json
Expand Up @@ -30,14 +30,14 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/fs/hard-link-dir#readme",
"funding": "https://opencollective.com/pnpm",
"peerDependencies": {
"@pnpm/logger": "^5.0.0"
},
"devDependencies": {
"@pnpm/fs.hard-link-dir": "workspace:*",
"@pnpm/prepare": "workspace:*"
},
"exports": {
".": "./lib/index.js"
},
"dependencies": {
"@pnpm/logger": "^5.0.0"
}
}
24 changes: 13 additions & 11 deletions fs/hard-link-dir/src/index.ts
Expand Up @@ -6,21 +6,23 @@ export async function hardLinkDir (src: string, destDirs: string[]) {
if (destDirs.length === 0) return
// Don't try to hard link the source directory to itself
destDirs = destDirs.filter((destDir) => path.relative(destDir, src) !== '')
// TODO try/catch proceed to make destDir but leave empty
await _hardLinkDir(src, destDirs, true)
}

async function _hardLinkDir (src: string, destDirs: string[], isRoot?: boolean) {
let files: string[] = []
try {
files = await fs.readdir(src)
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
globalWarn(`Source directory not found when creating hardLinks for: ${src}. Creating destinations as empty: ${destDirs.join(', ')}`)
await Promise.all(
destDirs.map(async (dir) => {
await fs.mkdir(dir, { recursive: true })
})
)
}
if (!isRoot || err.code !== 'ENOENT') throw err
globalWarn(`Source directory not found when creating hardLinks for: ${src}. Creating destinations as empty: ${destDirs.join(', ')}`)
await Promise.all(
destDirs.map(async (dir) => {
await fs.mkdir(dir, { recursive: true })
})
)
zkochan marked this conversation as resolved.
Show resolved Hide resolved
return
}

await Promise.all(
files.map(async (file) => {
if (file === 'node_modules') return
Expand All @@ -37,7 +39,7 @@ export async function hardLinkDir (src: string, destDirs: string[]) {
return destSubdir
})
)
await hardLinkDir(srcFile, destSubdirs)
await _hardLinkDir(srcFile, destSubdirs)
return
}
await Promise.all(
Expand Down
8 changes: 6 additions & 2 deletions fs/hard-link-dir/test/index.ts
Expand Up @@ -8,8 +8,6 @@ test('hardLinkDirectory()', async () => {
const srcDir = path.join(tempDir, 'source')
const dest1Dir = path.join(tempDir, 'dest1')
const dest2Dir = path.join(tempDir, 'dest2')
const missingDirSrc = path.join(tempDir, 'missing_source')
const missingDirDest = path.join(tempDir, 'missing_dest')

fs.mkdirSync(srcDir, { recursive: true })
fs.mkdirSync(dest1Dir, { recursive: true })
Expand All @@ -33,6 +31,12 @@ test('hardLinkDirectory()', async () => {
// It should not link files from node_modules
expect(fs.existsSync(path.join(dest1Dir, 'node_modules/file.txt'))).toBe(false)
expect(fs.existsSync(path.join(dest2Dir, 'node_modules/file.txt'))).toBe(false)
})

test("don't fail on missing source and dest directories", async () => {
const tempDir = createTempDir()
const missingDirSrc = path.join(tempDir, 'missing_source')
const missingDirDest = path.join(tempDir, 'missing_dest')

await hardLinkDir(missingDirSrc, [missingDirDest])

Expand Down