From 84db8bcc93e558c5b96675c92824eca042060483 Mon Sep 17 00:00:00 2001 From: Alex Parker Date: Thu, 27 Oct 2016 15:50:38 -0700 Subject: [PATCH 1/2] normalize manifest in FileResolver --- __tests__/commands/install.js | 9 +++++ .../install-file-relative/package.json | 5 +++ .../install-file-relative/root-a/index.js | 1 + .../install-file-relative/root-a/package.json | 4 ++ .../install-file-relative/root-b/package.json | 7 ++++ .../sub/sub-a/package.json | 7 ++++ src/resolvers/exotics/file-resolver.js | 39 ++++++++++++++++++- 7 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 __tests__/fixtures/install/install-file-relative/package.json create mode 100644 __tests__/fixtures/install/install-file-relative/root-a/index.js create mode 100644 __tests__/fixtures/install/install-file-relative/root-a/package.json create mode 100644 __tests__/fixtures/install/install-file-relative/root-b/package.json create mode 100644 __tests__/fixtures/install/install-file-relative/sub/sub-a/package.json diff --git a/__tests__/commands/install.js b/__tests__/commands/install.js index 358c4180d9..acbac8f70a 100644 --- a/__tests__/commands/install.js +++ b/__tests__/commands/install.js @@ -76,6 +76,15 @@ test.concurrent('root install with optional deps', (): Promise => { return runInstall({}, 'root-install-with-optional-dependency'); }); +test.concurrent('install file: protocol with relative paths', (): Promise => { + return runInstall({noLockfile: true}, 'install-file-relative', async (config) => { + assert.equal( + await fs.readFile(path.join(config.cwd, 'node_modules', 'root-a', 'index.js')), + 'foobar\n', + ); + }); +}); + test.concurrent('install file: protocol', (): Promise => { return runInstall({noLockfile: true}, 'install-file', async (config) => { assert.equal( diff --git a/__tests__/fixtures/install/install-file-relative/package.json b/__tests__/fixtures/install/install-file-relative/package.json new file mode 100644 index 0000000000..0b3d8b022b --- /dev/null +++ b/__tests__/fixtures/install/install-file-relative/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "sub-a": "file:sub/sub-a" + } +} \ No newline at end of file diff --git a/__tests__/fixtures/install/install-file-relative/root-a/index.js b/__tests__/fixtures/install/install-file-relative/root-a/index.js new file mode 100644 index 0000000000..323fae03f4 --- /dev/null +++ b/__tests__/fixtures/install/install-file-relative/root-a/index.js @@ -0,0 +1 @@ +foobar diff --git a/__tests__/fixtures/install/install-file-relative/root-a/package.json b/__tests__/fixtures/install/install-file-relative/root-a/package.json new file mode 100644 index 0000000000..08c97953a2 --- /dev/null +++ b/__tests__/fixtures/install/install-file-relative/root-a/package.json @@ -0,0 +1,4 @@ +{ + "name": "root-a", + "version": "1.0.0" +} \ No newline at end of file diff --git a/__tests__/fixtures/install/install-file-relative/root-b/package.json b/__tests__/fixtures/install/install-file-relative/root-b/package.json new file mode 100644 index 0000000000..62e3715504 --- /dev/null +++ b/__tests__/fixtures/install/install-file-relative/root-b/package.json @@ -0,0 +1,7 @@ +{ + "name": "root-b", + "version": "1.0.0", + "dependencies": { + "root-a": "file:../root-a" + } +} \ No newline at end of file diff --git a/__tests__/fixtures/install/install-file-relative/sub/sub-a/package.json b/__tests__/fixtures/install/install-file-relative/sub/sub-a/package.json new file mode 100644 index 0000000000..938c4be8bc --- /dev/null +++ b/__tests__/fixtures/install/install-file-relative/sub/sub-a/package.json @@ -0,0 +1,7 @@ +{ + "name": "sub-a", + "version": "1.0.0", + "dependencies": { + "root-b": "file:../../root-b" + } +} \ No newline at end of file diff --git a/src/resolvers/exotics/file-resolver.js b/src/resolvers/exotics/file-resolver.js index 134c0f7ae2..4a780eaae5 100644 --- a/src/resolvers/exotics/file-resolver.js +++ b/src/resolvers/exotics/file-resolver.js @@ -10,6 +10,10 @@ import * as fs from '../../util/fs.js'; const invariant = require('invariant'); const path = require('path'); +type Dependencies = { + [key: string]: string +}; + export default class FileResolver extends ExoticResolver { constructor(request: PackageRequest, fragment: string) { super(request, fragment); @@ -41,6 +45,39 @@ export default class FileResolver extends ExoticResolver { manifest._uid = manifest.version; - return manifest; + const dependencies = this.normalizeDependencyPaths(manifest.dependencies, loc); + const optionalDependencies = this.normalizeDependencyPaths(manifest.optionalDependencies, loc); + + if (dependencies !== manifest.dependencies || optionalDependencies !== manifest.optionalDependencies) { + const _manifest = Object.assign({}, manifest); + if (dependencies != null) { + _manifest.dependencies = dependencies; + } + if (optionalDependencies != null) { + _manifest.optionalDependencies = optionalDependencies; + } + return _manifest; + } else { + return manifest; + } + } + + normalizeDependencyPaths(section: ?Dependencies, loc: string): ?Dependencies { + if (section == null) { + return section; + } + + let temp = section; + + for (const [k, v] of util.entries(section)) { + if (typeof v === 'string' && v.startsWith('file:') && !path.isAbsolute(v)) { + if (temp === section) { + temp = Object.assign({}, section); + } + temp[k] = `file:${path.relative(this.config.cwd, path.join(loc, util.removePrefix(v, 'file:')))}`; + } + } + + return temp; } } From 25666553d34dda23a7856f57e16eceeb67305b27 Mon Sep 17 00:00:00 2001 From: Alex Parker Date: Thu, 27 Oct 2016 17:06:41 -0700 Subject: [PATCH 2/2] add a comment --- src/resolvers/exotics/file-resolver.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/resolvers/exotics/file-resolver.js b/src/resolvers/exotics/file-resolver.js index 4a780eaae5..44b4c6c587 100644 --- a/src/resolvers/exotics/file-resolver.js +++ b/src/resolvers/exotics/file-resolver.js @@ -45,6 +45,7 @@ export default class FileResolver extends ExoticResolver { manifest._uid = manifest.version; + // Normalize relative paths; if anything changes, make a copy of the manifest const dependencies = this.normalizeDependencyPaths(manifest.dependencies, loc); const optionalDependencies = this.normalizeDependencyPaths(manifest.optionalDependencies, loc);