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.

Allowing non-zero exit code in some of the commands to support pre-commit autofixing

See original GitHub issue

Description

This issue is essentially a reverse of #26 🙂

We use eslint and prettier as linting tools and our typical package.json looks like this:

{
  "scripts": {
    "fix": "yarn fix:eslint && yarn fix:prettier",
    "fix:eslint": "eslint --fix \"**/*\"",
    "fix:prettier": "prettier --write \"**/*\"",
    "lint": "yarn lint:eslint && yarn lint:prettier",
    "lint:eslint": "eslint \"**/*\"",
    "lint:prettier": "prettier --check \"**/*\""
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "**/*": [
      "eslint --fix",
      "prettier --write",
      "git add"
    ]
  },
  "devDependencies": {
    "eslint": "^5.16.0",
    "eslint-config-prettier": "^4.3.0",
    "husky": "^2.3.0",
    "lint-staged": "^8.1.7",
    "prettier": "^1.17.1"
  }
}
  • We want to involve lint-staged as a pre-commit hook.
  • We want eslint and prettier to fix autofixable problems, but not block the commit if unfixable errors or warnings are found.
  • The reason why we use **/* everywhere is because we specify matched extensions in .eslintignore / .prettierignore – helps keep things DRY.

This strategy does not fully work at the moment: every time eslint --fix finds a problem, it exits with a non-zero exit code and so the commit is blocked. This is a reasonable default behaviour, however, it is not quite what we are after.

As in many other workflows, we merge PRs to master only when yarn lint passes in the CI. This means that the last commit in a PR needs to be crystal clean, however any work-in-progress commits to a new branch can include imperfections. Typical examples of acceptable issues would be console.log not yet removed from the code (because of ongoing debugging) or present unused variables (because the implementation is incomplete). A precommit hook that autofixes formatting and, say, sorts imports, is useful, however a superstrict guard that blocks a commit is an obstacle to the process. We want to commit and push imperfect code to new branches as soon as possible to avoid loss of work, we just want to stay as close to perfect as automation allows us.

What we can do now is to implement a wrapper around eslint --fix (e.g. wrapped-eslint-fix) that would always exit with zero. It would be used like this:

  "lint-staged": {
    "**/*": [
      "wrapped-eslint-fix",
      "prettier --write",
      "git add"
    ]
  },

However, to make it easier to re-use the same trick in multiple projects, it would be nice to be able to do something like this:

  "lint-staged": {
    "**/*": [
      // option 1
      ["eslint --fix", { ignoreExitCode: true }],
      // option 2
      { command: "eslint --fix", ignoreExitCode: true },
      // option 3
      { command: "eslint --fix", options: { ignoreExitCode: true }},
      // remaining commands can keep the current syntax, but may also switch to the new one
      "prettier --write",
      "git add"
    ]
  },

New syntax is similar to what we see in eslint and webpack configs:

"eslintRule1": "error",
"eslintRule2": [ "error", { someOption: true } ],

"use": [
  "some-webpack-loader",
  { loader: "some-other-loader", options: { foo: "bar" } },
]

The potential new feature seems backwards-compatible and may support the development process in teams like ours. WDYT?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
kachkaevcommented, May 19, 2019

I totally understand your arguments and also value tools that are dead-simple on the surface. I’ve opened this issue to share our situation and to discover if anyone else faces a similar problem.

This will probably work for us:

 "lint-staged": {
   "**/*": [
-    "eslint --fix",
-    "prettier --write",
+    "suppress-exit-code eslint --fix",
+    "suppress-exit-code prettier --write",
     "git add"
   ]
},

Just published suppress-exit-code to npm.

@okonet feel free to close the issue if you want and thank you for taking part in this conversation. If anyone else seeks for suppressing exit codes, please give a shout below – this will help others know if extending lint-staged is worth it.

1reaction
tdkncommented, Nov 18, 2021

I use this workaround:

// lint-staged.config.js

const silent = shell => `sh -c '${shell} || exit 0'`

module.exports = {
  "**/*": (files) => {
    return `prettier --write --ignore-unknown ${files.join(" ")}`;
  },
  "*.{js,jsx,ts,tsx}": (files) => {
    return silent(`eslint --fix ${files.join(" ")}`);
  },
  "*.{css,scss}": (files) => {
    return silent(`stylelint --fix ${files.join(" ")}`);
  },
};

The important points are:

Read more comments on GitHub >

github_iconTop Results From Across the Web

pre-commit
Each git repo can support as many languages/hooks as you want. The hook must exit nonzero on failure or modify files. A git...
Read more >
Reasons for git commit failure (exiting with non-zero)
A pre-commit hook exits nonzero, aborting the commit. ... here mean generated commit only, or should it include the command's exit status?)
Read more >
CLI reference - Semgrep
Reference for the Semgrep command line tool including options and exit code behavior.
Read more >
GitHooks with PowerShell on Windows to automate source ...
Git for Windows supports Bash commands and shell scripts via Cygwin. ... The hook should # exit with non-zero status after issuing an ......
Read more >
Improve Code Quality with Git Hooks and Pre-commit
You specify a list of checks (hooks) in a configuration file which will be automatically executed when the git commit command is called....
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