Skip to content

Commit

Permalink
fix: stop working around bug fixed in [email protected]
Browse files Browse the repository at this point in the history
Removes all usage of `.replace(/#/g, '%23')` for compatibility with the new version

This is the code changes from npm#8112 isloated from the dependency update itself.

Closes:
npm/npm-package-arg#203

Credit: @TrevorBurnham
  • Loading branch information
TrevorBurnham authored and wraithgar committed Feb 18, 2025
1 parent b153927 commit 811ca29
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
14 changes: 7 additions & 7 deletions lib/commands/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Diff extends BaseCommand {
const pkgName = await this.packageName()
return [
`${pkgName}@${this.npm.config.get('tag')}`,
`file:${this.prefix.replace(/#/g, '%23')}`,
`file:${this.prefix}`,
]
}

Expand Down Expand Up @@ -134,7 +134,7 @@ class Diff extends BaseCommand {
}
return [
`${pkgName}@${a}`,
`file:${this.prefix.replace(/#/g, '%23')}`,
`file:${this.prefix}`,
]
}

Expand Down Expand Up @@ -166,7 +166,7 @@ class Diff extends BaseCommand {
}
return [
`${spec.name}@${spec.fetchSpec}`,
`file:${this.prefix.replace(/#/g, '%23')}`,
`file:${this.prefix}`,
]
}

Expand All @@ -179,7 +179,7 @@ class Diff extends BaseCommand {
}
}

const aSpec = `file:${node.realpath.replace(/#/g, '%23')}`
const aSpec = `file:${node.realpath}`

