Skip to content

💡 Add a flag to exit with 0 when no issues remain after knip --fix #1577

@hituzi-no-sippo

Description

@hituzi-no-sippo

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions