Skip to content

Commit

Permalink
fix(core): all graph nodes should have targets block, even if its emp…
Browse files Browse the repository at this point in the history
…ty (#18625)
  • Loading branch information
AgentEnder committed Aug 14, 2023
1 parent 3627df4 commit b2ecea4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
58 changes: 58 additions & 0 deletions packages/nx/plugins/package-json-workspaces.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as memfs from 'memfs';

import '../src/utils/testing/mock-fs';
import { getNxPackageJsonWorkspacesPlugin } from './package-json-workspaces';

describe('nx package.json workspaces plugin', () => {
it('should build projects from package.json files', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'root',
scripts: { echo: 'echo root project' },
}),
'packages/lib-a/package.json': JSON.stringify({
name: 'lib-a',
scripts: { test: 'jest' },
}),
},
'/root'
);

const plugin = getNxPackageJsonWorkspacesPlugin('/root');

// Targets from package.json files are handled outside of `createNodes`,
// because they are recognized even if the package.json file is not included
// in the package manager workspaces configuration.
//
// If any project has a package.json file in its root directory, those scripts
// are targets regardless of this plugin. As such, all we have to do here is identify
// that the package.json represents an Nx project, and `normalizeProjectNodes`
// will handle the rest.
expect(plugin.createNodes[1]('package.json', null)).toMatchInlineSnapshot(`
{
"projects": {
"root": {
"name": "root",
"projectType": "library",
"root": ".",
"sourceRoot": ".",
},
},
}
`);
expect(plugin.createNodes[1]('packages/lib-a/package.json', null))
.toMatchInlineSnapshot(`
{
"projects": {
"lib-a": {
"name": "lib-a",
"projectType": "library",
"root": "packages/lib-a",
"sourceRoot": "packages/lib-a",
},
},
}
`);
});
});
55 changes: 55 additions & 0 deletions packages/nx/plugins/project-json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as memfs from 'memfs';

import '../src/utils/testing/mock-fs';
import { getNxProjectJsonPlugin } from './project-json';

describe('nx project.json plugin', () => {
it('should build projects from project.json', () => {
memfs.vol.fromJSON(
{
'project.json': JSON.stringify({
name: 'root',
targets: { command: 'echo root project' },
}),
'packages/lib-a/project.json': JSON.stringify({
name: 'lib-a',
targets: {
executor: 'nx:run-commands',
options: {},
},
}),
},
'/root'
);

const plugin = getNxProjectJsonPlugin('/root');
expect(plugin.createNodes[1]('project.json', null)).toMatchInlineSnapshot(`
{
"projects": {
"root": {
"name": "root",
"root": ".",
"targets": {
"command": "echo root project",
},
},
},
}
`);
expect(plugin.createNodes[1]('packages/lib-a/project.json', null))
.toMatchInlineSnapshot(`
{
"projects": {
"lib-a": {
"name": "lib-a",
"root": "packages/lib-a",
"targets": {
"executor": "nx:run-commands",
"options": {},
},
},
},
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ export function normalizeProjectTargets(
targetDefaults: NxJsonConfiguration['targetDefaults'],
projectName: string
): Record<string, TargetConfiguration> {
const targets = project.targets;
// Any node on the graph will have a targets object, it just may be empty
const targets = project.targets ?? {};

for (const target in targets) {
// We need to know the executor for use in readTargetDefaultsForTarget,
// but we haven't resolved the `command` syntactic sugar yet.
const executor =
targets[target].executor ?? targets[target].command
? 'nx:run-commands'
: null;

// Allows things like { targetDefaults: { build: { command: tsc } } }
const defaults = resolveCommandSyntacticSugar(
readTargetDefaultsForTarget(target, targetDefaults, executor),
`targetDefaults:${target}`
Expand Down

1 comment on commit b2ecea4

@vercel
Copy link

@vercel vercel bot commented on b2ecea4 Aug 14, 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-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.