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

Handle case where peerDep is unresolvable #546

Merged
merged 1 commit into from
Sep 23, 2020
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
31 changes: 23 additions & 8 deletions packages/core/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,30 @@ export default class Package {
@Memoize()
get dependencies(): Package[] {
let names = flatMap(this.dependencyKeys, key => Object.keys(this.packageJSON[key] || {}));
return names.map(name => {
if (this.nonResolvableDeps) {
let dep = this.nonResolvableDeps.get(name);
if (dep) {
return dep;
return names
.map(name => {
if (this.nonResolvableDeps) {
let dep = this.nonResolvableDeps.get(name);
if (dep) {
return dep;
}
}
}
return this.packageCache.resolve(name, this);
});
try {
return this.packageCache.resolve(name, this);
} catch (error) {
// if the package was not found do not error out here. this is relevant
// for the case where a package might be an optional peerDependency and we dont
// want to error if it was not found. Additionally, erroring here is "far" away
// from the actual logical failure point and so not failing here will provide a better
// error message down the line
if (error.code === 'MODULE_NOT_FOUND') {
return false;
}

throw error;
}
})
.filter(Boolean) as Package[];
}

hasDependency(name: string): boolean {
Expand Down
39 changes: 39 additions & 0 deletions packages/core/tests/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,43 @@ describe('package', () => {
expect(nonResolvableDeps.get('good')).toBeTruthy();
}
});

test('it does not throw if the peerDependency is not resolvable', () => {
let { name: tmpLocation } = tmp.dirSync();
let projectJSON = {
'package.json': JSON.stringify({
name: 'foobar-web',
dependencies: {
good: '*',
},
peerDependencies: {
bad: '*',
'valid-peer': '*',
},
}),
node_modules: {
good: {
'package.json': JSON.stringify({
name: 'good',
}),
},
'valid-peer': {
'package.json': JSON.stringify({
name: 'valid-peer',
}),
},
},
};

fixturify.writeSync(tmpLocation, projectJSON);

let packageCache = new PackageCache();
let packageInstance = new Package(tmpLocation, packageCache);
let dependencies = packageInstance.dependencies;

expect(dependencies.length).toBe(2);
expect(dependencies.map(dep => dep.name)).toContain('good');
expect(dependencies.map(dep => dep.name)).toContain('valid-peer');
expect(dependencies.map(dep => dep.name)).not.toContain('bad');
});
});