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: don't copy optional dependencies from the store #7255

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/mean-panthers-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@pnpm/create-cafs-store": patch
"@pnpm/headless": patch
"@pnpm/core": patch
"pnpm": patch
---

Optional dependencies that do not have to be built will be reflinked (or hardlinked) to the store instead of copied [#7046](https://github.com/pnpm/pnpm/issues/7046).
5 changes: 5 additions & 0 deletions .changeset/tiny-ears-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/exec.files-include-install-scripts": major
---

Initial release.
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"gwhitney",
"haptics",
"hardlink",
"hardlinked",
"hardlinking",
"hardlinks",
"hashbang",
Expand Down Expand Up @@ -191,6 +192,7 @@
"refclone",
"reflattened",
"reflink",
"reflinked",
"reflinks",
"rehoist",
"reka",
Expand Down
15 changes: 15 additions & 0 deletions exec/files-include-install-scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @pnpm/exec.files-include-install-scripts

> Checks if a package has files indicating that it needs to be built during installation

[![npm version](https://img.shields.io/npm/v/@pnpm/exec.files-include-install-scripts.svg)](https://www.npmjs.com/package/@pnpm/exec.files-include-install-scripts)

## Installation

```sh
pnpm add @pnpm/exec.files-include-install-scripts
```

## License

MIT
43 changes: 43 additions & 0 deletions exec/files-include-install-scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@pnpm/exec.files-include-install-scripts",
"version": "0.0.0",
"description": "Checks if a package has files indicating that it needs to be built during installation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"!*.map"
],
"engines": {
"node": ">=16.14"
},
"scripts": {
"lint": "eslint \"src/**/*.ts\"",
"test": "pnpm run compile",
"prepublishOnly": "pnpm run compile",
"fix": "tslint -c tslint.json src/**/*.ts test/**/*.ts --fix",
"compile": "tsc --build && pnpm run lint --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/main/exec/files-include-install-scripts",
"keywords": [
"pnpm8",
"pnpm"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/pnpm/pnpm/issues"
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/exec/files-include-install-scripts#readme",
"peerDependencies": {
},
"dependencies": {
},
"devDependencies": {
"@pnpm/exec.files-include-install-scripts": "workspace:*"
},
"funding": "https://opencollective.com/pnpm",
"exports": {
".": "./lib/index.js"
}
}

4 changes: 4 additions & 0 deletions exec/files-include-install-scripts/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function filesIncludeInstallScripts (filesIndex: Record<string, unknown>): boolean {
return filesIndex['binding.gyp'] != null ||
Object.keys(filesIndex).some((filename) => !(filename.match(/^[.]hooks[\\/]/) == null)) // TODO: optimize this
}
13 changes: 13 additions & 0 deletions exec/files-include-install-scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@pnpm/tsconfig",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": [
"src/**/*.ts",
"../../__typings__/**/*.d.ts"
],
"references": [],
"composite": true
}
8 changes: 8 additions & 0 deletions exec/files-include-install-scripts/tsconfig.lint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"test/**/*.ts",
"../../__typings__/**/*.d.ts"
]
}
2 changes: 1 addition & 1 deletion pkg-manager/core/src/install/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ async function linkAllPkgs (
filesResponse: files,
force: opts.force,
sideEffectsCacheKey,
requiresBuild: depNode.requiresBuild || depNode.patchFile != null,
requiresBuild: depNode.patchFile != null || (depNode.optional ? (depNode.requiresBuild ? undefined : false) : depNode.requiresBuild),
})
if (importMethod) {
progressLogger.debug({
Expand Down
37 changes: 37 additions & 0 deletions pkg-manager/core/test/install/optionalDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,40 @@ describe('supported architectures', () => {
expect(fs.readdirSync('node_modules/.pnpm').length).toBe(5)
})
})

test('optional dependency is hardlinked to the store if it does not require a build', async () => {
prepareEmpty()
const manifest = {
dependencies: {
'@pnpm.e2e/pkg-with-good-optional': '*',
},
}

const reporter = jest.fn()
await install(manifest, await testDefaults({ reporter }, {}, {}, { packageImportMethod: 'hardlink' }))

expect(reporter).toHaveBeenCalledWith(
expect.objectContaining({
level: 'debug',
name: 'pnpm:progress',
method: 'hardlink',
status: 'imported',
to: path.resolve('node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive'),
})
)

await rimraf('node_modules')

reporter.mockClear()
await install(manifest, await testDefaults({ frozenLockfile: true, reporter }, {}, {}, { packageImportMethod: 'hardlink' }))

expect(reporter).toHaveBeenCalledWith(
expect.objectContaining({
level: 'debug',
name: 'pnpm:progress',
method: 'hardlink',
status: 'imported',
to: path.resolve('node_modules/.pnpm/is-positive@1.0.0/node_modules/is-positive'),
})
)
})
2 changes: 1 addition & 1 deletion pkg-manager/headless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ async function linkAllPkgs (
filesResponse,
force: opts.force,
disableRelinkLocalDirDeps: opts.disableRelinkLocalDirDeps,
requiresBuild: depNode.requiresBuild || depNode.patchFile != null,
requiresBuild: depNode.patchFile != null || (depNode.optional ? (depNode.requiresBuild ? undefined : false) : depNode.requiresBuild),
sideEffectsCacheKey,
})
if (importMethod) {
Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/headless/src/linkHoistedModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async function linkAllPkgsInOrder (
force: true,
disableRelinkLocalDirDeps: opts.disableRelinkLocalDirDeps,
keepModulesDir: true,
requiresBuild: depNode.requiresBuild || depNode.patchFile != null,
requiresBuild: depNode.patchFile != null || (depNode.optional ? (depNode.requiresBuild ? undefined : false) : depNode.requiresBuild),
sideEffectsCacheKey,
})
if (importMethod) {
Expand Down
1 change: 1 addition & 0 deletions pkg-manager/resolve-dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@pnpm/core-loggers": "workspace:*",
"@pnpm/dependency-path": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/exec.files-include-install-scripts": "workspace:*",
"@pnpm/lockfile-types": "workspace:*",
"@pnpm/lockfile-utils": "workspace:*",
"@pnpm/manifest-utils": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions pkg-manager/resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import { PnpmError } from '@pnpm/error'
import { filesIncludeInstallScripts } from '@pnpm/exec.files-include-install-scripts'
import {
packageManifestLogger,
} from '@pnpm/core-loggers'
Expand Down Expand Up @@ -341,8 +342,7 @@ async function finishLockfileUpdates (
Boolean(pkgJson.scripts.install) ||
Boolean(pkgJson.scripts.postinstall)
) ||
files.filesIndex['binding.gyp'] ||
Object.keys(files.filesIndex).some((filename) => !(filename.match(/^[.]hooks[\\/]/) == null)) // TODO: optimize this
filesIncludeInstallScripts(files.filesIndex)
)
}
if (typeof depNode.requiresBuild === 'function') {
Expand Down
3 changes: 3 additions & 0 deletions pkg-manager/resolve-dependencies/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../../config/pick-registry-for-package"
},
{
"path": "../../exec/files-include-install-scripts"
},
{
"path": "../../fetching/pick-fetcher"
},
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions store/create-cafs-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@pnpm/logger": "^5.0.0"
},
"dependencies": {
"@pnpm/exec.files-include-install-scripts": "workspace:*",
"@pnpm/store.cafs": "workspace:*",
"@pnpm/fetcher-base": "workspace:*",
"@pnpm/fs.indexed-pkg-importer": "workspace:*",
Expand Down
22 changes: 19 additions & 3 deletions store/create-cafs-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs'
import { promises as fs, readFileSync } from 'fs'
import path from 'path'
import { filesIncludeInstallScripts } from '@pnpm/exec.files-include-install-scripts'
import {
type CafsLocker,
createCafs,
Expand Down Expand Up @@ -34,7 +35,8 @@ export function createPackageImporterAsync (
const gfm = getFlatMap.bind(null, opts.cafsDir)
return async (to, opts) => {
const { filesMap, isBuilt } = gfm(opts.filesResponse, opts.sideEffectsCacheKey)
const pkgImportMethod = (opts.requiresBuild && !isBuilt)
const willBeBuilt = !isBuilt && (opts.requiresBuild ?? pkgRequiresBuild(filesMap))
const pkgImportMethod = willBeBuilt
? 'clone-or-copy'
: (opts.filesResponse.packageImportMethod ?? packageImportMethod)
const impPkg = cachedImporterCreator(pkgImportMethod)
Expand Down Expand Up @@ -63,7 +65,8 @@ function createPackageImporter (
const gfm = getFlatMap.bind(null, opts.cafsDir)
return (to, opts) => {
const { filesMap, isBuilt } = gfm(opts.filesResponse, opts.sideEffectsCacheKey)
const pkgImportMethod = (opts.requiresBuild && !isBuilt)
const willBeBuilt = !isBuilt && (opts.requiresBuild ?? pkgRequiresBuild(filesMap))
const pkgImportMethod = willBeBuilt
? 'clone-or-copy'
: (opts.filesResponse.packageImportMethod ?? packageImportMethod)
const impPkg = cachedImporterCreator(pkgImportMethod)
Expand Down Expand Up @@ -128,3 +131,16 @@ export function createCafsStore (
},
}
}

function pkgRequiresBuild (filesMap: Record<string, string>) {
return filesIncludeInstallScripts(filesMap) ||
filesMap['package.json'] && pkgJsonHasInstallScripts(filesMap['package.json'])
}

function pkgJsonHasInstallScripts (file: string): boolean {
const pkgJson = JSON.parse(readFileSync(file, 'utf8'))
if (!pkgJson.scripts) return false
return Boolean(pkgJson.scripts.preinstall) ||
Boolean(pkgJson.scripts.install) ||
Boolean(pkgJson.scripts.postinstall)
}
3 changes: 3 additions & 0 deletions store/create-cafs-store/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../../__utils__/prepare"
},
{
"path": "../../exec/files-include-install-scripts"
},
{
"path": "../../fetching/fetcher-base"
},
Expand Down