From 6b4da91cb0804e1609854e1f4e77fcd55899e411 Mon Sep 17 00:00:00 2001 From: "Patryk (PsychoX) Ludwikowski" Date: Mon, 4 Jul 2022 02:37:06 +0200 Subject: [PATCH] fix: allow link from path with hash character --- lib/commands/link.js | 4 +-- .../test/lib/commands/link.js.test.cjs | 5 +++ test/lib/commands/link.js | 36 +++++++++++++++++++ .../arborist/lib/arborist/build-ideal-tree.js | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/commands/link.js b/lib/commands/link.js index b0b889ea787fd..7bce73ed2bb6f 100644 --- a/lib/commands/link.js +++ b/lib/commands/link.js @@ -122,7 +122,7 @@ class Link extends ArboristWorkspaceCmd { ...this.npm.flatOptions, prune: false, path: this.npm.prefix, - add: names.map(l => `file:${resolve(globalTop, 'node_modules', l)}`), + add: names.map(l => `file:${resolve(globalTop, 'node_modules', l).replace(/#/g, '%23')}`), save, workspaces: this.workspaceNames, }) @@ -133,7 +133,7 @@ class Link extends ArboristWorkspaceCmd { async linkPkg () { const wsp = this.workspacePaths const paths = wsp && wsp.length ? wsp : [this.npm.prefix] - const add = paths.map(path => `file:${path}`) + const add = paths.map(path => `file:${path.replace(/#/g, '%23')}`) const globalTop = resolve(this.npm.globalDir, '..') const arb = new Arborist({ ...this.npm.flatOptions, diff --git a/tap-snapshots/test/lib/commands/link.js.test.cjs b/tap-snapshots/test/lib/commands/link.js.test.cjs index a9a10b20a2f83..e01409e4ce196 100644 --- a/tap-snapshots/test/lib/commands/link.js.test.cjs +++ b/tap-snapshots/test/lib/commands/link.js.test.cjs @@ -5,6 +5,11 @@ * Make sure to inspect the output below. Do not ignore changes! */ 'use strict' +exports[`test/lib/commands/link.js TAP hash character in working directory path > should create a global link to current pkg, even within path with hash 1`] = ` +{CWD}/test/lib/commands/tap-testdir-link-hash-character-in-working-directory-path/global-prefix/lib/node_modules/test-pkg-link -> {CWD}/test/lib/commands/tap-testdir-link-hash-character-in-working-directory-path/i_like_#_in_my_paths/test-pkg-link + +` + exports[`test/lib/commands/link.js TAP link global linked pkg to local nm when using args > should create a local symlink to global pkg 1`] = ` {CWD}/test/lib/commands/tap-testdir-link-link-global-linked-pkg-to-local-nm-when-using-args/my-project/node_modules/@myscope/bar -> {CWD}/test/lib/commands/tap-testdir-link-link-global-linked-pkg-to-local-nm-when-using-args/global-prefix/lib/node_modules/@myscope/bar {CWD}/test/lib/commands/tap-testdir-link-link-global-linked-pkg-to-local-nm-when-using-args/my-project/node_modules/@myscope/linked -> {CWD}/test/lib/commands/tap-testdir-link-link-global-linked-pkg-to-local-nm-when-using-args/scoped-linked diff --git a/test/lib/commands/link.js b/test/lib/commands/link.js index a01de0b247990..b9ef2d35e2fdc 100644 --- a/test/lib/commands/link.js +++ b/test/lib/commands/link.js @@ -514,3 +514,39 @@ t.test('--global option', async t => { 'should throw an useful error' ) }) + +t.test('hash character in working directory path', async t => { + const testdir = t.testdir({ + 'global-prefix': { + lib: { + node_modules: { + a: { + 'package.json': JSON.stringify({ + name: 'a', + version: '1.0.0', + }), + }, + }, + }, + }, + 'i_like_#_in_my_paths': { + 'test-pkg-link': { + 'package.json': JSON.stringify({ + name: 'test-pkg-link', + version: '1.0.0', + }), + }, + }, + }) + npm.globalDir = resolve(testdir, 'global-prefix', 'lib', 'node_modules') + npm.prefix = resolve(testdir, 'i_like_#_in_my_paths', 'test-pkg-link') + + link.workspacePaths = null + await link.exec([]); + const links = await printLinks({ + path: resolve(npm.globalDir, '..'), + global: true, + }) + + t.matchSnapshot(links, 'should create a global link to current pkg, even within path with hash') +}) diff --git a/workspaces/arborist/lib/arborist/build-ideal-tree.js b/workspaces/arborist/lib/arborist/build-ideal-tree.js index da2652c449a1c..ea001816ef17a 100644 --- a/workspaces/arborist/lib/arborist/build-ideal-tree.js +++ b/workspaces/arborist/lib/arborist/build-ideal-tree.js @@ -603,7 +603,7 @@ Try using the package name instead, e.g: if (filepath) { const { name } = spec const tree = this.idealTree.target - spec = npa(`file:${relpath(tree.path, filepath)}`, tree.path) + spec = npa(`file:${relpath(tree.path, filepath).replace(/#/g, '%23')}`, tree.path) spec.name = name } return spec