Skip to content

Commit

Permalink
fix: print better error on git-hosted pkg install failure (#4044)
Browse files Browse the repository at this point in the history
ref #4038
  • Loading branch information
zkochan committed Nov 28, 2021
1 parent 0595e82 commit fb1a95a
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/eleven-clouds-scream.md
@@ -0,0 +1,6 @@
---
"@pnpm/git-fetcher": patch
"@pnpm/tarball-fetcher": patch
---

The temporary directory should be removed after preparing the git-hosted package.
5 changes: 5 additions & 0 deletions .changeset/nice-dolphins-kiss.md
@@ -0,0 +1,5 @@
---
"@pnpm/tarball-fetcher": patch
---

Fetch is not retried if preparation of git-hosted package fails.
5 changes: 5 additions & 0 deletions .changeset/spotty-bees-float.md
@@ -0,0 +1,5 @@
---
"@pnpm/prepare-package": patch
---

If prepare fails, throw a pnpm error with a known error code.
1 change: 1 addition & 0 deletions packages/git-fetcher/src/index.ts
Expand Up @@ -24,6 +24,7 @@ export default () => {
// removing /.git to make directory integrity calculation faster
await rimraf(path.join(tempLocation, '.git'))
const filesIndex = await cafs.addFilesFromDir(tempLocation, opts.manifest)
await rimraf(tempLocation)
return { filesIndex }
},
}
Expand Down
1 change: 1 addition & 0 deletions packages/prepare-package/package.json
Expand Up @@ -28,6 +28,7 @@
},
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/prepare-package#readme",
"dependencies": {
"@pnpm/error": "workspace:2.0.0",
"@pnpm/read-package-json": "workspace:5.0.6",
"@zkochan/rimraf": "^2.1.1",
"execa": "npm:safe-execa@^0.1.1",
Expand Down
7 changes: 6 additions & 1 deletion packages/prepare-package/src/index.ts
@@ -1,4 +1,5 @@
import path from 'path'
import PnpmError from '@pnpm/error'
import { safeReadPackageFromDir } from '@pnpm/read-package-json'
import rimraf from '@zkochan/rimraf'
import execa from 'execa'
Expand All @@ -8,7 +9,11 @@ export default async function preparePackage (pkgDir: string) {
const manifest = await safeReadPackageFromDir(pkgDir)
if (manifest?.scripts?.prepare != null && manifest.scripts.prepare !== '') {
const pm = (await preferredPM(pkgDir))?.name ?? 'npm'
await execa(pm, ['install'], { cwd: pkgDir })
try {
await execa(pm, ['install'], { cwd: pkgDir })
} catch (err: any) { // eslint-disable-line
throw new PnpmError('PREPARE_PKG_FAILURE', `${err.shortMessage}`) // eslint-disable-line
}
await rimraf(path.join(pkgDir, 'node_modules'))
}
}
3 changes: 3 additions & 0 deletions packages/prepare-package/tsconfig.json
Expand Up @@ -9,6 +9,9 @@
"../../typings/**/*.d.ts"
],
"references": [
{
"path": "../error"
},
{
"path": "../read-package-json"
}
Expand Down
1 change: 1 addition & 0 deletions packages/tarball-fetcher/package.json
Expand Up @@ -41,6 +41,7 @@
"@pnpm/graceful-fs": "workspace:1.0.0",
"@pnpm/prepare-package": "workspace:1.0.6",
"@zkochan/retry": "^0.2.0",
"@zkochan/rimraf": "^2.1.1",
"ramda": "^0.27.1",
"ssri": "^8.0.1"
},
Expand Down
9 changes: 8 additions & 1 deletion packages/tarball-fetcher/src/createDownloader.ts
Expand Up @@ -12,6 +12,7 @@ import {
import { FetchFromRegistry } from '@pnpm/fetching-types'
import preparePackage from '@pnpm/prepare-package'
import * as retry from '@zkochan/retry'
import rimraf from '@zkochan/rimraf'
import fromPairs from 'ramda/src/fromPairs'
import omit from 'ramda/src/omit'
import ssri from 'ssri'
Expand Down Expand Up @@ -117,8 +118,13 @@ export default (
try {
resolve(await fetch(attempt))
} catch (error: any) { // eslint-disable-line
if (error.response?.status === 401 || error.response?.status === 403) {
if (
error.response?.status === 401 ||
error.response?.status === 403 ||
error.code === 'ERR_PNPM_PREPARE_PKG_FAILURE'
) {
reject(error)
return
}
const timeout = op.retry(error)
if (timeout === false) {
Expand Down Expand Up @@ -242,6 +248,7 @@ async function prepareGitHostedPkg (filesIndex: FilesIndex, cafs: Cafs) {
})
await preparePackage(tempLocation)
const newFilesIndex = await cafs.addFilesFromDir(tempLocation)
await rimraf(tempLocation)
return newFilesIndex
}

Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb1a95a

Please sign in to comment.