-
-
Notifications
You must be signed in to change notification settings - Fork 431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lint-staged ignores tsconfig.json when it called through husky hooks #825
Comments
Can you try explicitly setting the config file for tsc? "lint-staged": {
"*.{js,ts}": [
"tsc -p tsconfig.json --noEmit"
]
} We have a couple of issues like this, and I can't say for certain what causes it. I assume it might be a wrong working directory. |
Also, please post your debug logs by enabling them: "husky": {
"hooks": {
"pre-commit": "lint-staged --debug"
}
}, |
Do you by chance have |
It's because you get the filenames passed as an argument. Try using the function syntax. // lint-staged.config.js
module.exports = {
"*.{js,jsx}": [
"eslint --cache --fix",
],
"*.{ts,tsx}": [
() => "tsc --skipLibCheck --noEmit",
"eslint --cache --fix",
],
} |
Similar issue with When invoked from husky, it does not find the project folder or configuration :$ Here's my setup. It works as long as all changes are staged :P // package.json
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"yarn eslint --quiet --fix",
"bash -c tsc --noEmit" // notice bash!
]
},
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
"pre-commit": "lint-staged"
}
}, // tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
// notice the filters!
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
} |
same issue @sombreroEnPuntas thank you! it's working for me |
adding bash fixed it for me, thanks @sombreroEnPuntas
|
@antoinerousseau thanks! using it in a js file with a function syntax works. |
Thank you all 👍 |
Seems to have been solved. If someone wants to open a PR about adding this to the README, we'd appreciate it! |
The
|
@sombreroEnPuntas |
I've tried lots of different ways to get this working on a project (gatsby) "lint-staged": {
"*/**/*.{js,ts,tsx}": [
"yarn type-check",
"eslint"
] but when i run this i get weird react native errors when the project isn't react native
|
I managed to get around this by emulating the config with the appropriate flags for the const tscFlags = [
"--target es5",
"--allowJs",
"--skipLibCheck",
"--strict",
"--forceConsistentCasingInFileNames",
"--noEmit",
"--esModuleInterop",
"--module esnext",
"--moduleResolution node",
"--resolveJsonModule",
"--isolatedModules",
"--noImplicitAny",
"--jsx preserve",
];
module.exports = {
"**/*.ts?(x)": (filenames) =>
`tsc ${tscFlags.join(" ")} ${filenames.map((fN) => `"${fN}"`).join(" ")}`,
"**/*.(ts|js)?(x)": () => [
"prettier --ignore-unknown --write",
"npm run lint",
],
}; Not the most elegant solution😅 But works like a charm telling the TypeScript compiler to only compile files that are staged, while mapping onto the output of your actual Also! You'll notice the string escaping of the file names. 👀 That is to get around issues on Windows where paths to the files contain spaces. (In my case my username) |
I know it's little bit late for this issue, but I want to share my solution for this problem. First of all, there are only 2 ways to use
When you use But here's the thing: if you only changing one file like Some people would suggest the second case to do the type checking, like changing the setting to be: '**/*.{ts,tsx}': [
() => "tsc-files --noEmit -p tsconfig.json",
"eslint --cache --fix",
], So that it would ignore the It seems like this is a dilemma. I would suggest putting the
|
Yet another 🚲 which allows to run tsc only on staged files. package.json "scripts": {
"lint-staged": "lint-staged"
},
"devDependencies": {
"husky": "^7.0.4",
"lint-staged": "^12.3.4",
"vue-tsc": "^0.31.4"
}
.lintstagedrc.js
.husky/pre-commit #!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# 🚲 stash unstaged changes so vue-tsc won't cover 'em, then pop 'em after
# https://stackoverflow.com/a/71150883/12496886
# https://github.com/okonet/lint-staged/issues/825
unstaged_files=$(git diff --name-only | tr '\n' ' ');
if [[ -n $unstaged_files ]]; then
(git stash push $unstaged_files && NODE_ENV=production npm run lint-staged && git stash pop) || git stash pop #handle error
else
NODE_ENV=production npm run lint-staged
fi For some reason |
@souljorje your shell script can't find the |
@shikelong I suggest a project-based type-check instead of checking files individually, as they're dependent. |
const getTscFlags = () => {
const compilerOptions = {
allowJs: true,
allowSyntheticDefaultImports: true,
esModuleInterop: true,
isolatedModules: true,
jsx: 'react-native',
lib: ['es2017'],
types: ['react-native', 'jest'],
moduleResolution: 'node',
noEmit: true,
target: 'esnext',
skipLibCheck: true,
resolveJsonModule: true
}
return Object.keys(compilerOptions)
.flatMap(key => {
const value = compilerOptions[key]
if (Array.isArray(value)) {
return `${key} ${value.join(',')}`
}
if (typeof value === 'string') {
return `${key} ${value}`
}
return key
})
.map(key => `--${key}`)
.join(' ')
}
module.exports = {
'*.{js,jsx}': ['eslint --fix', 'jest -u --bail --findRelatedTests'],
'*.{ts,tsx}': [`tsc ${getTscFlags()}`, 'eslint --fix', 'jest -u --bail --findRelatedTests']
} |
"I would suggest putting the tsc -p tsconfig.json --noEmit --skipLibCheck in pre-push using husky" I think Using Husky will alse scan the whole project. |
@sombreroEnPuntas it works , nice bro!!!!!! |
这个问题解决了吗 |
- Executing via bash to load tsconfig.json in lint-staged - see: lint-staged/lint-staged#825 (comment)
Fixed the problem (ts check does not scope the type check to the staged files. ex. throwing module not found error if the module is imported in a staged file, but not added to the stage) of ts-check in precommit hook by following: lint-staged/lint-staged#825 (comment)
package.json: "lint-staged": {
"*": ["bash -c \"pnpm run typecheck\"", "eslint --fix"]
}, |
When using |
don't use TLDR; detailed explanation: tsc --project tsconfig.json --noEmit src/staged-file.ts src/other-staged-file.ts where the
to circumvent this, you need to provide your tsc --noEmit --strict --baseUrl "." --paths { "app/*": ["./src/app/*"], # ... it's not feasible to maintain such code, so the remaining options are either writing a script in your |
I use |
@zdenkolini Thank you for sharing |
in my case it fails running it in folders as npx lint-staged this is my .lintstagedrc.json file
when running pnpm run types, it reads node_modules even if they are excluded in the tsconfig at the same level as the .lintstagedrc.json file. So the types check fail. Obviously running pnpm run types in the same directory directly does work using last version: ^15.2.5", adding
which is already in my tsconfig fixes it for me, for some reason |
Best option is to use It type checks all the files even non-staged ones, but that's fine, a change in a file may break type checking in some other file.
|
@zdenkolini |
I would suggest not using // types.ts
export type User = {
name: string;
} // user.ts
import { User } from './types'
const user: User = {
name: 'John Doe'
} Now let's say you make some changes to // types.ts
export type User = {
- name: string;
+ fullName: string;
}
// package.json
"lint-staged": {
"*.{js,jsx,ts,tsx}": "bash -c 'tsc --noEmit'"
},
// lint-staged.config.js
export default {
'**/*.ts?(x)': () => 'tsc --noEmit', // notice the empty function parameters and not passing the file list to the command
}
|
lint-staged passed a list of files to tsc. tsc cannot find its configuration when this happens. For the workaround, see lint-staged/lint-staged#825.
Description
lint-staged ignores tsconfig.json when it called through husky hooks (see "Steps to reproduce")
Steps to reproduce
Create these files (test.js, tsconfig, package.json):
test.ts
tsconfig.json
package.json
Then on command line write:
// everything works fine (it uses tsconfig.json)!
tsc --noEmit
// throw an error because it ignores tsconfig.json
git commit
// error TS1056: Accessors are only available when targeting ECMAScript 5 and higherEnvironment
lint-staged
: v10.1.0The text was updated successfully, but these errors were encountered: