nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
22.69k stars 2.27k forks source link

Inferred tasks collide with scripts in package.json #26331

Closed jsmecham closed 1 month ago

jsmecham commented 1 month ago

Current Behavior

When using inferred tasks as part of Project Crystal, an infinite loop occurs when the package.json of a project uses a script name that matches an inferred task name.

For example, when running nx build test-a (see configuration below), it just continuously invokes nx build:

% npx nx build test-a

> nx run test-a:build

> test-a@0.0.0 build
> nx build

> nx run test-a:build

> test-a@0.0.0 build
> nx build

> nx run test-a:build

> test-a@0.0.0 build
> nx build

> nx run test-a:build

> test-a@0.0.0 build
> nx build

... and so on.

Configuration

nx.json

{
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "extends": "nx/presets/npm.json",
  "plugins": [
    {
      "plugin": "@nx/vite/plugin",
      "options": {
        "buildTargetName": "build"
      }
    }
  ]
}

packages/test-a/package.json

{
  "name": "my-project",
  "version": "0.0.0",
  "scripts": {
    "build": "nx build"
  }
}

packages/test-a/project.json

{
  "name": "test-a",
  "$schema": "../../node_modules/nx/schemas/project-schema.json"
}

Expected Behavior

I would expect the inferred task to continue to work by calling nx build. The use case here is that I'd like my team to be able to use both npm run build (muscle memory) and npx nx build to perform the same thing and to ease adoption of Nx.

An option to disable inferring package.json scripts as tasks would be helpful, but at the very least, this infinite loop behavior should be recognized or documented.

GitHub Repo

https://github.com/jsmecham/nx-inferred-tasks-with-package-scripts

Steps to Reproduce

  1. clone the repository
  2. run npx nx build test-b and observe working behavior
  3. run npx nx build test-a and observe broken behavior

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.11.0
OS     : darwin-arm64
npm    : 10.4.0

nx             : 19.1.2
@nx/js         : 19.1.2
@nx/workspace  : 19.1.2
@nx/devkit     : 19.1.2
@nrwl/tao      : 19.1.2
@nx/vite       : 19.1.2
@nx/web        : 19.1.2
typescript     : 5.3.3
---------------------------------------
Registered Plugins:
@nx/vite/plugin

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

FrozenPandaz commented 1 month ago

The above repo does not seem to have any of the package.json scripts that you described. Could you check if your changes were pushed please?

There are several things which are already in place for this sort of issue.

  1. When running nx add @nx/vite, Nx will configure the plugin added to not conflict with any existing scripts. So in cases where you have a package.json script called build (which also creates an Nx target named build), the @nx/vite/plugin plugin will be configured to create vite:build targets instead.
  2. You can switch to only including some or non of the package.json scripts in Nx by setting nx.includedScripts = [] in your package.json. This will create only the script names in that array in Nx.

Hopefully one of those above helps your issue. If not, please verify that your changes have been pushed and I can take a look.

jsmecham commented 1 month ago

Oops! I forgot to push. I've done so now.

However, includedScripts looks like it does exactly what I want. For some reason, I did not see that section as I was scouring for an option. My apologies!

github-actions[bot] commented 1 week ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.