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.

Eslint not producing the error messages when run as a precommit hook using lint-staged

See original GitHub issue

Description

Bug, eslint not producing the error messages when run as a precommit hook using lint-staged

Steps to reproduce

Simply create a react app using create-react-app and fill in the following configs

Here’s my configuration

package.json

{
	"name": "hello-world",
	"version": "0.1.0",
	"private": true,
	"dependencies": {
		"react": "^16.8.6",
		"react-dom": "^16.8.6",
		"react-scripts": "3.0.1"
	},
	"scripts": {
		"start": "react-scripts start",
		"build": "react-scripts build",
		"test": "react-scripts test",
		"eject": "react-scripts eject"
	},
	"husky": {
		"hooks": {
			"pre-commit": "lint-staged"
		}
	},
	"lint-staged": {
		"*.js": [
			"eslint --fix",
			"git add"
		],
		"*.{js, jsx, json, html, css}": [
			"prettier --write",
			"git add"
		]
	},
	"eslintConfig": {
		"extends": "react-app"
	},
	"browserslist": {
		"production": [
			">0.2%",
			"not dead",
			"not op_mini all"
		],
		"development": [
			"last 1 chrome version",
			"last 1 firefox version",
			"last 1 safari version"
		]
	},
	"devDependencies": {
		"eslint": "^5.16.0",
		"eslint-config-prettier": "^6.0.0",
		"eslint-plugin-prettier": "^3.1.0",
		"husky": "^3.0.0",
		"lint-staged": "^9.2.0",
		"prettier": "^1.18.2"
	}
}

Now if I run that simply on the project like, ./node_modules/.bin/eslint . , it is working as usual. Displays the changes that need to be made in the file but does not work as a pre-commit hook, prettier is working though. I still see that the eslint is running before every commit from the command line but I don’t see any output presented. What could be the issue?

  • Changing the line to, eslint * --fix, shows me fixes that are to be made in all files, even .css and .md files.
  • Making it eslint *.js --fix tells me, no files matching the pattern were found.
  • Even eslint . --fix doesn’t work.

My folder structure is a simple create-react-app format, if that info is needed.

I’ll post the eslintrc and prettierrc file below as well,

.prettierrc

{
"singleQuote": false
}

.eslintrc

{
"extends": ["react-app", "prettier"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
}
}

Clarifications:

  1. I had to use the longer format version of prettier configuration, as opposed to plugin:prettier/recommended because I ran into this error while trying to have a separate prettierrc config file instead of having it inline in package.json

  2. I had to use a lower version of eslint because the newer version was throwing random errors from a plain create-react-app and hence had to downgrade it as discussed here.

Debug Logs

I added an additional unused variable in src/App.js that eslint should scream about ran lint-staged. You can find that it checks the corresponding file, so I hope the blob is right but it isn’t throwing any errors on the terminal. Also attached the output when I run eslint separately below.

➜  hello-world git:(master) ✗ ./node_modules/.bin/lint-staged --debug
  lint-staged:bin Running `lint-staged@9.2.0` +0ms
  lint-staged Loading config using `cosmiconfig` +0ms
  lint-staged Successfully loaded config from `/Users/nobody1/leisure/learning/react/hello-world/packag
e.json`:
  lint-staged { '*.{js, jsx}': [ 'eslint --fix', 'git add' ],
  lint-staged   '*.{js, jsx, json, html, css}': [ 'prettier --write', 'git add' ] } +7ms
  lint-staged:cfg Validating config +0ms
Running lint-staged with the following config:
{
  '*.{js, jsx}': [
    'eslint --fix',
    'git add'
  ],
  '*.{js, jsx, json, html, css}': [
    'prettier --write',
    'git add'
  ]
}
  lint-staged:run Running all linter scripts +0ms
  lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ] +0ms
  lint-staged:run Resolved git directory to be `/Users/nobody1/leisure/learning/react/hello-world` +21m
