Skip to content

Commit 33ec41f

Browse files
jsnajdrruyadorno
authored andcommitted
fix: relativize file links when inflating shrinkwrap
PR-URL: #758 Credit: @jsnajdr Close: #758 Reviewed-by: @ruyadorno
1 parent b411f76 commit 33ec41f

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

lib/install/deps.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ function doesChildVersionMatch (child, requested, requestor) {
7474
var childReq = child.package._requested
7575
if (childReq) {
7676
if (childReq.rawSpec === requested.rawSpec) return true
77-
if (childReq.type === requested.type && childReq.saveSpec === requested.saveSpec) return true
77+
if (childReq.type === requested.type) {
78+
if (childReq.saveSpec === requested.saveSpec) return true
79+
if (childReq.fetchSpec === requested.fetchSpec) return true
80+
}
7881
}
7982
// If _requested didn't exist OR if it didn't match then we'll try using
8083
// _from. We pass it through npa to normalize the specifier.
@@ -200,6 +203,7 @@ function removeObsoleteDep (child, log) {
200203
})
201204
}
202205

206+
exports.packageRelativePath = packageRelativePath
203207
function packageRelativePath (tree) {
204208
if (!tree) return ''
205209
var requested = tree.package._requested || {}
@@ -570,7 +574,7 @@ function addDependency (name, versionSpec, tree, log, done) {
570574
try {
571575
var req = childDependencySpecifier(tree, name, versionSpec)
572576
if (tree.swRequires && tree.swRequires[name]) {
573-
var swReq = childDependencySpecifier(tree, name, tree.swRequires[name], tree.package._where)
577+
var swReq = childDependencySpecifier(tree, name, tree.swRequires[name])
574578
}
575579
} catch (err) {
576580
return done(err)

lib/install/get-requested.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict'
22
const npa = require('npm-package-arg')
33
const moduleName = require('../utils/module-name.js')
4-
4+
const packageRelativePath = require('./deps').packageRelativePath
55
module.exports = function (child, reqBy) {
66
if (!child.requiredBy.length) return
77
if (!reqBy) reqBy = child.requiredBy[0]
88
const deps = reqBy.package.dependencies || {}
99
const devDeps = reqBy.package.devDependencies || {}
10+
const optDeps = reqBy.package.optionalDependencies || {}
1011
const name = moduleName(child)
11-
return npa.resolve(name, deps[name] || devDeps[name], reqBy.realpath)
12+
const spec = deps[name] || devDeps[name] || optDeps[name]
13+
const where = packageRelativePath(reqBy)
14+
return npa.resolve(name, spec, where)
1215
}

lib/install/inflate-shrinkwrap.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ function tarballToVersion (name, tb) {
8989
return match[2] || match[1]
9090
}
9191

92+
function relativizeLink (name, spec, topPath, requested) {
93+
if (!spec.startsWith('file:')) {
94+
return
95+
}
96+
97+
let requestedPath = requested.fetchSpec
98+
if (requested.type === 'file') {
99+
requestedPath = path.dirname(requestedPath)
100+
}
101+
102+
const relativized = path.relative(requestedPath, path.resolve(topPath, spec.slice(5)))
103+
return 'file:' + relativized
104+
}
105+
92106
function inflatableChild (onDiskChild, name, topPath, tree, sw, requested, opts) {
93107
validate('OSSOOOO|ZSSOOOO', arguments)
94108
const usesIntegrity = (
@@ -101,7 +115,14 @@ function inflatableChild (onDiskChild, name, topPath, tree, sw, requested, opts)
101115
sw.resolved = sw.version
102116
sw.version = regTarball
103117
}
104-
if (sw.requires) Object.keys(sw.requires).map(_ => { sw.requires[_] = tarballToVersion(_, sw.requires[_]) || sw.requires[_] })
118+
if (sw.requires) {
119+
Object.keys(sw.requires).forEach(name => {
120+
const spec = sw.requires[name]
121+
sw.requires[name] = tarballToVersion(name, spec) ||
122+
relativizeLink(name, spec, topPath, requested) ||
123+
spec
124+
})
125+
}
105126
const modernLink = requested.type === 'directory' && !sw.from
106127
if (hasModernMeta(onDiskChild) && childIsEquivalent(sw, requested, onDiskChild)) {
107128
// The version on disk matches the shrinkwrap entry.

test/tap/install-dep-classification.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test('optional dependency identification', function (t) {
126126
optional: true
127127
},
128128
example: {
129-
version: '1.0.0',
129+
version: 'file:../example-1.0.0.tgz',
130130
optional: true
131131
}
132132
}
@@ -150,7 +150,7 @@ test('development dependency identification', function (t) {
150150
dev: true
151151
},
152152
example: {
153-
version: '1.0.0',
153+
version: 'file:../example-1.0.0.tgz',
154154
dev: true
155155
}
156156
}
@@ -173,7 +173,7 @@ test('default dependency identification', function (t) {
173173
optional: true
174174
},
175175
example: {
176-
version: '1.0.0',
176+
version: 'file:../example-1.0.0.tgz',
177177
optional: true
178178
}
179179
}

0 commit comments

Comments
 (0)