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
- Clone the repository
- Verify that we have a nested project setup (project
sub-project
is nested inside projectmylibrary
). - Verify that there's a
build
target defined on the projectmylibrary
. - Verify that there's no
build
target defined on the projectsub-project
. - Verify that
targetDefaults
is configured to define default inputs for the targetbuild
(specifically ignoring particular folders) - Now try to run the command:
npx nx lint mylibrary
. - You will now see a linting error saying that
tslib
is not added as a dependency to thepackage.json
ofmylibrary
. - This is happening because the
@nx/dependency-checks
eslint plugin relies on a functioncollectDependenciesFromFileMap
, which will check if a project in question has abuild
target, if it does, then it'll use the input for that taget to build dependency graph. - 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 thebuild
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 :)