"attempt to use null value" when doing zig build run inside emacs compilation mode or shell command

Hi, I use emacs when coding and frequently use the built in compilation mode for compiling and running my code. I recently ran into an issue when trying to build using this compilation mode on 0.17.0. When running zig build run inside a normal terminal things work as expected, but when running zig build run inside emacs i get the error:

thread 7098 panic: attempt to use null value

Doesn’t seem to matter if i use compilation mode or the shell command keybind, the same issue appears, and when running the same command in konsole the project builds as expected.

I haven’t been on linux for a while, but I don’t remember this being an issue in 0.16.0


This sounds like you used an optional without testing for null. Like a=b.?; where b is the optional.
Better to check for it explicitly:

if (b) |val| {
    a = val;
} else ...

Nah the panic is in the build system, otherwise there would be something like

step foo returned error code 1: [insert stderr from foo]

And if the issue was in the configure phase (build.zig) then there should be a stacktrace as it is build in debug mode.

So the issue is probably a regression in the maker split. I did a quick look but I can’t find anything obvious.

@Psimonk run the command with ZIG_DEBUG_CMD=1 to build the maker in debug mode to get more info.

My build.zig:

const std = @import("std");

const Translator = @import("translate_c").Translator;

pub fn build(b: *std.Build) void {
    const includePath = b.option([]const u8, "include", "Specify include path.");
    const libPath = b.option([]const u8, "lib", "Specify lib path.");

    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const glMod = b.createModule(.{
        .root_source_file = b.path("src/gl.zig"),
        .target = target,
        .optimize = optimize,
    });

    // Linking OpenGL differs between Windows and Linux.
    switch (target.result.os.tag) {
        .linux => {
            glMod.linkSystemLibrary("GL", .{});
        },
        .windows => {
            glMod.linkSystemLibrary("opengl32", .{});
        },
        else => {},
    }

    const translate_c = b.dependency("translate_c", .{
        .target = target,
        .optimize = optimize,
    });

    // SDL2
    const sdlTranslate = Translator.init(translate_c, .{
        .c_source_file = b.path("src/c/sdl.h"),
        .target = target,
        .optimize = optimize,
        .link_system_libs = &.{
            .{ .name = "SDL2" },
        },
    });

    if (includePath) |path| {
        sdlTranslate.addIncludePath(.{ .cwd_relative = path });
    }

    if (libPath) |path| {
        sdlTranslate.mod.addLibraryPath(.{ .cwd_relative = path });
    }

    // stb_image.h
    const stbiTranslate = Translator.init(translate_c, .{
        .c_source_file = b.path("src/c/stb_image.h"),
        .target = target,
        .optimize = optimize,
    });

    stbiTranslate.mod.addCSourceFile(.{
        .file = b.path("src/c/stbi.c"),
        .flags = &.{"-std=c99"},
    });

    // Main exe
    const exe = b.addExecutable(.{
        .name = "ziggin",
        .root_module = b.createModule(
            .{
                .root_source_file = b.path("src/main.zig"),
                .target = target,
                .optimize = optimize,
                .imports = &.{
                    .{
                        .name = "gl",
                        .module = glMod,
                    },
                    .{
                        .name = "sdl",
                        .module = sdlTranslate.mod,
                    },
                    .{
                        .name = "stbi",
                        .module = stbiTranslate.mod,
                    },
                },
            },
        ),
    });

    // Use -mwindows when building in release mode.
    if (target.result.os.tag == .windows and optimize != .debug) {
        exe.subsystem = .windows;
    }

    b.installArtifact(exe);

    const run_step = b.step("run", "Run the app");

    const run_cmd = b.addRunArtifact(exe);
    run_step.dependOn(&run_cmd.step);
    run_cmd.step.dependOn(b.getInstallStep());
}

Do I really need to handle for null here?

if (includePath) |path| {
    sdlTranslate.addIncludePath(.{ .cwd_relative = path });
}

if (libPath) |path| {
    sdlTranslate.mod.addLibraryPath(.{ .cwd_relative = path });
}

It doesn’t resolve to false in the if statement when they are unset?

This is what happens when doing it that way:


No this seems fine, though it is weird but I don’t think its relevant to the issue.

This is quite weird; unfortunately it is quite late for me so I can’t dig into it at the moment.

Maybe the zig cache is corrupted?

Can confirm. I’ve been getting this crash recently and deleting the cache fixes it for me

Just tried deleting the projects local cache as well as ~/.cache/zig, sadly still getting the same issue

should it be .zig-cache ?

That’s the project local cache, i did delete that as well.

What’s the output for zig version and zig env in emacs vs konsole? If they don’t match, that could be a lead.

From what I can tell they match, here is zig env:

[simon@simon-linux ziggin]$ zig env
.{
    .zig_exe = "/home/simon/.local/bin/zig/zig",
    .lib_dir = "/home/simon/.local/bin/zig/lib",
    .std_dir = "/home/simon/.local/bin/zig/lib/std",
    .global_cache_dir = "/home/simon/.cache/zig",
    .version = "0.17.0",
    .target = "x86_64-linux.7.2.9...7.2.9-gnu.2.44",
    .env = .{
        .ZIG_GLOBAL_CACHE_DIR = null,
        .ZIG_LOCAL_CACHE_DIR = null,
        .ZIG_LOCAL_PKG_DIR = null,
        .ZIG_LIB_DIR = null,
        .ZIG_LIBC = null,
        .ZIG_BUILD_ERROR_STYLE = null,
        .ZIG_BUILD_MULTILINE_ERRORS = null,
        .ZIG_BUILD_SUMMARY = null,
        .ZIG_VERBOSE_LINK = null,
        .ZIG_VERBOSE_CC = null,
        .ZIG_VERBOSE_CMD = null,
        .ZIG_DEBUG_CMD = null,
        .ZIG_IS_DETECTING_LIBC_PATHS = null,
        .ZIG_IS_AVOIDING_CALLING_ITSELF = null,
        .NIX_CFLAGS_COMPILE = null,
        .NIX_CFLAGS_LINK = null,
        .NIX_LDFLAGS = null,
        .C_INCLUDE_PATH = null,
        .CPLUS_INCLUDE_PATH = null,
        .LIBRARY_PATH = null,
        .CC = null,
        .PKG_CONFIG = null,
        .NO_COLOR = null,
        .CLICOLOR_FORCE = null,
        .XDG_CACHE_HOME = null,
        .LOCALAPPDATA = null,
        .HOME = "/home/simon",
        .PROGRAMDATA = null,
        .HOMEBREW_PREFIX = null,
    },
}