Skip to content

Commit

Permalink
fix: should not throw an error when local dependency use file protocol (
Browse files Browse the repository at this point in the history
#6157)

close #6115
  • Loading branch information
await-ovo committed Mar 4, 2023
1 parent 690bead commit 019e4f2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .changeset/flat-bees-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@pnpm/plugin-commands-licenses": patch
"@pnpm/license-scanner": patch
"pnpm": patch
---

Should not throw an error when local dependency use file protocol [#6115](https://github.com/pnpm/pnpm/issues/6115).
52 changes: 26 additions & 26 deletions reviewing/license-scanner/src/getPkgInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ export async function readPackageIndexFile (
files: Record<string, string>
}
> {
// If the package resolution is of type directory we need to do things
// differently and generate our own package index file
const isLocalPkg = packageResolution.type === 'directory'
if (isLocalPkg) {
const localInfo = await fetchFromDir(
path.join(opts.lockfileDir, (packageResolution as DirectoryResolution).directory),
{}
)
return {
local: true,
files: localInfo.filesIndex,
}
}

const isPackageWithIntegrity = 'integrity' in packageResolution

let pkgIndexFilePath
Expand Down Expand Up @@ -201,35 +215,21 @@ export async function readPackageIndexFile (
)
}

// If the package resolution is of type directory we need to do things
// differently and generate our own package index file
const isLocalPkg = packageResolution.type === 'directory'
if (isLocalPkg) {
const localInfo = await fetchFromDir(
path.join(opts.lockfileDir, (packageResolution as DirectoryResolution).directory),
{}
)
try {
const { files } = await loadJsonFile<PackageFilesIndex>(pkgIndexFilePath)
return {
local: true,
files: localInfo.filesIndex,
local: false,
files,
}
} else {
try {
const { files } = await loadJsonFile<PackageFilesIndex>(pkgIndexFilePath)
return {
local: false,
files,
}
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
throw new PnpmError(
'MISSING_PACKAGE_INDEX_FILE',
`Failed to find package index file for ${depPath}, please consider running 'pnpm install'`
)
}

throw err
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
throw new PnpmError(
'MISSING_PACKAGE_INDEX_FILE',
`Failed to find package index file for ${depPath}, please consider running 'pnpm install'`
)
}

throw err
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pnpm licenses should work with file protocol dependency: show-packages 1`] = `
"┌─────────────────────────────────┬─────────┐
│ Package │ License │
├─────────────────────────────────┼─────────┤
│ is-positive │ MIT │
├─────────────────────────────────┼─────────┤
│ pnpm-with-file-protocol-sub-dep │ Unknown │
└─────────────────────────────────┴─────────┘
"
`;

exports[`pnpm licenses: output as json: found-license-types 1`] = `
[
"MIT",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "pnpm-with-file-protocol",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"is-positive": "^3.1.0",
"pnpm-with-file-protocol-sub-dep": "file:./sub-dep"
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "pnpm-with-file-protocol-sub-dep",
"version": "1.0.0"
}
24 changes: 24 additions & 0 deletions reviewing/plugin-commands-licenses/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,27 @@ test('pnpm licenses: should correctly read LICENSE file with executable file mod
expect(exitCode).toBe(0)
expect(stripAnsi(output)).toMatchSnapshot('show-packages-details')
})

test('pnpm licenses should work with file protocol dependency', async () => {
const workspaceDir = path.resolve('./test/fixtures/with-file-protocol')

const tmp = tempy.directory()
const storeDir = path.join(tmp, 'store')
await install.handler({
...DEFAULT_OPTS,
dir: workspaceDir,
pnpmHomeDir: '',
storeDir,
})

const { output, exitCode } = await licenses.handler({
...LICENSES_OPTIONS,
dir: workspaceDir,
pnpmHomeDir: '',
long: false,
storeDir: path.resolve(storeDir, 'v3'),
}, ['list'])

expect(exitCode).toBe(0)
expect(stripAnsi(output)).toMatchSnapshot('show-packages')
})

0 comments on commit 019e4f2

Please sign in to comment.