Skip to content

Commit

Permalink
fix: allow link from path with hash character
Browse files Browse the repository at this point in the history
  • Loading branch information
AgainPsychoX committed Jul 4, 2022
1 parent ef8d2ed commit 6b4da91
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/commands/link.js
Expand Up @@ -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,
})
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tap-snapshots/test/lib/commands/link.js.test.cjs
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/lib/commands/link.js
Expand Up @@ -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')
})
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/arborist/build-ideal-tree.js
Expand Up @@ -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
Expand Down

0 comments on commit 6b4da91

Please sign in to comment.