s
  lint-staged:git Running git command [ 'diff', '--staged', '--diff-filter=ACM', '--name-only' ] +21ms
  lint-staged:run Loaded list of staged files in git:
  lint-staged:run [ 'package.json', 'src/App.js' ] +16ms
  lint-staged:gen-tasks Generating linter tasks +0ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '*.{js, jsx}',
  lint-staged:gen-tasks   commands: [ 'eslint --fix', 'git add' ],
  lint-staged:gen-tasks   fileList:
  lint-staged:gen-tasks    [ '/Users/nobody1/leisure/learning/react/hello-world/src/App.js' ] } +4ms
  lint-staged:gen-tasks Generated task: 
  lint-staged:gen-tasks { pattern: '*.{js, jsx, json, html, css}',
  lint-staged:gen-tasks   commands: [ 'prettier --write', 'git add' ],
  lint-staged:gen-tasks   fileList:
  lint-staged:gen-tasks    [ '/Users/nobody1/leisure/learning/react/hello-world/src/App.js' ] } +6ms
Stashing changes... [started]
  lint-staged:git Running git command [ 'status', '--porcelain' ] +29ms
Stashing changes... [skipped]
→ No partially staged files found...
Running linters... [started]
Running tasks for *.{js, jsx} [started]
Running tasks for *.{js, jsx, json, html, css} [started]
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'eslint --fix', 'git add' ] +0ms
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'prettier --write', 'git add' ] +8ms
eslint --fix [started]
prettier --write [started]
  lint-staged:task cmd: eslint +0ms
  lint-staged:task args: [ '--fix',
  '/Users/nobody1/leisure/learning/react/hello-world/src/App.js' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
  lint-staged:task cmd: prettier +4ms
  lint-staged:task args: [ '--write',
  '/Users/nobody1/leisure/learning/react/hello-world/src/App.js' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
prettier --write [completed]
git add [started]
  lint-staged:task cmd: git +321ms
  lint-staged:task args: [ 'add',
  '/Users/nobody1/leisure/learning/react/hello-world/src/App.js' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
git add [completed]
Running tasks for *.{js, jsx, json, html, css} [completed]
eslint --fix [completed]
git add [started]
  lint-staged:task cmd: git +880ms
  lint-staged:task args: [ 'add',
  '/Users/nobody1/leisure/learning/react/hello-world/src/App.js' ] +0ms
  lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false } +0ms
git add [completed]
Running tasks for *.{js, jsx} [completed]
Running linters... [completed]
  lint-staged tasks were executed successfully! +1s

Eslint output below.

➜  hello-world git:(master) ✗ ./node_modules/.bin/eslint src/App.js --fix

/Users/nobody1/leisure/learning/react/hello-world/src/App.js
  4:5  warning  'x' is defined but never used             no-unused-vars
  4:8  warning  'madsfasdsssf' is defined but never used  no-unused-vars

✖ 2 problems (0 errors, 2 warnings)

Environment

  • OS: macOS High Sierra
  • Node.js: v11.3.0
  • lint-staged: v9.2.0,

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:24 (4 by maintainers)

github_iconTop GitHub Comments

25reactions
iirojcommented, Jul 21, 2019

I think the exit code of eslint is 0 in case of warnings, that’s why it’s not failing here. Maybe you can try running it in lint-staged with the --max-warnings=0 option, or alternatively set the rule level to error.

9reactions
791106912commented, Mar 25, 2020

I also have this problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Husky pre-commit hook not triggering using lint-staged
New to husky and having problems running pre-commit hook. pre-commit file (I have husky install script): #!/usr/bin/env sh .
Read more >
Run ESLint on git commit with Husky and Lint Staged
The answer is lint-staged. It is a package that helps in running pre-commit hooks only on files that have been changed in current...
Read more >
Lint Staged With Husky for Pre-commit Validations - YouTube
Learn how to use lint staged with husky to implement linting and formatting your code with Prettier and Eslint on pre - commit....
Read more >
How to run ESLint using a pre-commit hook - Level Up Coding
ESLint Git pre-commit hook · If you haven't initialized git for your repo then do so by running the command: git init ·...
Read more >
Supported hooks - pre-commit
blacken-docs - Run `black` on python code blocks in documentation files ... php-lint-all - Check PHP Syntax on ALL PHP staged files with...
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