Importing zig from parent directory not working

This is a problem I had yesterday, and I figured it out with the help of the discord. I’m reposting here so search engines can pick this up and folks in the future that have the same problem can hopefully find the answer when they search for it, because I could not find the answer.

I just starting using Zig yesterday, and this is the problem I was having (see first screenshot). I couldn’t find any information on the web if this is valid or not, but seems like it should be. The import statement cannot traverse up one level to find the zig file referenced. The documentation says this is relatively pathed to the source file with the import statement, leading me to believe this should be a valid configuration. My Import statement looked like this:
const buf = @import(“…/buf-iter.zig”);
where …/buf-iter.zig is in the parent directory.

Freakman on discord said
try adding --main-mod-path ".." to your build-exe command
and relative paths like that are usually accepted, but the actual error is that it’s importing from outside the module path

That works. thanks! Also figured out how to do in the build.zig…add main_pkg-path = .{ .path = ".." } to the addExecutable parameter…like this

All of this was necessary because I’m doing advent of code challenges and want all my day projects to use the same set of utility methods without having to copy each zig file into each project’s folder structure.

1 Like

It can use the package management system to refer parent directories.