pug

pug

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.

Showing Posts 1 to 2

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 OP

Perfect that worked, thank you!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews