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
mix.exs:
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(),
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
defp escript do
[main_module: ElixirStructErr]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
elixir_struct_err.ex:
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"})
end
end
lib/foo/bar.ex:
defmodule Foo.Bar do
defmodule Foo.Bar.Baz do
defstruct [:some_arg]
end
alias Foo.Bar.Baz, as: Baz
@spec test(baz :: Baz) :: any
def test(baz), do: IO.puts baz.some_arg
end
When I run mix escript.build
, 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.