Skip to content

using --fail-on-changes does not create stash and discards all unstaged changes from partially staged file #1666

@movy

Description

@movy

I am just starting with lint-staged, and instantly hit this issue :( Luckily, I was experimenting on a small change and was able to recover my losses from VSCode file history. This is not related to VSCode per se, as repro below is done purely from command line and produced the same outcome.

The most basic config:

"*.{js,jsx}": "eslint"

git version 2.34.1

Repro steps:

  • stage a single line from a file, to make staged version trigger linting error
  • run npx lint-staged --fail-on-changes
  • surprisingly, lint-staged fails, despite our config witheslint without --fix flag
  • ALL unstaged changes to the staged file are GONE. And stash is not created!

Running without --fail-on-changes flag works fine, but I would not want unverified linter's modifications in my repo, so really want to make this work.

Full log is below.

p.s. In addition, I also managed to wipe out ALL unstaged changes to all files when ran npx lint-staged --fail-on-changes --hide-unstaged. As those changes included freshly installed lint-staged config, it ended up with a very vanilla error message from git: No valid configuration found, which defo can be improved (example of confusion: desktop/desktop#16870). I have also checked #1657 and #1402, and do not think my case falls into those.

Repro log:

$ git status    
Changes not staged for commit:
        modified:   bots/restart-trading.js

### STAGED SINGLE LINE from restart-trading.js to produce linting error on staged code

$ git status
Changes to be committed:
        modified:   bots/restart-trading.js

Changes not staged for commit:
        modified:   bots/restart-trading.js

$ npx lint-staged --fail-on-changes
✔ Backed up original state in git stash (b3adf7b5)
✔ Hiding unstaged changes to partially staged files...
⚠ Running tasks for staged files...
  ❯ package.json — 1 file
    ❯ *.{js,jsx} — 1 file
      ✖ eslint [FAILED]
    ↓ *.mjs — no files
    ↓ **/*.ts?(x) — no files
✖ Tasks modified files and --fail-on-changes was used!
↓ Skipped because of errors from tasks.
✔ Cleaning up temporary files...

✖ lint-staged failed because `--fail-on-changes` was used.

✖ eslint:
(node:2632900) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

/home/ubuntu/jesse-bot/bots/restart-trading.js
  260:35  error  'maxUptime' is not defined  no-undef
  393:24  error  'maxUptime' is not defined  no-undef

✖ 2 problems (2 errors, 0 warnings)

$ git status
Changes to be committed:
        modified:   bots/restart-trading.js

### ALL UNSTAGED CHANGES ARE GONE!!!

$ git stash show
No stash entries found.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions