Skip to content

targetDefaults plugin by doesn't inherit default target inputs on projects if a target is not set. #26708

Open

Description

Current Behavior

The targetDefaults plugin that nx provides, will include every single file in a project if there is no build target explicitly defined on the project.

Inside nx.json:

"targetDefaults": {
    "build": {
      "inputs": [
        "{projectRoot}/src/**/*.ts",
        "!{projectRoot}/**/stories/**/*.ts" // Ignoring a bunch of files inside the project
      ]
    },
}

Inside a project (notice that there is no build target defined):

{
  "name": "my-project",
  "$schema": "../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "src",
  "projectType": "library",
}

Inferred targetDefaults for the "build" target:

  • {projectRoot}/**/*

This behavior happens exactly in this position: https://github.com/nrwl/nx/blob/master/packages/js/src/utils/find-npm-dependencies.ts#L99-L101

Expected Behavior

The inputs defined for a target globally should be inherited by projects.

Inferred targetDefaults for the "build" target:

  • {projectRoot}/src/**/*.ts
  • !{projectRoot}/**/stories/**/*,ts

Why should this happen?

Because the @nx/dependency-checks eslint plugin specifically looks for the "build" target's inputs to determine dependencies.

GitHub Repo

https://github.com/elevenpassin/nx-target-defaults-bug

Steps to Reproduce

  1. Clone the repository
  2. Verify that we have a nested project setup (project sub-project is nested inside project mylibrary).
  3. Verify that there's a build target defined on the project mylibrary.
  4. Verify that there's no build target defined on the project sub-project.
  5. Verify that targetDefaults is configured to define default inputs for the target build (specifically ignoring particular folders)
  6. Now try to run the command: npx nx lint mylibrary.
  7. You will now see a linting error saying that tslib is not added as a dependency to the package.json of mylibrary.
  8. This is happening because the @nx/dependency-checks eslint plugin relies on a function collectDependenciesFromFileMap, which will check if a project in question has a build target, if it does, then it'll use the input for that taget to build dependency graph.
  9. If a project doesn't have a build target defined (as in our case), then we'll end up marking every file in the project as part of the dependency graph instead of using the globally defined inputs for the build target, resulting in dependency checks eslint plugin to throw errors.

Nx Report

Node   : 18.15.0
   OS     : darwin-x64
   yarn   : 1.22.21
   
   nx                 : 18.0.2
   @nx/js             : 18.0.2
   @nx/jest           : 18.0.2
   @nx/linter         : 18.0.2
   @nx/eslint         : 18.0.2
   @nx/workspace      : 18.0.2
   @nx/cypress        : 18.0.2
   @nx/devkit         : 18.0.2
   @nx/esbuild        : 18.0.2
   @nx/eslint-plugin  : 18.0.2
   @nx/node           : 18.0.2
   @nx/plugin         : 18.0.2
   @nx/react          : 18.0.2
   @nx/remix          : 18.0.2
   @nx/storybook      : 18.0.2
   @nrwl/tao          : 18.0.2
   @nx/vite           : 18.0.2
   @nx/web            : 18.0.2
   @nx/webpack        : 18.0.2
   typescript         : 5.4.3
   ---------------------------------------
   Community plugins:
   @nx-aws-plugin/nx-aws-cache : 3.2.1
   ---------------------------------------
   Local workspace plugins:
         @pd-mfe/workspace-codeowners
         @pd-mfe/workspace-dev-server
         @pd-mfe/workspace-formatting
         @pd-mfe/workspace-typescript
         @pd-mfe/workspace-nx-plugin
         @pd-mfe/workspace-graphql
         @pd-mfe/workspace-docker
         @pd-mfe/workspace-react
         @pd-mfe/workspace-node
         @pd-mfe/workspace-npm
         @pd-mfe/workspace-s3

Failure Logs

No response

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

I am happy to work on a patch for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions