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

Fix: nested relative link: protocal dependency symlinks #3605

Merged
merged 3 commits into from
Jun 26, 2017
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
19 changes: 19 additions & 0 deletions __tests__/commands/install/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,25 @@ test.concurrent(
},
);

test.concurrent(
'resolves the symlinks of other symlinked packages relative to the package using the link: protocol',
async () => {
await runInstall({}, 'install-link-nested', async (config): Promise<void> => {
const expectPath = path.join(config.cwd, 'node_modules', 'b');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do I understand it right?

root
  -- > link: a --> link: b

Gets installed

node_modules
  a (symlink) -> ../a
  b (symlink) -> ../a/b

Copy link
Member

@bestander bestander Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried that we touch what is inside a, probably a symlinked dependency should not participate in deduping?

And be like

node_modules/
  a (symlink) -> ../a

a/ 
  b/
  node_modules/
  b (symlink) -> ../b

I wonder if several projects will link to a, would they be modifying their contents

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the following is what gets installed:

node_modules
  a (symlink) -> ../a
  b (symlink) -> ../a/b

This is however what is getting installed on develop currently:

node_modules
  a (symlink) -> ../a
  b (symlink) -> ../b

And that means the b symlink is going nowhere. I can't comment on deduping and that behavior as I'm new to the yarn codebase and have only basic understanding on resolvers in general and ExoticResolver logic specifically 😕

I can dig into it though and add a test case regarding:

I wonder if several projects will link to a, would they be modifying their contents

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, looks like it is fine then.
We'll probably be discovering more issues with transitive link: and file: dependencies, we'll deal with them as they come.
Feel free to investigate a bit more.


const stat = await fs.lstat(expectPath);
expect(stat.isSymbolicLink()).toEqual(true);

const target = await fs.readlink(expectPath);
if (process.platform !== 'win32') {
expect(target).toEqual('../a/b');
} else {
expect(target).toMatch(/[\\\/]b[\\\/]$/);
}
});
},
);

test('changes the cache path when bumping the cache version', async () => {
await runInstall({}, 'install-github', async (config): Promise<void> => {
const inOut = new stream.PassThrough();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "b",
"version": "0.0.0",
"main": "index.js"
}
1 change: 1 addition & 0 deletions __tests__/fixtures/install/install-link-nested/a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a;
8 changes: 8 additions & 0 deletions __tests__/fixtures/install/install-link-nested/a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "a",
"version": "0.0.0",
"main": "index.js",
"dependencies": {
"b": "link:b"
}
}
5 changes: 5 additions & 0 deletions __tests__/fixtures/install/install-link-nested/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"a": "link:a"
}
}
40 changes: 39 additions & 1 deletion src/resolvers/exotics/link-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import * as fs from '../../util/fs.js';

const path = require('path');

type Dependencies = {
[key: string]: string,
};

export default class LinkResolver extends ExoticResolver {
constructor(request: PackageRequest, fragment: string) {
super(request, fragment);
Expand Down Expand Up @@ -41,6 +45,40 @@ export default class LinkResolver 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('link:') && !path.isAbsolute(v)) {
if (temp === section) {
temp = Object.assign({}, section);
}
temp[k] = `link:${path.relative(this.config.cwd, path.join(loc, util.removePrefix(v, 'link:')))}`;
}
}

return temp;
}
}