Suggest an idea for Knip
Description
When using Knip in a Git pre-commit hook, knip --fix returns exit code 1 even when no issues remain after the fix, which prevents the commit from being created.
--no-exit-code does not fit my use case either, because it always returns 0 even when unfixed issues remain.
I would like knip --fix to exit with 0 when no issues remain after fixing, matching ESLint's behavior.
A new flag would be great for this. Changing the default behavior would also be nice, but I understand that would be a breaking change, so a flag seems more practical. That said, if it were possible to change the default, I would really appreciate it.
References
Documents I believe are related to this issue.
- --no-exit-code - CLI Arguments
|
### `--no-exit-code` |
|
|
|
Always exit with code zero (`0`), even when there are lint issues. |
|
|
|
The default exit codes: |
|
|
|
| Code | Description | |
|
| :--: | :--------------------------------------------------------------- | |
|
| `0` | Knip ran successfully, no lint issues | |
|
| `1` | Knip ran successfully, but there is at least one lint issue | |
|
| `2` | Knip did not run successfully due to bad input or internal error | |
- Using Knip in CI
|
Knip will exit the process with code `1` if there are one or more issues. |
- integration.yml
|
bunx --bun knip --tags=-knipignore --production --fix --no-exit-code --allow-remove-files --format |
|
pnpm run knip --fix --no-exit-code |
Behavior
Knip (v5.85.0)
$ cat package.json
{
"scripts": {
"knip-check": "knip",
"knip-fix": "knip --fix"
},
"devDependencies": {
"knip": "^5.85.0"
}
}
$ cat src/index.ts
import { foo } from './export';
$ cat src/export.ts
const foo = 1
const bar = 2
export { foo, bar }
$ npm run knip-check
> knip-check
> knip
Unused exports (1)
bar src/export.ts:4:15
$ npm run knip-fix
> knip-fix
> knip --fix
Unused exports (1)
bar src/export.ts:4:15 (removed)
$ echo $?
1 # expected `0`
$ npm run knip-check
> knip-check
> knip
✂️ Excellent, Knip found no issues.
$ echo $?
0
$ npm run knip-fix
> knip-fix
> knip --fix
✂️ Excellent, Knip found no issues.
$ echo $?
0
ESLint (v10.0.2)
$ cat package.json
{
"scripts": {
"eslint-check": "eslint foo.ts",
"eslint-fix": "eslint --fix foo.ts"
},
"devDependencies": {
"@eslint/js": "^10.0.1",
"eslint": "^10.0.2",
"globals": "^17.3.0",
"typescript-eslint": "^8.56.1"
}
}
$ cat eslint.config.mjs
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import { defineConfig } from "eslint/config";
export default defineConfig([
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.browser } },
tseslint.configs.recommended,
]);
$ cat foo.ts
let foo = 1
export default foo
$ npm run eslint-check
> eslint-check
> eslint foo.ts
/home/quux/bob/foo.ts
1:5 error 'foo' is never reassigned. Use 'const' instead prefer-const
✖ 1 problem (1 error, 0 warnings)
1 error and 0 warnings potentially fixable with the `--fix` option.
$ echo $?
1
$ npm run eslint-fix
> eslint-fix
> eslint --fix foo.ts
$ echo $?
0
$ npm run eslint-check
> eslint-check
> eslint foo.ts
$ echo $?
0
Suggest an idea for Knip
Description
When using Knip in a Git pre-commit hook,
knip --fixreturns exit code 1 even when no issues remain after the fix, which prevents the commit from being created.--no-exit-codedoes not fit my use case either, because it always returns 0 even when unfixed issues remain.I would like
knip --fixto exit with 0 when no issues remain after fixing, matching ESLint's behavior.A new flag would be great for this. Changing the default behavior would also be nice, but I understand that would be a breaking change, so a flag seems more practical. That said, if it were possible to change the default, I would really appreciate it.
References
Documents I believe are related to this issue.
knip/packages/docs/src/content/docs/reference/cli.md
Lines 357 to 367 in de4c7d8
knip/packages/docs/src/content/docs/guides/using-knip-in-ci.md
Line 10 in de4c7d8
knip/.github/workflows/integration.yml
Line 123 in de4c7d8
knip/.github/workflows/integration.yml
Line 165 in de4c7d8
Behavior
Knip (v5.85.0)
ESLint (v10.0.2)