881 words, 5 min read

If your project mixes Elixir, JavaScript, and Rust, keeping dependencies fresh quickly turns into a chore. You have three package managers, three lock files, and three commands to remember. Skip one, and you end up with a stale lock file that bites you months later.

My fix is simple: wrap everything in one Mix alias, so a full dependency refresh is just:

mix dependencies.update

This post explains how it works and how you can build the same thing.

The setup

A typical Phoenix project with a Rust NIF (via Rustler) has three dependency worlds:

  • Hex packages for Elixir, tracked in mix.exs and mix.lock
  • npm packages for the frontend, tracked in assets/package.json and package-lock.json
  • Cargo crates for the native code, tracked in native/*/Cargo.toml and Cargo.lock

Each has its own update command. Mix has a feature that ties them together: aliases.

The alias

In mix.exs, an alias is just a named list of other Mix tasks that run in order:

defp aliases do
  [
    "dependencies.update": [
      "local.hex --force",
      "deps.update --all",
      "deps.clean --unused",
      "native.update",
      "assets.update"
    ]
  ]
end

Aliases can contain built-in tasks, tasks from dependencies, and your own custom tasks. Here is what each step does.

1. local.hex --force

This upgrades the Hex package manager itself, so you resolve dependencies with the latest client. --force skips the confirmation prompt, which keeps the whole process non-interactive.

2. deps.update --all

This updates every Elixir dependency to the newest version allowed by the constraints in mix.exs and rewrites mix.lock.

3. deps.clean --unused

After an update, some packages may no longer be needed, for example, because a dependency dropped one of its own. This removes them from deps/ so the build directory matches the lock file.

4. native.update: Cargo

Mix doesn't know about Cargo, so this is a small custom task. It finds every crate under native/ and runs cargo update in each:

defmodule Mix.Tasks.Native.Update do
  use Mix.Task

  @shortdoc "Run cargo update for the Rustler crates in native/"

  @impl Mix.Task
  def run(_args) do
    "native"
    |> crate_dirs()
    |> Enum.each(&cargo_update/1)
  end

  def crate_dirs(root) do
    root
    |> Path.join("*/Cargo.toml")
    |> Path.wildcard()
    |> Enum.map(&Path.dirname/1)
    |> Enum.sort()
  end

  defp cargo_update(dir) do
    Mix.shell().info("Running cargo update in #{dir} ...")

    case System.cmd("cargo", ["update"], cd: dir, into: IO.stream()) do
      {_, 0} -> :ok
      {_, code} -> Mix.raise("cargo update failed in #{dir} with exit code #{code}")
    end
  end
end

A few details:

  • Discovery through a wildcard. Adding a new crate under native/ requires no change to the alias. It is picked up automatically.
  • cd: instead of changing directory. System.cmd/3 runs Cargo inside the crate directory, so the right Cargo.lock is updated.
  • into: IO.stream(). Cargo's output streams straight to your terminal, so you see progress live.
  • Mix.raise/1 on failure. A non-zero exit code aborts the whole alias. You never end up with a half-updated project that looks fine.

5. assets.update: npm

The npm step follows the same pattern, but is simpler because there is only one package.json:

defmodule Mix.Tasks.Assets.Update do
  use Mix.Task

  @shortdoc "Run npm update in assets/"

  @impl Mix.Task
  def run(_args) do
    assets_dir = Path.join(File.cwd!(), "assets")

    Mix.shell().info("Running npm update in #{assets_dir} ...")

    case System.cmd("npm", ["update"], cd: assets_dir, into: IO.stream()) do
      {_, 0} -> :ok
      {_, code} -> Mix.raise("npm update failed with exit code #{code}")
    end
  end
end

npm update respects the semver ranges in package.json. It won't jump across major versions. For those, you still need to bump the range yourself, which is what you want: major upgrades deserve a deliberate decision.

Why bother with a wrapper?

One command, nothing forgotten. The point isn't saving keystrokes. It's that you can't forget the Cargo lock file when you don't have to remember it exists.

Fail fast. Each custom task raises on a non-zero exit code, and Mix stops the alias at the first failure.

Easy to extend. Need to update GitHub Actions versions or regenerate a vendored file? Add another task to the list.

Self-documenting. The alias documents your update process. A new team member can read ten lines in mix.exs and know exactly what happens.

What I do after running it

Updating is only half the job. Afterwards, I:

  1. Review the diff of mix.lock, package-lock.json, and Cargo.lock.
  2. Run the full test suite, plus a compile with warnings treated as errors.
  3. Skim changelogs for anything with a major version bump.
  4. Commit the lock file changes separately from any code changes they require.

Because all three lock files change in one go, the resulting commit is a clean "update dependencies" snapshot that is easy to review and, if needed, revert.