You have 3 ways to import a file. To me, they all mean something different:
std.Build.addModule
should only be used when you intend you code to be used as a library. You are signaling that the associated root source file is an entry point for your library. A library can have multiple of these.
std.Build.addAnonymousModule
creates a module, but does not expose it to dependents. It is effectively internal to your library. For me, this is best used to split large code cases in smaller units with designated responsabilities. I will never outright start a project with submodules, but instead split bigger projects when their size gets too big.
@import
with a relative path is the final way to import a file. This means the file is internal to the module which uses it. It is also the simplest and recommended way for your use case.
Side note: If I recall correctly, modules cannot @import
files from the parent folder of the root source file. Say you were to create an ast
module for src/ast/ast.zig
, it would not be able to access the src/lexer.zig
module as the latter is in a parent directory of the former. This is a nice way to ensure that API boundaries are respected and modules cannot interact with internal APIs of other modules.