Carve out the essentials of your Clojure app
Carve out the essentials of your Clojure app.
Carve will search through your code for unused vars and will remove them.
Experimental. Use with caution! Breaking changes may happen. Feedback and bugfix PRs welcome.
brew install borkdude/brew/carve
carveis available in the Arch User Repository. It can be installed using your favorite AUR helper such as yay, yaourt, apacman and pacaur. Here is an example using
yay:
yay -S carve-bin
Grab the binary for your OS at Github releases.
Add to your
deps.ednunder the
:aliaseskey:
:carve {:extra-deps {borkdude/carve {:git/url "https://github.com/borkdude/carve" :sha ""}} :main-opts ["-m" "carve.main"]}
where the latest SHA can be found with:
$ git ls-remote https://github.com/borkdude/carve.git refs/heads/master
Carve invokes clj-kondo and uses the analysis information to check which vars are unused. To remove the relevant bits of code it uses rewrite-cljc.
The usage for a typical Clojure app looks like:
carve --opts '{:paths ["src" "test"]}'
for the CLI or:
clojure -M:carve --opts '{:paths ["src" "test"]}'
on the JVM.
You can also store the config for your project in
.carve/config.edn. When invoking carve with no options, the options in
.carve/config.ednwill be used. When providing options, the CLI options will take precedence over the configuration in
.carve/config.edn.
Currently
carveonly has one command line option,
--opts, which expects an EDN map or EDN file with the following options of which only
:pathsis required:
:paths: a list of paths to analyze. Can be a mix of individual files and directories.
:ignore-vars: a list of vars to ignore. Useful for when the analyzer has it wrong or you just want to keep the var for whatever reason.
:api-namespaces: a list of namespaces of which only unused private vars will be reported.
:carve-ignore-file: a file where ignored vars can be stored,
.carve/ignoreby default.
:interactive: ask what to do with an unused var: remove from the file, add to
.carve/ignoreor continue. Set to
trueby default.
:out-dir: instead of writing back to the original file, write to this dir.
:dry-run: just print the unused var expression.
:aggressive: runs multiple times until no unused vars are left. Defaults to
false.
:report: when truthy, prints unused vars to stdout. Implies
:dry-run true. The output format may be set using
:report {:format ...}where format can be
:edn,
:textor
:ignore. The text output can be interpreted by editors like Emacs. This option can be combined with
:aggressive.
:silent: when truthy, does not write to stdout. Implies
:interactive false.
:clj-kondo/config: a map of clj-kondo config opts that are passed on to clj-kondo, which is used to analyze usages. e.g.: passing
{:skip-comments true}will ignore function usage in
(comment)forms. Note that the config in
.clj-kondo/config.ednis used as well - options passed with this key will override options set in the clj-kondo config file.
$ clojure -M:carve --opts '{:paths ["test-resources"] :dry-run true}' Carving test-resources/app.cljFound unused var: (defn unused-function [])
...
Carving test-resources/api.clj
Found unused var: (defn- private-lib-function [])
...
$ clojure -M:carve --opts '{:paths ["test-resources"]}' Carving test-resources/app.cljFound unused var: (defn unused-function [])
Type Y to remove or i to add app/unused-function to .carve/ignore n Found unused var: (defn another-unused-function [])
Type Y to remove or i to add app/another-unused-function to .carve/ignore i ...
$ cat .carve/ignore app/another-unused-function
Keep in mind that if you ran
carvewith
'{:paths ["src" "test"]}', there might still be potentially lots of unused code, which wasn't detected simply because there are tests for it.
So after a first cycle of carving you might want to do another run with simply
{:paths ["src"]}, which will help deleting the rest of the unused code. Just beware that this will break all the tests using the code you just deleted, and you'll have to fix/delete them manually.*
Carve also removes any unused refers from namespace
:requireforms. This means any unused refer, not just refers for functions determined to be unused by carve.
A good use case for Carve is the CI integration, to ensure that no one can introduce dead code into a codebase. This example shows how to add this step into CircleCI, but any other CI configuration will be similar.
First add this configuration into a
.circleci/deps.ednfile:
{:aliases {:carve {:extra-deps {borkdude/carve {:git/url "https://github.com/borkdude/carve" :sha "$LATEST_CARVE_SHA"}} :main-opts ["-m" "carve.main"]}}}
Then configure your build step like this:
find_dead_code: working_directory: ~/$your-project docker: - image: circleci/clojure:openjdk-11-tools-depssteps: - checkout - run: mkdir -p ~/.clojure && cp .circleci/deps.edn ~/.clojure/deps.edn - run: clojure -M:carve --opts '{:paths ["src" "test"] :report {:format :text}}'
If the
reportstep finds any dead code it exits with status code
1, thus failing the build step.
Running carve with in report mode (for example
clojure -M:carve --opts '{:paths ["src" "test"] :report {:format :text}}') you can make all the links clickable by switching to compilation-mode.
Using
:report {:format :ignore}returns the list of unused vars in the same format as .carve/ignore so you can create the initial ignore file or append to an existing one. For example with:
carve --opts '{:paths ["src" "test"] :report {:format :ignore}}' > .carve/ignore
If you want to run tests in Emacs and Cider you need to use the test alias, or it will fail while trying to load the
test.checklibrary. You can place this in your
.dir-locals.elfile in the root directory to always use the test alias:
((clojure-mode . ((cider-clojure-cli-global-options . "-R:test"))))
or alter the command used by
cider-jack-inby prefixing the invocation with
C-u.
Copyright © 2019-2021 Michiel Borkent
Distributed under the EPL License. See LICENSE.