#getting env vars failing

1 messages · Page 1 of 1 (latest)

kindred locust
#

I am trying to fetch an env var on my M1 machine and no matter whether I use the std.os or std.proccess methods the env var is always not found (even when it is set). Is this maybe a mac issue?

spark edge
#

what is the code that fails?

kindred locust
#

and mainnet_rpc is set an an env var. Can confirm with an echo $mainnet_rpc

chrome star
#

I guess the obvious questions are:

Are you sure you have the case correct?
Are you sure that the zig program is running in a context that has the environment variable set?

alpine mantle
#

its possible this is a result of not linking libc. could you try again with libc linked?

kindred locust
#

I am running using zig build test if that helps

alpine mantle
#

so in your build.zig, i think you'll need something like

test_exe.linkLibC();
#

did you try this? i'm not saying its a solution, just trying to help find the problem.

#

maybe try running this

// /tmp/tmp.zig
const std = @import("std");

// pub fn main() !void {
test {
    const foo = try std.process.getEnvVarOwned(std.heap.page_allocator, "FOO");
    std.debug.print("foo={s}\n", .{foo});
}
$ FOO="BAR" zig test /tmp/tmp.zig
Test [1/1] test_0... foo=BAR
All 1 tests passed.
#

and if that fails, try

$ FOO="BAR" zig test /tmp/tmp.zig -lc
kindred locust
#

Hmm this actually works even without -lc

#

I wonder if its since I am doing it from zig build test (which I need to use since my project has deps that are imported using the package manager and setup in the build file)

alpine mantle
#

is it returning an error or just not printing anything?

#

another thing to check, are the tests just getting cached? you can see if its getting cached w/ $ zig build test --summary all

#

and to prevent it in your build.zig

const run_unit_tests = b.addRunArtifact(unit_tests);
run_unit_tests.has_side_effects = true;
kindred locust
#

It returns the variable not found error. It's also not a cache issue as I made changes to trigger a rebuild, but I didn't know I could do this in the build file which is cool.