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

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
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
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
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
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews