From 018a185cad22eeb8b57558da1510eb0aedb1ec4d Mon Sep 17 00:00:00 2001 From: Birdee <85372418+BirdeeHub@users.noreply.github.com> Date: Thu, 14 May 2026 05:08:44 -0700 Subject: [PATCH] fix(windows): ... ?? seriously windows what?? --- .github/workflows/nix-build.yml | 39 ++++++++++++++++++++++++++++++++- osenv.c | 32 ++++++++++++++++++++++++--- test.lua | 18 +++++++-------- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/.github/workflows/nix-build.yml b/.github/workflows/nix-build.yml index 19af516..02fb5e2 100644 --- a/.github/workflows/nix-build.yml +++ b/.github/workflows/nix-build.yml @@ -10,13 +10,50 @@ jobs: test: strategy: matrix: - os: [ubuntu-latest, macos-latest] + os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v6 with: fetch-depth: 0 + - name: Install Nix + if: runner.os != 'Windows' uses: DeterminateSystems/nix-installer-action@main + - name: Run checks + if: runner.os != 'Windows' run: nix flake check -Lvv --log-format bar-with-logs + + - name: Set up windows msvc compiler + uses: step-security/msvc-dev-cmd@v1 + - name: Setup Lua (only 1 version because windows) + if: runner.os == 'Windows' + uses: leafo/gh-actions-lua@v13 + with: + luaVersion: "5.4" + + - name: Install LuaRocks + if: runner.os == 'Windows' + shell: powershell + run: | + Invoke-WebRequest ` + -Uri "https://luarocks.github.io/luarocks/releases/luarocks-3.13.0-windows-64.zip" ` + -OutFile "luarocks.zip" + + $dir = Join-Path $env:RUNNER_TEMP "luarocks" + New-Item -ItemType Directory -Force -Path "$dir" + Expand-Archive luarocks.zip -DestinationPath "$dir" + $root = Join-Path $dir "luarocks-3.13.0-windows-64" + $link = "./.lua/bin" + New-Item -ItemType SymbolicLink -Target "$root/luarocks.exe" -Path "$link/luarocks.exe" + + - name: Build via luarocks + if: runner.os == 'Windows' + run: | + tree /f . + luarocks make --verbose + tree /f . + - name: Test via luarocks + if: runner.os == 'Windows' + run: luarocks test --verbose diff --git a/osenv.c b/osenv.c index 4affcf2..2b9f486 100644 --- a/osenv.c +++ b/osenv.c @@ -6,7 +6,7 @@ #include #endif -static void clear_process_env(void) { +static void clear_process_env(lua_State *L) { #ifdef _WIN32 LPCH env = GetEnvironmentStringsA(); if (!env) return; @@ -19,11 +19,17 @@ static void clear_process_env(void) { size_t len = (size_t)(eq - p); - char key[len + 1]; + char *key = (char *)malloc(len + 1); + if (!key) continue; memcpy(key, p, len); key[len] = '\0'; SetEnvironmentVariableA(key, NULL); + // sync CRT + if (_putenv_s(key, "") != 0) + luaL_error(L, "failed to sync CRT env (putenv)"); + + free(key); } FreeEnvironmentStringsA(env); #else @@ -57,6 +63,9 @@ static int env_unset(lua_State *L, const char *key) { #ifdef _WIN32 if (!SetEnvironmentVariableA(key, NULL)) return luaL_error(L, "failed to unset env var"); + // sync CRT + if (_putenv_s(key, "") != 0) + return luaL_error(L, "failed to sync CRT env (putenv)"); #else if (unsetenv(key) != 0) return luaL_error(L, "failed to unset env var"); @@ -73,6 +82,9 @@ static int env_set(lua_State *L, const char *key, const char *val, const int for } if (!SetEnvironmentVariableA(key, val)) return luaL_error(L, "failed to set env var"); + // sync CRT + if (_putenv_s(key, val) != 0) + return luaL_error(L, "failed to sync CRT env (putenv)"); #else if (setenv(key, val, force) != 0) return luaL_error(L, "failed to set env var"); @@ -201,11 +213,25 @@ static int env__newindex(lua_State *L) { static int env__index(lua_State *L) { lua_settop(L, 2); const char *key = luaL_checkstring(L, 2); +#ifdef _WIN32 + DWORD size = GetEnvironmentVariableA(key, NULL, 0); + if (size == 0) { + lua_pushnil(L); + return 1; + } + char *buf = (char *)malloc(size); + if (!buf) return luaL_error(L, "out of memory"); + GetEnvironmentVariableA(key, buf, size); + lua_pushstring(L, buf); + free(buf); +#else const char *val = getenv(key); if (val) lua_pushstring(L, val); else lua_pushnil(L); +#endif + return 1; } @@ -221,7 +247,7 @@ static int env__call(lua_State *L) { // env(table) luaL_checktype(L, 2, LUA_TTABLE); - if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env(); + if (nargs > 2 && lua_toboolean(L, 3)) clear_process_env(L); lua_pushnil(L); while (lua_next(L, 2) != 0) { diff --git a/test.lua b/test.lua index c4f5536..7c14276 100755 --- a/test.lua +++ b/test.lua @@ -32,15 +32,6 @@ end --- -test("osenv: environment clearing and restoration", function() - local osenv = require("osenv") - local oldenv = osenv() - ok(type(oldenv) == "table", "osenv() should return a table") - osenv({}, true) - ok(next(osenv()) == nil, "env should be empty after osenv({}, true)") - osenv(oldenv) -end) - test("osenv: get and set with various types", function() local osenv = require("osenv") local oldenv = osenv() @@ -124,6 +115,15 @@ test("set if unset tests __call", function() ok(os.getenv("TESTVARIABLE2") == nil and os.getenv("TESTVARIABLE") == nil, "both vars should be nil after restore") end) +test("osenv: environment clearing and restoration", function() + local osenv = require("osenv") + local oldenv = osenv() + ok(type(oldenv) == "table", "osenv() should return a table") + osenv({}, true) + ok(next(osenv()) == nil, "env should be empty after osenv({}, true)") + osenv(oldenv) +end) + --- test.await(function(self)