Aliases and potential cyclical module use error

I am running into an issue about a potential cyclical module usage regarding a struct, but I am not sure what exactly is going wrong with the code. I’ve created a minimal project which reproduces the error.

Directory structure:

  • lib
    ** elixir_struct_err.ex
    *** foo
    **** bar.ex


defmodule ElixirStructErr.MixProject do
  use Mix.Project

  def project do
      app: :elixir_struct_err,
      version: "0.1.0",
      elixir: "~> 1.12",
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      escript: escript(),

  # Run "mix help" to learn about applications.
  def application do
      extra_applications: [:logger]

  defp escript do
    [main_module: ElixirStructErr]

  # Run "mix help deps" to learn about dependencies.
  defp deps do
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "", tag: "0.1.0"}


defmodule ElixirStructErr do
  alias Foo.Bar, as: Bar
  alias Foo.Bar.Baz, as: Baz

  def main(_args \\ []) do
    Bar.test(%Baz{some_arg: "a test"})


defmodule Foo.Bar do
  defmodule Foo.Bar.Baz do
    defstruct [:some_arg]

  alias Foo.Bar.Baz, as: Baz

  @spec test(baz :: Baz) :: any
  def test(baz), do: IO.puts baz.some_arg

When I run mix, I get this error:

== Compilation error in file lib/elixir_struct_err.ex ==
** (CompileError) lib/elixir_struct_err.ex:6: Foo.Bar.Baz.struct/1 is undefined, cannot expand struct Foo.Bar.Baz. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
(stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3

I’ve tried to trim this down as much as possible. I’m expecting it to compile a script whose main just prints “a test”. Does anyone know what is going on here?

Kind of random disclaimer, my only other post to date was about the same error, but in that case it was a typo on my end. This time around, given that I’ve posted full-code, I am pretty confident that there aren’t any typos in the module name.

When you write a defmodule nested inside another defmodule, the naming automatically includes the prefix:

iex(2)> Foo.Bar.Baz.__struct__()
** (UndefinedFunctionError) function Foo.Bar.Baz.__struct__/0 is undefined (module Foo.Bar.Baz is not available)
    iex:2: (file)

iex(2)> Foo.Bar.Foo.Bar.Baz.__struct__()
%Foo.Bar.Foo.Bar.Baz{some_arg: nil}

You want lib/foo/bar.ex to start out:

defmodule Foo.Bar do
  defmodule Baz do
    defstruct [:some_arg]

Perfect that worked, thank you!