From b96af41867e81be1fc8295ae889c1dacb7cda337 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 24 Aug 2022 01:59:05 +0300 Subject: [PATCH 1/3] fix: symlink local dep even when target dir does not exist close #5219 --- packages/symlink-dependency/src/symlinkDirectRootDependency.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/symlink-dependency/src/symlinkDirectRootDependency.ts b/packages/symlink-dependency/src/symlinkDirectRootDependency.ts index 8d060d81bdd..5176fc39700 100644 --- a/packages/symlink-dependency/src/symlinkDirectRootDependency.ts +++ b/packages/symlink-dependency/src/symlinkDirectRootDependency.ts @@ -50,7 +50,7 @@ export default async function symlinkDirectRootDependency ( } catch (err: any) { // eslint-disable-line if (err.code !== 'ENOENT') throw err globalWarn(`Local dependency not found at ${dependencyLocation}`) - return + dependencyRealLocation = dependencyLocation } const dest = path.join(destModulesDirReal, importAs) From 7079b29cacac461bb202c5dde7512b1a21c4a87c Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 24 Aug 2022 02:24:04 +0300 Subject: [PATCH 2/3] test: symlink dep --- .changeset/orange-swans-relax.md | 6 ++++++ packages/symlink-dependency/jest.config.js | 1 + packages/symlink-dependency/package.json | 8 ++++--- .../test/symlinkDirectRootDependency.test.ts | 21 +++++++++++++++++++ packages/symlink-dependency/tsconfig.json | 3 +++ pnpm-lock.yaml | 3 +++ 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 .changeset/orange-swans-relax.md create mode 100644 packages/symlink-dependency/jest.config.js create mode 100644 packages/symlink-dependency/test/symlinkDirectRootDependency.test.ts diff --git a/.changeset/orange-swans-relax.md b/.changeset/orange-swans-relax.md new file mode 100644 index 00000000000..6f7d006dae6 --- /dev/null +++ b/.changeset/orange-swans-relax.md @@ -0,0 +1,6 @@ +--- +"@pnpm/symlink-dependency": patch +"pnpm": patch +--- + +Symlink a local dependency to `node_modules`, even if the target directory doesn't exist [#5219](https://github.com/pnpm/pnpm/issues/5219). diff --git a/packages/symlink-dependency/jest.config.js b/packages/symlink-dependency/jest.config.js new file mode 100644 index 00000000000..f697d831691 --- /dev/null +++ b/packages/symlink-dependency/jest.config.js @@ -0,0 +1 @@ +module.exports = require('../../jest.config.js') diff --git a/packages/symlink-dependency/package.json b/packages/symlink-dependency/package.json index ce49bbb7234..d598c0e580e 100644 --- a/packages/symlink-dependency/package.json +++ b/packages/symlink-dependency/package.json @@ -16,6 +16,7 @@ }, "devDependencies": { "@pnpm/logger": "^4.0.0", + "@pnpm/prepare": "workspace:*", "@pnpm/symlink-dependency": "workspace:*" }, "directories": { @@ -33,10 +34,11 @@ "repository": "https://github.com/pnpm/pnpm/blob/main/packages/symlink-dependency", "scripts": { "start": "tsc --watch", - "test": "pnpm run compile", - "lint": "eslint src/**/*.ts", + "test": "pnpm run compile && pnpm run _test", + "lint": "eslint src/**/*.ts test/**/*.ts", "prepublishOnly": "pnpm run compile", - "compile": "tsc --build && pnpm run lint --fix" + "compile": "tsc --build && pnpm run lint --fix", + "_test": "jest" }, "dependencies": { "@pnpm/core-loggers": "workspace:*", diff --git a/packages/symlink-dependency/test/symlinkDirectRootDependency.test.ts b/packages/symlink-dependency/test/symlinkDirectRootDependency.test.ts new file mode 100644 index 00000000000..737d7fad1eb --- /dev/null +++ b/packages/symlink-dependency/test/symlinkDirectRootDependency.test.ts @@ -0,0 +1,21 @@ +import fs from 'fs' +import path from 'path' +import { tempDir } from '@pnpm/prepare' +import { symlinkDirectRootDependency } from '@pnpm/symlink-dependency' + +test('symlink is created to directory that does not yet exist', async () => { + const tmp = tempDir(false) + const destModulesDir = path.join(tmp, 'node_modules') + const dependencyLocation = path.join(tmp, 'dep') + fs.mkdirSync(destModulesDir) + await symlinkDirectRootDependency(dependencyLocation, destModulesDir, 'dep', { + linkedPackage: { + name: 'dep', + version: '1.0.0', + }, + prefix: '', + }) + fs.mkdirSync(dependencyLocation) + fs.writeFileSync(path.join(dependencyLocation, 'index.js'), 'module.exports = {}') + expect(fs.existsSync(path.join(destModulesDir, 'dep/index.js'))).toBe(true) +}) diff --git a/packages/symlink-dependency/tsconfig.json b/packages/symlink-dependency/tsconfig.json index 2cce143602d..783b976c392 100644 --- a/packages/symlink-dependency/tsconfig.json +++ b/packages/symlink-dependency/tsconfig.json @@ -9,6 +9,9 @@ "../../typings/**/*.d.ts" ], "references": [ + { + "path": "../../privatePackages/prepare" + }, { "path": "../core-loggers" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 25fc9544a3c..c5e823e6da5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5031,6 +5031,9 @@ importers: '@pnpm/logger': specifier: ^4.0.0 version: 4.0.0 + '@pnpm/prepare': + specifier: workspace:* + version: link:../../privatePackages/prepare '@pnpm/symlink-dependency': specifier: workspace:* version: 'link:' From 9a61250e920aec6da2d4dac08a408bdd0678c87e Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Wed, 24 Aug 2022 03:44:20 +0300 Subject: [PATCH 3/3] docs: add comment --- packages/symlink-dependency/src/symlinkDirectRootDependency.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/symlink-dependency/src/symlinkDirectRootDependency.ts b/packages/symlink-dependency/src/symlinkDirectRootDependency.ts index 5176fc39700..3e1ef4b1e05 100644 --- a/packages/symlink-dependency/src/symlinkDirectRootDependency.ts +++ b/packages/symlink-dependency/src/symlinkDirectRootDependency.ts @@ -50,6 +50,9 @@ export default async function symlinkDirectRootDependency ( } catch (err: any) { // eslint-disable-line if (err.code !== 'ENOENT') throw err globalWarn(`Local dependency not found at ${dependencyLocation}`) + // Sometimes the linked in local package does not exist during installation + // and is created later via a build script. + // That is why we create the symlink even if the target directory does not exist. dependencyRealLocation = dependencyLocation }