tj-actions/eslint-changed-files

[BUG] Action Fails on Commit Step Even if There is no Changes to Commit

Huzaifa-Asif opened this issue · 1 comments

Is there an existing issue for this?

  • I have searched the existing issues

Does this issue exist in the latest version?

  • I'm using the latest release

Describe the bug?

My Github Action
name: ESLint Changed Files

on:
pull_request:
types: [opened, synchronize, ready_for_review]
branches:
- 'flight/**'

jobs:
eslint-check:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x] # Define the Node.js version(s) you want the check to run on.

steps:
- name: Check out code
  uses: actions/checkout@v4
  with:
    ref: ${{ github.event.pull_request.head.sha }}
    fetch-depth: 0
  
- name: Use Node.js ${{ matrix.node-version }}
  uses: actions/setup-node@v2
  with:
    node-version: ${{ matrix.node-version }}

- name: Install dependencies
  run: npm install eslint jest eslint-plugin-jest eslint-plugin-node

- name: Action Configuration
  uses: tj-actions/eslint-changed-files@v21
  with:
    extra_args: "--fix"
    config_path: ".eslintrc.json"
    skip_annotations: true
    token: ${{ secrets.GITHUB_TOKEN }} # GitHub automatically creates a `GITHUB_TOKEN` secret to use in your workflow.
    eslint_flags: '--ext .js,.jsx,.ts,.tsx' # Specify the file extensions you want ESLint to check

- name: Fix Lint Errors
  if: ${{ ! cancelled() }}
  run: npx eslint --fix --config .eslintrc.json

- name: Verify Changed files
  if: ${{ ! cancelled() }}
  uses: tj-actions/verify-changed-files@v16
  id: verify-changed-files
  with:
    files: |
      **/*.{js,.jsx,ts,tsx}

- name: Commit outstanding changes
  if: ${{ ! cancelled() }} && steps.verify-changed-files.outputs.files_changed == 'true'
  run: |
    git config --local user.email "github-actions[bot]@users.noreply.github.com"
    git config --local user.name "github-actions[bot]"
    git add ${{ steps.verify-changed-files.outputs.changed_files }}
    git commit -m "refactor: lint fixes"

- name: Push changes
  if: ${{ ! cancelled() }} && steps.verify-changed-files.outputs.files_changed == 'true'
  uses: ad-m/github-push-action@master
  with:
    github_token: ${{ secrets.GITHUB_TOKEN }}
    branch: ${{ github.head_ref }}

To Reproduce

Check the github action provided above and you can find the issue.

What OS are you seeing the problem on?

ubuntu-latest or ubuntu-22.04

Expected behavior?

Expected behaviour should be to skip this step as steps.verify-changed-files.outputs.files_changed == 'true' condition is not true.

The reason i added ${{ ! cancelled() }} is that i have to run action on both error and warning, previously eslint was only working on warning or errors so i added this not cancelled to make it run on both cases.

Relevant log output

All the Steps work fine but the Commit Action throws this error.

Nothing specified, nothing added.
hint: Maybe you wanted to say 'git add .'?
hint: Turn this message off by running
hint: "git config advice.addEmptyPathspec false"
HEAD detached at [8](https://github.com/respond-io/respond-io/actions/runs/6925116179/job/18835294903#step:8:9)d38[9](https://github.com/respond-io/respond-io/actions/runs/6925116179/job/18835294903#step:8:10)cc67c
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   package.json

no changes added to commit (use "git add" and/or "git commit -a")
Error: Process completed with exit code 1.

Has all relevant logs been included?

  • I've included all relevant logs

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Hi @Huzaifa-Asif a couple of issues with your workflow:

  1. There’s no input named eslint_flags. I assume you meant to use the extra_args input.
  2. The error you are getting is due to installing non global dependencies which either creates or updates your package.json which isn’t detected as changed because of the filters you specified.
npm install eslint jest eslint-plugin-jest eslint-plugin-node

Hence the error since there are uncommitted changes.

I’ll suggest updating your package.json dev dependencies accordingly or install the packages globally to prevent this issue.

See: https://github.com/tj-actions/eslint-changed-files/blob/main/.github/workflows/test.yml#L19-L96 for a working version.