__struct__/1 is undefined cannot expand from tutorial code

Hi all,

Attempting to learn Phoenix from a tutorial. Stumbled upon Phoenix: An Introduction to Elixir's Web Framework and thought I would give it a go. I’m at the part where mix phx.server is ran after defining the first views, and I can’t get it to compile.

== Compilation error in file lib/my_project/foos.ex ==
** (CompileError) lib/my_project/foos.ex:17: MyProject.Foos.Foo.__struct__/1 is undefined, cannot expand struct MyProject.Foos.Foo. 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
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.12.2) expanding macro: Kernel.|>/2

I’ve double checked the code, and I’m pretty sure I’ve faithfully adapted their code to my own Phoenix project. Is the blog’s code not compatible with newer versions of Phoenix (I’m using v1.6.15)?

I’m pretty confused on troubleshooting for Phoenix. For troubleshooting this: is there a way to individually compile / check the offending file, lib/my_project/foos/foo.ex, that defines the struct (it sounds like that’s where the actual problem is)? For the cyclical module usage, are there any automated ways to check if this is happening (off-topic, is there a technical limitation to this?). Thanks.

This looks like a simple typo, but it’s hard to tell because you have obfuscated the name. If the compiler is telling you the struct doesn’t exist, and it isn’t giving you an error on the other file, then the struct as written doesn’t exist.

Depends on what you mean by “usage”. Modules calling other modules code isn’t a problem, nor is using other modules structs. If you have a cyclic compile time dependency then that’s an issue, but the compiler will also just tell you about it.

1 Like

Embarrassing, there was a typo after all. Thanks.

1 Like

No worries, happens to all of us!

1 Like