From 813615ba63ac80d1d169fd8581b7f7d4bd87ea42 Mon Sep 17 00:00:00 2001 From: "Patryk (PsychoX) Ludwikowski" Date: Sun, 3 Jul 2022 21:34:30 +0200 Subject: [PATCH] fix: link failing when cwd with hash tag Fixes https://github.com/npm/cli/issues/5120 --- lib/commands/link.js | 5 +++-- test/lib/commands/link.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/commands/link.js b/lib/commands/link.js index b0b889ea787fd..2e54578a79db4 100644 --- a/lib/commands/link.js +++ b/lib/commands/link.js @@ -2,6 +2,7 @@ const fs = require('fs') const util = require('util') const readdir = util.promisify(fs.readdir) const { resolve } = require('path') +const { pathToFileURL } = require('url') const Arborist = require('@npmcli/arborist') const npa = require('npm-package-arg') @@ -122,7 +123,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 => pathToFileURL(resolve(globalTop, 'node_modules', l)).href), save, workspaces: this.workspaceNames, }) @@ -133,7 +134,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 => pathToFileURL(path).href) const globalTop = resolve(this.npm.globalDir, '..') const arb = new Arborist({ ...this.npm.flatOptions, diff --git a/test/lib/commands/link.js b/test/lib/commands/link.js index a01de0b247990..2a418ac2c9c22 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, + }) + + console.log(links); +})