Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File url normalization #1498

Merged
merged 2 commits into from
Nov 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions __tests__/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ test.concurrent('root install with optional deps', (): Promise<void> => {
return runInstall({}, 'root-install-with-optional-dependency');
});

test.concurrent('install file: protocol with relative paths', (): Promise<void> => {
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<void> => {
return runInstall({noLockfile: true}, 'install-file', async (config) => {
assert.equal(
Expand Down
5 changes: 5 additions & 0 deletions __tests__/fixtures/install/install-file-relative/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"sub-a": "file:sub/sub-a"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "root-a",
"version": "1.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "root-b",
"version": "1.0.0",
"dependencies": {
"root-a": "file:../root-a"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "sub-a",
"version": "1.0.0",
"dependencies": {
"root-b": "file:../../root-b"
}
}
40 changes: 39 additions & 1 deletion src/resolvers/exotics/file-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -41,6 +45,40 @@ export default class FileResolver extends ExoticResolver {

manifest._uid = manifest.version;

return manifest;
// 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);

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;
}
}