Have you ever needed to debug a dependency, perhaps to track down a bug or maybe just to see how it works? In scripted languages like Ruby or JS, you can usually add debug statements, like puts "test" or console.log("test"), to the dependency’s code and see the output the next time you run your app.

This does not quite work in an Elixir / Mix project as dependencies are compiled and usually compiled just once, when they are fetched. Fortunately for us, mix (Elixir’s build tool) gives us an option called path, to automatically recompile a dependency whenever it changes. Simply declare the path option with your dependency in your project’s mix.exs like so:

defp deps do
  [
    ...
    {:my_dep, '~> 1.0.0', path: 'deps/my_dep'}
    ...
  ]
end

This tells mix to look at the deps/my_dep folder for that dependency when compiling, and recompile it (and related project files) anytime a change is detected. Now you can open up the code at deps/my_dep, add your debug statements, and explore away!

Absolute & Relative Paths

Note that the path option isn’t limited to looking at the deps folder, and also accepts relative and absolute system paths, like so:

defp deps do
  [
    ...
    {:my_dep, '~> 1.0.0', path: '../path/to/my/local/my_dep'}
    ...
  ]
end

This is particularly useful if you are developing a new Hex package along side your project, or if you want to clone a package locally to track your changes and submit patches to the original author later.

Reset the Dependency

Once you’re finished debugging and want to restore the dependency to it’s normal state, simply clean it out and re-fetch it by running:

mix do deps.clean my_dep, deps.get, compile

And everything will be reset back to how it was in the beginning.