// finds what version of the package to compare against, if a exact
// version or tag was passed than it should use that, otherwise
Expand Down Expand Up @@ -212,8 +212,8 @@ class Diff extends BaseCommand {
]
} else if (spec.type === 'directory') {
return [
`file:${spec.fetchSpec.replace(/#/g, '%23')}`,
`file:${this.prefix.replace(/#/g, '%23')}`,
`file:${spec.fetchSpec}`,
`file:${this.prefix}`,
]
} else {
throw this.usageError(`Spec type ${spec.type} not supported.`)
Expand Down Expand Up @@ -281,7 +281,7 @@ class Diff extends BaseCommand {

const res = !node || !node.package || !node.package.version
? spec.fetchSpec
: `file:${node.realpath.replace(/#/g, '%23')}`
: `file:${node.realpath}`

return `${spec.name}@${res}`
})
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Link extends ArboristWorkspaceCmd {
...this.npm.flatOptions,
prune: false,
path: this.npm.prefix,
add: names.map(l => `file:${resolve(globalTop, 'node_modules', l).replace(/#/g, '%23')}`),
add: names.map(l => `file:${resolve(globalTop, 'node_modules', l)}`),
save,
workspaces: this.workspaceNames,
})
Expand All @@ -135,7 +135,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.replace(/#/g, '%23')}`)
const add = paths.map(path => `file:${path}`)
const globalTop = resolve(this.npm.globalDir, '..')
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({
Expand Down
6 changes: 3 additions & 3 deletions workspaces/arborist/lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
.catch(/* istanbul ignore next */ () => null)
if (st && st.isSymbolicLink()) {
const target = await readlink(dir)
const real = resolve(dirname(dir), target).replace(/#/g, '%23')
const real = resolve(dirname(dir), target)
tree.package.dependencies[name] = `file:${real}`
} else {
tree.package.dependencies[name] = '*'
Expand Down Expand Up @@ -522,12 +522,12 @@ module.exports = cls => class IdealTreeBuilder extends cls {

const { name } = spec
if (spec.type === 'file') {
spec = npa(`file:${relpath(path, spec.fetchSpec).replace(/#/g, '%23')}`, path)
spec = npa(`file:${relpath(path, spec.fetchSpec)}`, path)
spec.name = name
} else if (spec.type === 'directory') {
try {
const real = await realpath(spec.fetchSpec, this[_rpcache], this[_stcache])
spec = npa(`file:${relpath(path, real).replace(/#/g, '%23')}`, path)
spec = npa(`file:${relpath(path, real)}`, path)
spec.name = name
} catch {
// TODO: create synthetic test case to simulate realpath failure
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/arborist/load-actual.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ module.exports = cls => class ActualLoader extends cls {
const actualRoot = tree.isLink ? tree.target : tree
const { dependencies = {} } = actualRoot.package
for (const [name, kid] of actualRoot.children.entries()) {
const def = kid.isLink ? `file:${kid.realpath.replace(/#/g, '%23')}` : '*'
const def = kid.isLink ? `file:${kid.realpath}` : '*'
dependencies[name] = dependencies[name] || def
}
actualRoot.package = { ...actualRoot.package, dependencies }
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/arborist/load-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ module.exports = cls => class VirtualLoader extends cls {
})

for (const [name, path] of workspaces.entries()) {
lockWS[name] = `file:${path.replace(/#/g, '%23')}`
lockWS[name] = `file:${path}`
}

// Should rootNames exclude optional?
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,7 @@ module.exports = cls => class Reifier extends cls {
// path initially, in which case we can end up with the wrong
// thing, so just get the ultimate fetchSpec and relativize it.
const p = req.fetchSpec.replace(/^file:/, '')
const rel = relpath(addTree.realpath, p).replace(/#/g, '%23')
const rel = relpath(addTree.realpath, p)
newSpec = `file:${rel}`
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/consistent-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const consistentResolve = (resolved, fromPath, toPath, relPaths = false) => {
raw,
} = npa(resolved, fromPath)
if (type === 'file' || type === 'directory') {
const cleanFetchSpec = fetchSpec.replace(/#/g, '%23')
const cleanFetchSpec = fetchSpec
if (relPaths && toPath) {
return `file:${relpath(toPath, cleanFetchSpec)}`
}
Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Link extends Node {
// the path/realpath guard is there for the benefit of setting
// these things in the "wrong" order
return this.path && this.realpath
? `file:${relpath(dirname(this.path), this.realpath).replace(/#/g, '%23')}`
? `file:${relpath(dirname(this.path), this.realpath)}`
: null
}

Expand Down
2 changes: 1 addition & 1 deletion workspaces/arborist/lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ class Node {
}

for (const [name, path] of this.#workspaces.entries()) {
new Edge({ from: this, name, spec: `file:${path.replace(/#/g, '%23')}`, type: 'workspace' })
new Edge({ from: this, name, spec: `file:${path}`, type: 'workspace' })
}
}

Expand Down
6 changes: 3 additions & 3 deletions workspaces/arborist/lib/shrinkwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ class Shrinkwrap {
if (!/^file:/.test(resolved)) {
pathFixed = resolved
} else {
pathFixed = `file:${resolve(this.path, resolved.slice(5)).replace(/#/g, '%23')}`
pathFixed = `file:${resolve(this.path, resolved.slice(5))}`
}
}

Expand Down Expand Up @@ -1011,7 +1011,7 @@ class Shrinkwrap {
}

if (node.isLink) {
lock.version = `file:${relpath(this.path, node.realpath).replace(/#/g, '%23')}`
lock.version = `file:${relpath(this.path, node.realpath)}`
} else if (spec && (spec.type === 'file' || spec.type === 'remote')) {
lock.version = spec.saveSpec
} else if (spec && spec.type === 'git' || rSpec.type === 'git') {
Expand Down Expand Up @@ -1089,7 +1089,7 @@ class Shrinkwrap {
// this especially shows up with workspace edges when the root
// node is also a workspace in the set.
const p = resolve(node.realpath, spec.slice('file:'.length))
set[k] = `file:${relpath(node.realpath, p).replace(/#/g, '%23')}`
set[k] = `file:${relpath(node.realpath, p)}`
} else {
set[k] = spec
}
Expand Down

0 comments on commit 811ca29

Please sign in to comment.