nrwl / nx

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

Nx affected exits with 0 when base ref does not exist #26553

Closed jsynacek closed 3 weeks ago

jsynacek commented 2 months ago

Current Behavior

$ npx nx affected --base=IDONTEXIST -t test
fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

 NX   Command failed: git diff --name-only --no-renames --relative "IDONTEXIST" "HEAD"

fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Pass --verbose to see the stacktrace.

Here, the underlying git command clearly exists with an error.

$ echo $?
0

But the nx affected command exists with 0.

Expected Behavior

The entire nx affected command should exit with whatever the underlying command exits with. In this particular case, it should be 128.

$ git diff --name-only --no-renames --relative "IDONTEXIST" "HEAD"
fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

$ echo $?
128

GitHub Repo

No response

Steps to Reproduce

See the description.

Nx Report

$ npx nx report

 NX   Report complete - copy this into the issue template

Node   : 20.12.2
OS     : darwin-arm64
npm    : 10.5.0

nx                 : 19.0.4
@nx/js             : 19.0.4
@nx/jest           : 19.0.4
@nx/linter         : 19.0.4
@nx/eslint         : 19.0.4
@nx/workspace      : 19.0.4
@nx/devkit         : 19.0.4
@nx/eslint-plugin  : 19.0.4
@nx/react          : 19.0.4
@nrwl/tao          : 19.0.4
@nx/web            : 19.0.4
@nx/webpack        : 19.0.4
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/webpack/plugin
@nx/eslint/plugin
@nx/jest/plugin

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

BaptisteMahe commented 1 month ago

Hello, I have the same issue. From my research, this behaviour appeared in nx@18.2.0.

When I run this command :

$ npx nx affected:build --base="0000000000000000000000000000000000000000" --head="ba25f78f9628a757f1796feb00cd4dd4c2de2c68" --configuration="dev" || echo "Failed"

Output in nx@18.1.0:

$ npx nx affected:build --base="0000000000000000000000000000000000000000" --head="ba25f78f9628a757f1796feb00cd4dd4c2de2c68" --configuration="dev" || echo "Failed"
fatal: bad object 0000000000000000000000000000000000000000
nx affected:build

Run command using --base=[SHA1] (affected by the committed, uncommitted and untracked changes):
      --base  Base of the current branch (usually main)                                                                                                                                                                      [string]

or using --base=[SHA1] --head=[SHA2] (affected by the committed changes):
      --base  Base of the current branch (usually main)                                                                                                                                                                      [string]
      --head  Latest commit of the current branch (usually HEAD)                                                                                                                                                             [string]

or using:
      --files        Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces                                                                  [string]
      --uncommitted  Uncommitted changes                                                                                                                                                                                    [boolean]
      --untracked    Untracked changes                                                                                                                                                                                      [boolean]

Options:
      --help            Show help                                                                                                                                                                                           [boolean]
      --version         Show version number                                                                                                                                                                                 [boolean]
  -c, --configuration   This is the configuration to use when performing tasks on projects                                                                                                                                   [string]
      --output-style    Defines how Nx emits outputs tasks logs                                                                                          [string] [choices: "dynamic", "static", "stream", "stream-without-prefixes"]
      --exclude         Exclude certain projects from being processed                                                                                                                                                        [string]
      --parallel        Max number of parallel processes [default is 3]                                                                                                                                                      [string]
      --runner          This is the name of the tasks runner configured in nx.json                                                                                                                                           [string]
      --graph           Show the task graph of the command. Pass a file path to save the graph data instead of viewing it in the browser.                                                                                    [string]
      --verbose         Prints additional information about the commands (e.g., stack traces)                                                                                                                               [boolean]
      --nxBail          Stop command execution after the first failed task                                                                                                                                 [boolean] [default: false]
      --nxIgnoreCycles  Ignore cycles in the task graph                                                                                                                                                    [boolean] [default: false]
      --skipNxCache     Rerun the tasks even when the results are available in the cache                                                                                                                   [boolean] [default: false]

Examples:
  affected -t custom-target                               Run custom target for all affected projects
  affected -t test --parallel=5                           Run tests in parallel
  affected -t lint test build                             Run lint, test, and build targets for affected projects. Requires Nx v15.4+
  affected -t test --files=libs/mylib/src/index.ts        Run tests for all the projects affected by changing the index.ts file
  affected -t test --base=main --head=HEAD                Run tests for all the projects affected by the changes between main and HEAD (e.g., PR)
  affected -t test --base=main~1 --head=main              Run tests for all the projects affected by the last commit on main
  affected -t=build --exclude='*,!tag:dotnet'             Run build for only projects with the tag `dotnet`
  affected -t build --tag=$NX_TASK_TARGET_PROJECT:latest  Use the currently executing project name in your command.

