pug

pug

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

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.

First 2 of 2 Posts Switch mode

al2o3cr

al2o3cr

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)
    Foo.Bar.Baz.__struct__()
    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]
  end
pug

pug

Perfect that worked, thank you!

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement