nrwl / nx

Smart Monorepos ยท Fast CI
https://nx.dev
MIT License
23.21k stars 2.31k forks source link

Inputs don't work for root-level targets #18518

Closed miluoshi closed 2 weeks ago

miluoshi commented 1 year ago

Current Behavior

When I set inputs property for root-level target defined as NPM script in package.json, cache hash is changed when any file in the repository is updated. The target is defined in cacheableOperations so it will run cached with consecutive runs with no file changes.

Expected Behavior

According to the docs we should be able to define inputs in package.json's nx.targets.<target_name>.inputs property.

I've tried defining inputs either in package.json or in nx.json's targetDefaults, but none of it affected hash calculation

GitHub Repo

No response

Steps to Reproduce

I was trying to implement a root-level target that validates URL links in all markdown files:

  1. Add a new npm script to package.json and enable root-level targets by adding the "nx" property:

    {
      "scripts": {
        "markdown-link-check": "sleep 3; echo 'markdown links checked'"
      },
      "nx": {}
    }
  2. Add this target to a cacheableOperations in nx.json:

    {
      "tasksRunnerOptions": {
        "options": {
          "cacheableOperations": ["build", "lint", "test", "markdown-link-check"]
        }
      }
    }
  3. Define inputs for this target either...

    a.) in package.json:

    "nx": {
        "targets": {
            "markdown-link-check": {
              "inputs": ["{workspaceRoot}/**/*.md"]
            }
        }
    }

    b.) or in nx.json:

    "targetDefaults": {
        "markdown-link-check": {
          "inputs": ["{workspaceRoot}/**/*.md"]
        }
    }
  4. Run yarn nx markdown-link-check or yarn nx run <wokrspace_name>:markdown-link-check and observe that the target runs successfully

  5. Run it again and observe the Nx cache hit

     >  NX   Successfully ran target markdown-link-check for project <workspace_name> (20ms)
    
    Nx read the output from the cache instead of running the command for 1 out of 1 tasks.
  6. Update any file not targeted by the inputs pattern

  7. Run the command again and observe that it is not retrieved from the cache, but it has been run fully

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 20.5.0
   OS     : darwin-arm64
   yarn   : 1.22.19

   nx                 : 16.5.5
   @nx/js             : 16.5.5
   @nx/jest           : 16.5.5
   @nx/linter         : 16.5.5
   @nx/workspace      : 16.5.5
   @nx/angular        : 16.5.5
   @nx/cypress        : 16.5.5
   @nx/devkit         : 16.5.5
   @nx/eslint-plugin  : 16.5.5
   @nrwl/nx-plugin    : 16.0.0-beta.1
   @nx/react          : 16.5.5
   @nx/rollup         : 16.5.5
   @nx/storybook      : 16.5.5
   @nrwl/tao          : 16.5.5
   @nx/web            : 16.5.5
   @nx/webpack        : 16.5.5
   typescript         : 5.1.3
   ---------------------------------------
   Community plugins:
   @ngrx/component-store    : 16.0.1
   @ngrx/effects            : 16.0.1
   @ngrx/entity             : 16.0.1
   @ngrx/schematics         : 16.0.1
   @ngrx/store              : 16.0.1
   @nx/nx-plugin            : 16.0.0-beta.1
   @storybook/angular       : 7.1.0
   @testing-library/angular : 14.1.1
   ng2-charts               : 4.1.1
   ---------------------------------------
>  NX   Report complete - copy this into the issue template

   Node   : 20.5.0
   OS     : darwin-arm64
   yarn   : 1.22.19

   nx                 : 16.5.5
   @nx/js             : 16.5.5
   @nx/jest           : 16.5.5
   @nx/linter         : 16.5.5
   @nx/workspace      : 16.5.5
   @nx/angular        : 16.5.5
   @nx/cypress        : 16.5.5
   @nx/devkit         : 16.5.5
   @nx/eslint-plugin  : 16.5.5
   @nrwl/nx-plugin    : 16.0.0-beta.1
   @nx/react          : 16.5.5
   @nx/rollup         : 16.5.5
   @nx/storybook      : 16.5.5
   @nrwl/tao          : 16.5.5
   @nx/web            : 16.5.5
   @nx/webpack        : 16.5.5
   typescript         : 5.1.3
   ---------------------------------------
   Community plugins:
   @ngrx/component-store    : 16.0.1
   @ngrx/effects            : 16.0.1
   @ngrx/entity             : 16.0.1
   @ngrx/schematics         : 16.0.1
   @ngrx/store              : 16.0.1
   @nx/nx-plugin            : 16.0.0-beta.1
   @storybook/angular       : 7.1.0
   @testing-library/angular : 14.1.1
   ng2-charts               : 4.1.1
   ---------------------------------------
   Local workspace plugins:
         /* redacted */
   ---------------------------------------
   The following packages should match the installed version of nx
     - @nrwl/nx-plugin@16.0.0-beta.1

   To fix this, run `nx migrate nx@16.5.5`

Failure Logs

No response

Operating System

Additional Information

No response

miluoshi commented 7 months ago

I've run into this again. Since cacheableOperations option was replaced with targetDefaults.<target_name>.cache = true inside nx.json, I've tried it again with Nx 17.1.2 and the result is the same - files not included in inputs still invalidate task graph.

I've tried to define inputs on 2 places:

with both options the result is the same - cache is invalidated regardless of inputs.

miluoshi commented 5 months ago

Could someone please validate whether this is a reproducible bug? ๐Ÿ™

AgentEnder commented 1 month ago

I can't validate this issue - using the Nx repo as an example we recently defined inputs + outputs for our pnpm documentation script. I validated that changing the e2e files didn't invalidate the script. Can you try with latest?

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because no results of retrying on the latest version of Nx was provided within 7 days. It will be closed in 21 days if no results are provided. If the issue is still present, please reply to keep it active. If the issue was not present, please close this issue. Thanks for being a part of the Nx community! ๐Ÿ™