#getenv, but I want to get value at runtime, not comptime.

1 messages · Page 1 of 1 (latest)

west burrow
#

I want to detect the environment at runtime so that the program acts accordingly, but I get an error with this trace. I don't want the envars at comptime. I want them at runtime.

/home/rocketeer/compile/zig/build/stage3/lib/zig/std/os.zig:2046:10: error: unable to evaluate comptime expression
    for (environ) |ptr| {
         ^~~~~~~
src/modules/window/window_system.zig:194:34: note: called from here
                if (std.os.getenv("XDG_SESSION_TYPE") == null) {
gilded folio
#

where is that line in your file?

west burrow
#
    fn setup_platform(desired_platform: PlatformEnum) PlatformError!Platform {
        const target = switch (builtin.os.tag) {
            .linux, .freebsd, .netbsd, .openbsd, .dragonfly, .solaris => {
                if (std.os.getenv("XDG_SESSION_TYPE") == null) {
                    return PlatformError.XDGSessionTypeEnvarNotSet;
                } else if (std.os.getenv("WAYLAND_DISPLAY") != null) {
                    .WAYLAND;
                } else if (std.os.getenv("DISPLAY") != null) {
                    .X11;
                } else {
                    return PlatformError.DisplayEnvarNotSet;
                }
            },
            .windows => .WIN32,
            .macos, .ios => .COCOA,
            else => {
                return PlatformError.UnsupportedPlatform;
            },
        };
gilded folio
#

where is setup_platform called

#

anything in a global scope is implicitly comptime

west burrow
#
    pub fn init() !WindowSystem {
        ...
        window_system.platform = try setup_platform(window_system.hints.init.platform_id);
        return window_system;
    }

called from

pub fn Module(comptime T: type, comptime name: []const u8) type {
    return struct {
        const Self = @This();
        const ModuleType = T;

        module: ModuleType,
        enabled: bool = true,
        ready: bool = false,

        pub fn init() Self {
            log(.DEBUG, "Initializing module\n", .{});
            const module = .{
                .module = ModuleType.init() catch |err| {
                    log(.ERROR, name, "Unable to initialize module: {s}\n", .{@errorName(err)});
                    return .{
                        .module = undefined,
                        .enabled = false,
                    };
                },
            };
            return module;
        }
...
#

from

pub fn main() void {
   var module_window = module.Module(window_system.WindowSystem, "WindowSystem").init();
   ...
#

Any clue?

gilded folio
#

im struggling to see the problem

#

what if you do const target: Platform instead of just const target? im grasping at straws a little bit here, i dont think what youre doing is allowed but i dont see how it could cause that error message

west burrow
#

It doesn't change anything 😦

#

I could do const target: i32 and it still gets to the comptime error before it realizes that target is the wrong type

gilded folio
#

is this code hosted anywhere?

west burrow
#

I can push it to my git

#

Here it is.
There are plenty of other zig errors too that I haven't gotten to yet tho.
I gotta warn you, the repo is pretty messy rn.

gilded folio
#

i feel crazy rn

#

i cant figure out whats wrong lol

west burrow
#

std bug?

gilded folio
#

oh i think i got it

#

wowowow what unfortunate error reporting

#
src/main.zig:8:9: error: variable of type 'modules.module.Module(modules.window.window_system.WindowSystem,"WindowSystem")' must be const or comptime
    var module_window = module.Module(window_system.WindowSystem, "WindowSystem").init();
        ^~~~~~~~~~~~~
src/modules/module.zig:8:17: note: struct requires comptime because of this field
        module: ModuleType,
                ^~~~~~~~~~
src/modules/window/window_system.zig:163:15: note: struct requires comptime because of this field
    platform: Platform,
              ^~~~~~~~
src/modules/window/window_system.zig:36:15: note: struct requires comptime because of this field
        init: fn (Self) void,
              ^~~~~~~~~~~~~~
src/modules/window/window_system.zig:36:15: note: use '*const fn (comptime modules.window.window_system.WindowSystem) void' for a function pointer type
        init: fn (Self) void,
              ^~~~~~~~~~~~~~
#

it goes on for like 100+ lines

#

basically raw function types are comptime only

west burrow
#

😵

gilded folio
#

after changing all of those to function pointers youll get an error about PFN_VkGetInstanceProcAddr being generic because of the anytype

west burrow
#

so this means I can't pass functions as data

gilded folio
#

i believe you could use comptime fields but those are a little weird

#

they cant be changed

west burrow
#

Ok. Thanks so much for the help.

gilded folio
#

np

#

that was very annoying to find