Skip to content
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

Result of preprocessors not considerd for exit code #300

Closed
matthias-dev opened this issue Oct 16, 2023 · 5 comments
Closed

Result of preprocessors not considerd for exit code #300

matthias-dev opened this issue Oct 16, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@matthias-dev
Copy link

According to issue #204 the preprocessor api can be used to filter out findings of knip. As far as I understand, the result of the preprocessors are not taken into account for calculating the totalErrorCount which is important for the exit code.
Because you advice to use the preprocessor api for a use case like the one in #204 (filter out findings with complex logic), I think it's a bug that the result of the preprocessors are not taken into account for the exit code.

@matthias-dev matthias-dev added the bug Something isn't working label Oct 16, 2023
@webpro webpro closed this as completed in 004d858 Oct 17, 2023
@webpro
Copy link
Collaborator

webpro commented Oct 17, 2023

🚀 This issue has been resolved in v2.34.0. See Release 2.34.0 for release notes.

@matthias-dev
Copy link
Author

Thanks for taking time to work on this issue. But I think it's still not fixed. The CLI command still returns the wrong exit code if issues are filtered by preprocessors. Because firstly the filter method in cli.ts still references the unprocessed report
.filter(reportGroup => report[reportGroup] && rules[reportGroup] === 'error')
it should be
.filter(reportGroup => finalData.report[reportGroup] && rules[reportGroup] === 'error').

And secondly the reduce function is using the counters dictionary which I think has to be reevaluated after the preprocessors run?
.reduce((errorCount: number, reportGroup) => errorCount + counters[reportGroup], 0);.

@webpro
Copy link
Collaborator

webpro commented Oct 18, 2023

I've got a temporary solution, need some more time (plus tests!) for this.

For now, the counters are sent to the preprocessor, and you can update those so the final result should be based on the updated numbers.

@matthias-dev
Copy link
Author

Thanks a lot for that workaround!

@MRVDH
Copy link

MRVDH commented Dec 5, 2023

This is the workaround that I used. At the end of my preprocessor, I loop over all the issue categories and check if that issue category is filled. If it is empty, I set the counter of that category to 0.

Note that you also need to clear the issue categories that you exclude in your knip configuration. For example, I have excluded types and nsExports in my knip.jsonc.

  ...
  // Excluded categories need to be cleared. And if there are no issues for a category, set the counter to 0.
  options.issues.nsExports = {};
  options.issues.types = {};

  for (const key in options.issues) {
    if (options.issues.hasOwnProperty(key)) {
      const element = options.issues[key];
      if (Object.keys(element).length === 0) {
        options.counters[key] = 0;
      }
    }
  }

  return options;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants