Skip to content

Instantly share code, notes, and snippets.

@webbertakken
Last active April 18, 2025 18:24
Show Gist options
  • Select an option

  • Save webbertakken/c2b457d39224baf701c8de1589b61555 to your computer and use it in GitHub Desktop.

Select an option

Save webbertakken/c2b457d39224baf701c8de1589b61555 to your computer and use it in GitHub Desktop.
Definitive cross-platform lint-staged config (explained)
{
// 1. Use these exact dependencies
"devDependencies": {
"husky": "=8.0.3",
"lint-staged": "=13.2.1"
}
// 2. Make sure it installs when people install dependencies
"scripts": {
"prepare": "husky install"
}
// 3. Configure checks to run simultaneously (only 1 that writes)
"lint-staged": {
"*.@(ts|tsx)": "bash -c 'tsc --skipLibCheck --noEmit'", // the `bash -c` part is to make sure tsc picks up tsconfig.json (See: https://github.com/okonet/lint-staged/issues/825#issuecomment-674575655)
"*.@(ts|tsx|js|jsx)": "eslint --max-warnings 0", // if you want to fail on warnings too, otherwise simply `eslint`
"*.@(ts|tsx|js|jsx|json|jsonc|json5|md|mdx|yaml|yml)": "prettier --write" // automatically fix formatting errors
}
}
# .husky/pre-commit
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
# If tty is available, apply fix from https://github.com/typicode/husky/issues/968#issuecomment-1176848345
if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then exec >/dev/tty 2>&1; fi
# Heavy checks should only be done on staged files
yarn lint-staged
@webbertakken

Copy link
Copy Markdown
Author

Added the if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; part so that the fix is not applied when no tty is available. This makes is so that you can still make commits from your IDE for example.

In the git log of WebStorm on Windows it looks like this:

image

Whereas from a tty (like the latest PowerShell on Windows below) you'll get the proper animated version:

pre-commit-hook

@webbertakken

Copy link
Copy Markdown
Author

This combines the fix (for no tty) from https://stackoverflow.com/a/69088164/3593896 with the fix (for broken cli animations) from typicode/husky#968 (comment)

@Ethorsen

Ethorsen commented May 9, 2023

Copy link
Copy Markdown

Amazing!

@webbertakken

Copy link
Copy Markdown
Author

Added single quotes to "bash -c 'tsc --skipLibCheck --noEmit'" because otherwise tsc flags won't be fired.

@shayl29

shayl29 commented May 29, 2023

Copy link
Copy Markdown

Well first of all, thank you so much! 🙏🙏🙏
You finally got me moving in some direction after hours of search.

For some reason, Webstorm was running the lint staged like it was a tty on my machine. It mad the colors and listr stuff to keep messing my logs.
I resolved it by tweaking this a little bit further, forcing lint-staged to use verbose renderer, by setting NODE_ENV="test" or TERM="dumb"
So for me I added an else statement

if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then exec >/dev/tty 2>&1; else export NODE_ENV="test"; fi

I found it in here

Note: I'm not sure if using those flags changes other parts in lint-staged behavior, but at least I know it forces IDE and other tools to log plain text. I wish lint-staged would enable that as a feature without this workaround.

@shayl29

shayl29 commented May 29, 2023

Copy link
Copy Markdown

After testing this further, the above solution works, but Github Desktop also messes things around.
Here is another check needs that could be added (for mega ultra 99% working solution)

# If tty is available, apply fix from https://github.com/typicode/husky/issues/968#issuecomment-1176848345
if sh -c ": >/dev/tty" >/dev/null 2>/dev/null; then exec >/dev/tty 2>&1; else export TERM="dumb"; fi

# bug on Windows/Github Desktop: add `.cmd` extension fixes it. more info: https://github.com/desktop/desktop/issues/12562#issuecomment-1153237769
case `uname` in
  *CYGWIN*|*MINGW*|*MSYS*)
    npx.cmd lint-staged
  ;;
  *)
    npx lint-staged
  ;;
esac

@webbertakken

Copy link
Copy Markdown
Author

Thanks for reporting back your findings @shayl29. I'll have a go with it in the coming weeks and see if it's worth updating the original above. The fix looks useful.

Do you know why Webstorm was running the pre-commit hook using a tty?

My Terminal settings are I think default apart from using the more modern Powershell, which I think should be used by everyone instead of using git bash or Mingw, so that we can move forward as a community.

Screenshot 2023-05-29 160409

@mreduar

mreduar commented Jan 21, 2024

Copy link
Copy Markdown

Does not work on WSL with Laravel Sail

error.mp4

@webbertakken

Copy link
Copy Markdown
Author

@mreduar did you end up fixing this issue? What worked for you? Anything I can add here?

@mreduar

mreduar commented Apr 18, 2025

Copy link
Copy Markdown

Unfortunately, I still haven’t found any solution that fixes this issue, even after all these time. For a long time now, I’ve just been running the commands without any colors or formatting as a workaround. If you or anyone else finds a proper fix, I’d really appreciate any updates here. Thanks!

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