Skip to content

Commit

Permalink
fix(core): targets referred to by targetDependencies should be option…
Browse files Browse the repository at this point in the history
…al by default
  • Loading branch information
AgentEnder committed Mar 15, 2022
1 parent 9c25cd9 commit d97486f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/nx/src/shared/workspace.ts
Expand Up @@ -115,6 +115,16 @@ export interface TargetDependencyConfig {
* The name of the target
*/
target: string;

/**
* This determines if the target is required or optional.
*
* This defaults to true when targeting dependencies.
*
* true: This target will be skipped if it is not present on the projects
* false: This target is *required*, and a build will fail if it is not present on the selected proejcts.
*/
optional?: boolean;
}

/**
Expand Down
19 changes: 13 additions & 6 deletions packages/workspace/src/tasks-runner/run-command.ts
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path';
import { appRootPath } from 'nx/src/utils/app-root';
import type {
NxJsonConfiguration,
ProjectConfiguration,
ProjectGraph,
ProjectGraphProjectNode,
TargetDependencyConfig,
Expand Down Expand Up @@ -347,7 +348,7 @@ export function createTask({
}

function addTasksForProjectDependencyConfig(
project: ProjectGraphProjectNode,
project: ProjectGraphProjectNode<ProjectConfiguration>,
{
target,
configuration,
Expand All @@ -366,6 +367,7 @@ function addTasksForProjectDependencyConfig(
target,
configuration,
});
const optional = dependencyConfig.optional ?? true;
seenSet.add(project.name);

if (path.includes(targetIdentifier)) {
Expand All @@ -387,10 +389,15 @@ function addTasksForProjectDependencyConfig(
const depProject = projectGraph.nodes[
dep.target
] as ProjectGraphProjectNode;
if (
depProject &&
projectHasTarget(depProject, dependencyConfig.target)
) {
const hasTarget =
depProject && projectHasTarget(depProject, dependencyConfig.target);
if (!optional && !hasTarget && depProject) {
output.error({
title: `Cannot find target '${dependencyConfig.target}' for project '${depProject.name}'`,
});
process.exit(1);
}
if (depProject && hasTarget) {
addTasksForProjectTarget(
{
project: depProject,
Expand Down Expand Up @@ -428,7 +435,7 @@ function addTasksForProjectDependencyConfig(
}
}
}
} else {
} else if (!optional || project.data.targets[dependencyConfig.target]) {
addTasksForProjectTarget(
{
project,
Expand Down
8 changes: 7 additions & 1 deletion packages/workspace/src/tasks-runner/task-graph-creator.ts
Expand Up @@ -55,7 +55,13 @@ export class TaskGraphCreator {
for (const t of tasks) {
if (
t.target.project === task.target.project &&
t.target.target === dependencyConfig.target
t.target.target === dependencyConfig.target &&
!(
(dependencyConfig.optional ?? true) &&
!this.projectGraph.nodes[t.target.project]?.data?.targets[
t.target.target
]
)
) {
graph.dependencies[task.id].push(t.id);
}
Expand Down

0 comments on commit d97486f

Please sign in to comment.