Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint-staged fails to run on Github Actions workflow: ✖ Failed to get staged files! #1198

Closed
hbarcelos opened this issue Aug 12, 2022 · 8 comments

Comments

@hbarcelos
Copy link

Description

I'm abusing lint-staged a little bit to enforce linting rules are being followed on pull requests.

Linters and their configs are the only things in our stack that are installed using yarn, so it happens sometimes that people forget to run yarn install and the rules are not enforced in the pre-commit hook.

Steps to reproduce

I created a .github/workflows/lint.yml file:

name: Linting

on:
  pull_request:
    branches:
      - master

jobs:
  lint:
    strategy:
      fail-fast: true

    name: Lint Files
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        # The linter won't run on submodules
        # with:
        #   submodules: recursive
      - uses: actions/setup-node@v3
        with:
          cache: 'yarn'
      - run: yarn install --frozen-lockfile
      - name: Run lint-staged
        run: yarn lint-staged --diff="${GITHUB_BASE_REF}...${GITHUB_HEAD_REF}"
        id: lint

Debug Logs

Expand to view
$ /home/runner/work/mip21-toolkit/mip21-toolkit/node_modules/.bin/lint-staged --debug --diff=master...chore/add-ci-lint
2022-08-12T16:40:55.200Z lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  cwd: undefined,
  debug: true,
  diff: 'master...chore/add-ci-lint',
  diffFilter: undefined,
  maxArgLength: undefined,
  quiet: false,
  relative: false,
  shell: false,
  stash: true,
  verbose: false
}
2022-08-12T16:40:55.202Z lint-staged:validateOptions Validating options...
2022-08-12T16:40:55.202Z lint-staged:validateOptions Validated options!
2022-08-12T16:40:55.203Z lint-staged Unset GIT_LITERAL_PATHSPECS (was `undefined`)
2022-08-12T16:40:55.203Z lint-staged:runAll Running all linter scripts...
2022-08-12T16:40:55.203Z lint-staged:runAll Using working directory `/home/runner/work/mip21-toolkit/mip21-toolkit`
2022-08-12T16:40:55.203Z lint-staged:resolveGitRepo Resolving git repo from `/home/runner/work/mip21-toolkit/mip21-toolkit`
2022-08-12T16:40:55.203Z lint-staged:resolveGitRepo Unset GIT_DIR (was `undefined`)
2022-08-12T16:40:55.203Z lint-staged:resolveGitRepo Unset GIT_WORK_TREE (was `undefined`)
2022-08-12T16:40:55.204Z lint-staged:execGit Running git command [ 'rev-parse', '--show-prefix' ]
2022-08-12T16:40:55.230Z lint-staged:resolveGitRepo Resolved git directory to be `/home/runner/work/mip21-toolkit/mip21-toolkit`
2022-08-12T16:40:55.230Z lint-staged:resolveGitRepo Resolved git config directory to be `/home/runner/work/mip21-toolkit/mip21-toolkit/.git`
2022-08-12T16:40:55.230Z lint-staged:execGit Running git command [ 'log', '-1' ]
⚠ Skipping backup because `--diff` was used.

2022-08-12T16:40:55.237Z lint-staged:execGit Running git command [
  'diff',
  '--name-only',
  '-z',
  '--diff-filter=ACMR',
  'master...chore/add-ci-lint'
]
✖ Failed to get staged files!

Environment

  • OS: Ubuntu 22.04 (Github Actions)
  • Node.js: 16.16.0
  • lint-staged: 13.0.3
@brunnocabral
Copy link

Hey @hbarcelos.
I have same problem too. I resolved with param fetch-depth from actions/checkout

- uses: actions/checkout@v2
  with:
    fetch-depth: 0

@iiroj
Copy link
Member

iiroj commented Sep 21, 2022

I would suggest trying to fetch all remotes or something before running. The error message isn't very helpful in this case.

@limaho
Copy link

limaho commented Dec 30, 2022

how can I run this workflow using npm?

@limaho
Copy link

limaho commented Dec 30, 2022

Trying with this but I am getting

✖ Failed to get staged files!

name: Lint

on:
  pull_request:
    branches:
      - master

jobs:
  build:

    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v2
      - run: npm install 
      - name: Run lint-staged
        run: npx lint-staged --diff="${GITHUB_BASE_REF}...${GITHUB_HEAD_REF}"

@lorenburton
Copy link

@limaho @hbarcelos specifying fetch-depth: 0 and adding origin/ before the branch names resolved the issue for me:

jobs:
  linters:
    name: Linters
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'yarn'

      - name: Install packages
        run: |
          yarn install --pure-lockfile

      - name: Linters
        run: yarn lint-staged --diff="origin/${GITHUB_BASE_REF}...origin/${GITHUB_HEAD_REF}"

@limaho
Copy link

limaho commented Feb 23, 2023

@limaho @hbarcelos specifying fetch-depth: 0 and adding origin/ before the branch names resolved the issue for me:

jobs:
  linters:
    name: Linters
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'yarn'

      - name: Install packages
        run: |
          yarn install --pure-lockfile

      - name: Linters
        run: yarn lint-staged --diff="origin/${GITHUB_BASE_REF}...origin/${GITHUB_HEAD_REF}"

Works great. Thanks @lorenburton 😊

@iiroj
Copy link
Member

iiroj commented Feb 23, 2023

Is this something we could add to the README?

@GarrisonD
Copy link

For me only this works:

steps:
  - uses: actions/checkout@v3
    with: { fetch-depth: 0 }

    ...

  - name: ESLint & Prettier
    run: npx lint-staged --diff=origin/main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants