question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-commit hook runs for all files rather than only for those that are staged

See original GitHub issue

I am using simple configuration via package.json:

"lint-staged": {
  "*.js": ["prettier --write", "eslint", "git add"]
 }

During commit of one file, prettier is run for all files (matching *.js) in my project rather then only for the file(s) being committed.

After execution of pre-commit hook, I see that hook did modifications in some files which are marked as unstaged by git. (Those files weren’t modified before commit)

Using:

husky 0.14.3
lint-staged 6.0.0
prettier 1.8.2

I didn’t hit this issue on different projects where we are using older version of lint-stage (v 5.0.0).


Console log: image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
sudo-suhascommented, Dec 4, 2017

The problem is here:

lint-staged Loaded scripts from package.json:
{ start: 'react-scripts start',
  build: 'react-scripts build',
  test: 'react-scripts test --env=jsdom',
  eject: 'react-scripts eject',
  'mock-api-server': 'nodemon mockApiServer/mockServer.js',
  precommit: 'lint-staged --debug',
  lint: 'eslint src/**/*.js',
  prettier: 'prettier --write \'src/**/*.{js,scss}\'',
  serve: 'npm run build && mkdir -p webroot/login2 && cp -R build/* webroot/login2/ && serve -s -p 4000 webroot/' }

You have a script named prettier which takes priority. You can also see this in the logs:

lint-staged:find-bin Resolving binary for command `prettier --write`
lint-staged:find-bin `prettier` resolved to npm script - `prettier --write 'src/**/*.{js,scss}'`

This gets executed using npm:

lint-staged:run-script bin: npm
lint-staged:run-script args: [ 'run',
  'prettier',
  '--',
  '--write',
  '/Users/matejprokop/dev/web-login/src/components/Content.js' ]

On the other hand, eslint and git are resolved correctly:

lint-staged:find-bin Resolving binary for command `eslint`
lint-staged:find-bin Binary for `eslint` resolved to `/Users/matejprokop/dev/web-login/node_modules/.bin/eslint`
...

So just rename your script to something like format and you should be good.

0reactions
sudo-suhascommented, Dec 4, 2017

No problem. This behavior seems to trip quite a few people. We have been considering removing support for npm scripts or at least de-prioritizing it over installed binaries.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Run eslint "ONLY" on the staged files - Stack Overflow
I am using husky and lint-staged. But it runs the lint command for all the files in src and not on staged files...
Read more >
Pre-commit Hook - Prettier
You can use Prettier with a pre-commit tool. This can re-format your files that are marked as “staged” via git add before you...
Read more >
pre-commit
We built pre-commit to solve our hook issues. It is a multi-language package manager for pre-commit hooks. You specify a list of hooks...
Read more >
How to use git pre-commit hooks, the hard way and the easy ...
You can also run pre-commit run --all-files at any time to run your pre-commit hooks against all files in your repository. I highly...
Read more >
pre-commit: Various Ways to Run Hooks - Adam Johnson
pre-commit's main mode of operation is to run hooks against changed files when you commit. But you can also run hooks without committing, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found