astro / deadnix

Scan Nix files for dead code
GNU General Public License v3.0
484 stars 15 forks source link

`--exclude` option has no effect #77

Open montchr opened 6 months ago

montchr commented 6 months ago

As titled:

$ deadnix --exclude packages/shells/cod/default.nix -- \
| grep packages --context=3

 70 │                  (final: prev: {
    │                     ╰─── Unused lambda argument: final
Warning: Unused declarations were found.
   ╭─[./packages/shells/cod/default.nix:2:3]
 2 │  stdenv,
   │     ╰─── Unused lambda pattern: stdenv
Warning: Unused declarations were found.
$ deadnix --exclude packages/shells/cod/default.nix -- packages/shells/cod/default.nix \
| grep packages --context=3

Warning: Unused declarations were found.
   ╭─[packages/shells/cod/default.nix:2:3]
 2 │  stdenv,
   │     ╰─── Unused lambda pattern: stdenv

Edit: This is v1.2.0 from nixos-unstable. I figured that'd be the most recent, but it looks like there's also v1.2.1. However, it makes no difference:

$ nix run github:astro/deadnix -- --exclude packages/shells/cod/default.nix -- packages/shells/cod/default.nix
Warning: Unused declarations were found.
   ╭─[packages/shells/cod/default.nix:2:3]
 2 │  stdenv,
   │     ╰─── Unused lambda pattern: stdenv
astro commented 6 months ago

Reason: packages/shells/cod/default.nix != ./packages/shells/cod/default.nix. You could run with explicit path rather than the default ..

Should we canonicalize more paths?

montchr commented 6 months ago

Aha, you're right -- I included the leading ./ and that worked.

You could run with explicit path rather than the default ..

True, though the example I provided was limited to provide a demonstration. Basically my goal was to use --no-lambda-pattern-names for everything under ./packages, but omit that flag everywhere else. Here's what I ended up with in my project's Justfile:

https://codeberg.org/montchr/dotfield/src/commit/f295a2f295e571588b4b64c692a4ec6503e59b39/Justfile#L80-L84

deadnix-params := '--no-underscore'

# <- Lint files
lint: (deadnix '--fail')
  statix check

# <- Write linter fixes to files
fix: (deadnix "--edit")
  statix fix

[private]
deadnix ARGS='--fail':
  fd -t f -e nix --exclude='packages/**/*.nix' --exec-batch \
    deadnix {{ARGS}} {{deadnix-params}}
  fd -t f -e nix . packages --exec-batch \
    deadnix {{ARGS}} --no-lambda-pattern-names {{deadnix-params}}

Should we canonicalize more paths?

Could you clarify what you mean by "canonicalize" in this context?

astro commented 6 months ago

Could you clarify what you mean by "canonicalize" in this context?

I mean adding more Path::canonicalize() than we already have.