A re-implementation of weeder using HIE files
Go to file
2020-03-15 14:52:41 +00:00
exe-weeder Looking better 2020-03-14 17:15:11 +00:00
nix/haskell Shell changes for 19.09 2019-10-28 07:46:43 +00:00
src Change UX to just use regular expressions 2020-03-15 14:52:41 +00:00
tests Random changes 2019-10-28 07:49:24 +00:00
.envrc Add .envrc 2019-09-12 09:52:12 +01:00
.gitignore Add .gitignore 2019-09-12 09:52:59 +01:00
atomic-write.nix Looking better 2020-03-14 17:15:11 +00:00
cborg-json.nix Looking better 2020-03-14 17:15:11 +00:00
cborg.nix Looking better 2020-03-14 17:15:11 +00:00
dhall.nix Looking better 2020-03-14 17:15:11 +00:00
hie.yaml Add hie.yaml 2019-09-12 09:53:07 +01:00
prettyprinter.nix Looking better 2020-03-14 17:15:11 +00:00
README.md Add a README 2020-03-08 15:41:43 +00:00
shell.nix Looking better 2020-03-14 17:15:11 +00:00
weeder.cabal Change UX to just use regular expressions 2020-03-15 14:52:41 +00:00
weeder.dhall Looking better 2020-03-14 17:15:11 +00:00

Weeder

Weeder is an application to perform whole-program dead-code analysis. Dead code is code that is written, but never reachable from any other code. Over the lifetime of a project, this happens as code is added and removed, and leftover code is never cleaned up. While GHC has warnings to detect dead code is a single module, these warnings don't extend across module boundaries - this is where Weeder comes in.

Weeder uses HIE files produced by GHC - these files can be thought of as source code that has been enhanced by GHC, adding full symbol resolution and type information. Weeder builds a dependency graph from these files to understand how code interacts. Once all analysis is done, Weeder performs a traversal of this graph from a set of roots (e.g., your main function), and determines which code is reachable and which code is dead.

Using Weeder

Preparing Your Code for Weeder

To use Weeder, you will need to generate .hie files from your source code. If you use Cabal, this is easily done by adding one line to your cabal.project.local file:

package *
  ghc-options: -fwrite-ide-info

Once this has been added, perform a full rebuild of your project:

cabal clean
cabal build all

Calling Weeder

When you call Weeder, you need to supply the directory containing .hie files - . is ussually sufficient, and at least one root - the starting point for alive-code analysis. If you're building an executable, the main function is a good starting point:

weeder . --root 'main Main main'

You can also supply additional roots by supply --root multiple times. The syntax of the argument given to --root is either:

  • unit-id ModuleName symbolName, where symbolName is the name of a variable (not a type).
  • unit-id ModuleName. This form will add all exported symbols from the given module. This can be useful if you are writing a library.

Limitations

Weeder currently has a few limitations:

Type Class Instances

Weeder is not currently able to analyse whether a type class instance is used. For this reason, Weeder adds all symbols referenced to from a type class instance to the root set, keeping this code alive. In short, this means Weeder might not detect dead code if it's used from a type class instance which is never actually needed.

Template Haskell

Weeder is currently unable to parse the result of a Template Haskell splice. If some Template Haskell code refers to other source code, this dependency won't be tracked by Weeder, and thus Weeder might end up with false positives.