Skip to content
manage and restrain cross-module dependencies in Elixir projects
Elixir
Branch: master
Clone or download
sasa1977 Merge pull request #13 from Geekfish/patch-1
Fix documentation link to demo system
Latest commit 8127d86 Jan 31, 2020
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
demos/my_system Finalize version Jan 27, 2020
images kick off Aug 12, 2019
lib
test
.credo.exs Use credo to enforce common style Aug 31, 2019
.formatter.exs kick off Aug 12, 2019
.gitignore
.tool-versions Use Elixir 1.10 Jan 27, 2020
.travis.yml
CHANGELOG.md Finalize version Jan 27, 2020
LICENSE kick off Aug 12, 2019
README.md Fix documentation link to demo system Jan 30, 2020
mix.exs Finalize version Jan 27, 2020
mix.lock Update deps Jan 27, 2020

README.md

Boundary

hex.pm hexdocs.pm Build Status

Boundary is a library which helps managing and restraining cross-module dependencies in Elixir projects.

Status

Highly experimental, untested, and unstable.

Documentation

For a detailed reference see docs for Boundary module and mix compiler.

Basic usage

To use this library, you first need to define the boundaries of your project. A boundary is a named group of one or more modules. Each boundary exports some (but not all!) of its modules, and can depend on other boundaries. During compilation, the boundary compiler will find and report all cross-module function calls which are not permitted according to the boundary configuration.

Example

The following code defines boundaries for a typical Phoenix based project generated with mix phx.new.

defmodule MySystem do
  use Boundary, deps: [], exports: []
  # ...
end

defmodule MySystemWeb do
  use Boundary, deps: [MySystem], exports: [Endpoint]
  # ...
end

defmodule MySystem.Application do
  use Boundary, deps: [MySystem, MySystemWeb]
  # ...
end

The configuration above defines three boundaries: MySystem, MySystemWeb, and MySystem.Application.

Boundary modules are determined automatically from the boundary name. For example, the MySystem boundary contains the MySystem module, as well as any module whose name starts with MySystem. (e.g. MySystem.User, MySystem.User.Schema, ...).

The configuration specifies the following rules:

  • Modules residing in the MySystemWeb boundary are allowed to invoke functions from modules exported by the MySystem boundary.
  • Modules residing in the MySystem.Application namespace are allowed to invoke functions from modules exported by MySystem and MySystemWeb boundaries.

All other cross-boundary calls are not permitted.

Next, you need to add the mix compiler:

defmodule MySystem.MixProject do
  use Mix.Project

  def project do
    [
      compilers: [:boundary, :phoenix, :gettext] ++ Mix.compilers(),
      # ...
    ]
  end

  # ...
end

Boundary rules are validated during compilation. For example, if we have the following code:

defmodule MySystem.User do
  def auth do
    MySystemWeb.Endpoint.url()
  end
end

The compiler will emit a warning:

$ mix compile

warning: forbidden call to MySystemWeb.Endpoint.url/0
  (calls from MySystem to MySystemWeb are not allowed)
  (call originated from MySystem.User)
  lib/my_system/user.ex:3

The complete working example is available here.

Because boundary is implemented as a mix compiler, it integrates seamlessly with editors which can work with mix compiler. For example, in VS Code with Elixir LS:

VS Code warning 1

VS Code warning 2

Troubleshooting

Boundary loads the application

Boundary loads the applications because it needs to fetch module attributes. If you're loading your application in your code, for example in test. Treat the case when the application is already loaded as a success case.

case Application.load(:my_app) do
  :ok -> :ok
  {:error, {:already_loaded, :my_app}} -> :ok
end

Known Issues

  • Boundary will report incorrect invalid calls when multiple modules are defined in the same file (which often happens with Protocol implementations)

Roadmap

  • support nested boundaries (defining internal boundaries within a boundary)
  • validate calls to external deps (e.g. preventing Ecto usage from MySystemWeb, or Plug usage from MySystem)
  • support Erlang modules

License

MIT

You can’t perform that action at this time.