Skip to content

Commit

Permalink
fix(core): fix missing top-level dependencies (#14832)
Browse files Browse the repository at this point in the history
  • Loading branch information
daiscog authored Apr 26, 2023
1 parent 2d78de5 commit 369ee09
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
99 changes: 98 additions & 1 deletion packages/workspace/src/utilities/buildable-libs-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('calculateProjectDependencies', () => {
},
};

const results = await calculateProjectDependencies(
const results = calculateProjectDependencies(
graph,
'root',
'example',
Expand Down Expand Up @@ -162,6 +162,103 @@ describe('calculateProjectDependencies', () => {
],
});
});

it('should include all top-level dependencies, even ones that are also transitive', async () => {
const graph: ProjectGraph = {
nodes: {
example: {
type: 'lib',
name: 'example',
data: {
files: [],
root: '/root/example',
targets: {
build: {
executor: 'x',
},
},
},
},
example2: {
type: 'lib',
name: 'example2',
data: {
files: [],
root: '/root/example2',
targets: {
build: {
executor: 'x',
},
},
},
},
},
externalNodes: {
'npm:formik': {
type: 'npm',
name: 'npm:formik',
data: {
packageName: 'formik',
version: '0.0.0',
},
},
'npm:foo': {
type: 'npm',
name: 'npm:foo',
data: {
packageName: 'foo',
version: '0.0.0',
},
},
},
dependencies: {
example: [
// when example2 dependency is listed first
{
source: 'example',
target: 'example2',
type: DependencyType.static,
},
{
source: 'example',
target: 'npm:formik',
type: DependencyType.static,
},
],
example2: [
// and example2 also depends on npm:formik
{
source: 'example2',
target: 'npm:formik',
type: DependencyType.static,
},
{
source: 'example2',
target: 'npm:foo',
type: DependencyType.static,
},
],
},
};

const results = calculateProjectDependencies(
graph,
'root',
'example',
'build',
undefined
);
expect(results).toMatchObject({
target: {
name: 'example',
},
topLevelDependencies: [
// expect example2 and formik as top-level deps, but not foo
expect.objectContaining({ name: 'example2' }),
expect.objectContaining({ name: 'formik' }),
],
});
});
});

describe('missingDependencies', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/workspace/src/utilities/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ function collectDependencies(
areTopLevelDeps = true
): { name: string; isTopLevel: boolean }[] {
(projGraph.dependencies[project] || []).forEach((dependency) => {
if (!acc.some((dep) => dep.name === dependency.target)) {
const existingEntry = acc.find((dep) => dep.name === dependency.target);
if (!existingEntry) {
// Temporary skip this. Currently the set of external nodes is built from package.json, not lock file.
// As a result, some nodes might be missing. This should not cause any issues, we can just skip them.
if (
Expand All @@ -153,6 +154,8 @@ function collectDependencies(
if (!shallow && isInternalTarget) {
collectDependencies(dependency.target, projGraph, acc, shallow, false);
}
} else if (areTopLevelDeps && !existingEntry.isTopLevel) {
existingEntry.isTopLevel = true;
}
});
return acc;
Expand Down

1 comment on commit 369ee09

@vercel
Copy link

@vercel vercel bot commented on 369ee09 Apr 26, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.