[GH-ISSUE #729] Environment variable lookup in zig library is broken #197

Closed
opened 2026-03-02 23:45:12 +03:00 by kerem · 0 comments
Owner

Originally created by @simonklee on GitHub (Feb 22, 2026).
Original GitHub issue: https://github.com/anomalyco/opentui/issues/729

OPENTUI_NO_GRAPHICS=1 works in native Zig tests, but is not seen in the Bun FFI shared-library runtime. The JS -> Bun FFI -> Zig shared-library path has footguns.

Problem:

In zig std/process.zig:

pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap {

uses std.c.environ only when builtin.link_libc == true, otherwise std.os.environ.
In Bun FFI shared-lib mode, std.os.environ is effectively empty, so env lookups return missing. Our tests dont hit this path, but JS->FFI->Zig / opencode runtime does.
Also, in std/os.zig:

/// See also `getenv`. Populated by startup code before main().
/// TODO this is a footgun because the value will be undefined when using `zig build-lib`.
/// https://github.com/ziglang/zig/issues/4524
pub var environ: [][*:0]u8 = undefined;

The situation seems to be improving with zig 0.16: https://codeberg.org/ziglang/zig/pulls/30644, but it doesn't solve the situation.

Explicitly passing env variables from host is a fairly straight forward way to solve this.

Originally created by @simonklee on GitHub (Feb 22, 2026). Original GitHub issue: https://github.com/anomalyco/opentui/issues/729 OPENTUI_NO_GRAPHICS=1 works in native Zig tests, but is not seen in the Bun FFI shared-library runtime. The JS -> Bun FFI -> Zig shared-library path has footguns. **Problem:** In zig std/process.zig: ``` pub fn getEnvMap(allocator: Allocator) GetEnvMapError!EnvMap { ``` uses std.c.environ only when builtin.link_libc == true, otherwise std.os.environ. In Bun FFI shared-lib mode, std.os.environ is effectively empty, so env lookups return missing. Our tests dont hit this path, but JS->FFI->Zig / opencode runtime does. Also, in std/os.zig: ``` /// See also `getenv`. Populated by startup code before main(). /// TODO this is a footgun because the value will be undefined when using `zig build-lib`. /// https://github.com/ziglang/zig/issues/4524 pub var environ: [][*:0]u8 = undefined; ``` The situation seems to be improving with zig 0.16: https://codeberg.org/ziglang/zig/pulls/30644, but it doesn't solve the situation. Explicitly passing env variables from host is a fairly straight forward way to solve this.
kerem closed this issue 2026-03-02 23:45:12 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/opentui#197
No description provided.