Find more information and examples at https://nx.dev/nx/affected

Error: Command failed: git diff --name-only --no-renames --relative "0000000000000000000000000000000000000000" "ba25f78f9628a757f1796feb00cd4dd4c2de2c68"
fatal: bad object 0000000000000000000000000000000000000000

    at genericNodeError (node:internal/errors:984:15)
    at wrappedFn (node:internal/errors:538:14)
    at checkExecSyncError (node:child_process:890:11)
    at execSync (node:child_process:962:15)
    at parseGitOutput (/Users/baptistemahe/Documents/repos/unico-workspace/node_modules/nx/src/utils/command-line-utils.js:240:41)
    at getFilesUsingBaseAndHead (/Users/baptistemahe/Documents/repos/unico-workspace/node_modules/nx/src/utils/command-line-utils.js:237:12)
    at parseFiles (/Users/baptistemahe/Documents/repos/unico-workspace/node_modules/nx/src/utils/command-line-utils.js:191:20)
    at getAffectedGraphNodes (/Users/baptistemahe/Documents/repos/unico-workspace/node_modules/nx/src/command-line/affected/affected.js:82:151)
    at Object.affected (/Users/baptistemahe/Documents/repos/unico-workspace/node_modules/nx/src/command-line/affected/affected.js:34:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  status: 128,
  signal: null,
  output: [
    null,
    <Buffer >,
    <Buffer 66 61 74 61 6c 3a 20 62 61 64 20 6f 62 6a 65 63 74 20 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 ... 9 more bytes>
  ],
  pid: 22116,
  stdout: <Buffer >,
  stderr: <Buffer 66 61 74 61 6c 3a 20 62 61 64 20 6f 62 6a 65 63 74 20 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 ... 9 more bytes>
}
Failed

Output in nx@18.2.0:

$ npx nx affected:build --base="0000000000000000000000000000000000000000" --head="ba25f78f9628a757f1796feb00cd4dd4c2de2c68" --configuration="dev" || echo "Failed"
fatal: bad object 0000000000000000000000000000000000000000

 NX   Command failed: git diff --name-only --no-renames --relative "0000000000000000000000000000000000000000" "ba25f78f9628a757f1796feb00cd4dd4c2de2c68"

fatal: bad object 0000000000000000000000000000000000000000

Pass --verbose to see the stacktrace.
sigginjals commented 1 month ago

Anyone found any workaround for this?

AgentEnder commented 1 month ago

I'm having a hard time replicating this issue. On my macbook at least, running nx affected --base FOO -t build errors with code 1.

image

@BaptisteMahe you issue is caused by the || echo Failed at the end. The echo command runs, indicating the first command exited with a non-zero code, but the whole command's status code is 0 due to how bash runs scripts.

@jsynacek Can you try to reproduce this with latest? If so, providing the exact command you see that doesn't fail would be helpful.

BaptisteMahe commented 1 month ago

@AgentEnder I put the || echo Failed at the end to show that the Failed keyword appears in one case but not the other. If it appears it means that the nx command exited with a non 0 code. As shown in the output, this echo command doesn't run on v18.2.0. Did you try with an nx version >= 18.2.0 ?

AgentEnder commented 1 month ago

I was trying with latest

sigginjals commented 1 month ago

This was causing me issues in a Github Action. The base commit didn't exists and therefore this threw an error. I fixed it by making sure it was included in the Checkout

The top of my action was:

      - uses: actions/checkout@v4
        name: Checkout [main]

Changed into:

     - uses: actions/checkout@v4
        name: Checkout [main]
        with:
          fetch-depth: 0
jsynacek commented 1 month ago

@jsynacek Can you try to reproduce this with latest?

I can confirm that this was fixed somewhere between v19.0.4 and v19.5.3. (Even though the exit code is 1 instead of whatever the exit code of the underlying command is, but that's ok I guess.)

Old (broken)

$ npx nx --version
Nx Version:
- Local: v19.0.4
- Global: Not found

$ npx nx affected --base=IDONTEXIST -t test
fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

 NX   Command failed: git diff --name-only --no-renames --relative "IDONTEXIST" "HEAD"

fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Pass --verbose to see the stacktrace.

$ echo $?
0

New (fixed)

$ npx nx --version
Nx Version:
- Local: v19.5.3
- Global: Not found

$ npx nx affected --base=IDONTEXIST -t test
fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

 NX   Command failed: git diff --name-only --no-renames --relative "IDONTEXIST" "HEAD"

fatal: ambiguous argument 'IDONTEXIST': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Pass --verbose to see the stacktrace.

$ echo $?
1
github-actions[bot] commented 3 weeks 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! 🙏