diff options
author | 2023-09-19 03:14:29 +0200 | |
---|---|---|
committer | 2023-09-18 18:14:29 -0700 | |
commit | 064721668740397763a2ab6b6293d5d34a7b14a8 (patch) | |
tree | 27a3f3277c70328382d772536864762ccb5e0f42 | |
parent | 6df837cff18882ae323d49e1a9def7fc6ab5a5b0 (diff) | |
download | bun-064721668740397763a2ab6b6293d5d34a7b14a8.tar.gz bun-064721668740397763a2ab6b6293d5d34a7b14a8.tar.zst bun-064721668740397763a2ab6b6293d5d34a7b14a8.zip |
fix: provide empty string to 0 length process environment variables (#5679)
* fix: provide empty string to len 0 process env vars
For process loaded env vars, its a bug to give them the literal value '""'
if the provided length is 0.
* fix: add test and remove unneeded branch
Removes the redundant branch for empty env vars and adds a test for the
process specific case.
* fix: remove empty_string_value
Removes the constant in favor of using the empty values or passing the
literal "".
* style: format env.test.ts
-rw-r--r-- | src/env_loader.zig | 12 | ||||
-rw-r--r-- | test/cli/run/env.test.ts | 10 |
2 files changed, 13 insertions, 9 deletions
diff --git a/src/env_loader.zig b/src/env_loader.zig index b8fa90d45..6957a1a26 100644 --- a/src/env_loader.zig +++ b/src/env_loader.zig @@ -35,8 +35,6 @@ pub const Loader = struct { did_load_process: bool = false, - const empty_string_value: string = "\"\""; - pub fn has(this: *const Loader, input: []const u8) bool { const value = this.map.get(input) orelse return false; if (value.len == 0) return false; @@ -304,7 +302,7 @@ pub const Loader = struct { } } else { while (iter.next()) |entry| { - const value: string = if (entry.value_ptr.value.len == 0) empty_string_value else entry.value_ptr.value; + const value: string = entry.value_ptr.value; const key = std.fmt.allocPrint(key_allocator, "process.env.{s}", .{entry.key_ptr.*}) catch unreachable; e_strings[0] = js_ast.E.String{ @@ -358,15 +356,11 @@ pub const Loader = struct { var key = env[0..i]; var value = env[i + 1 ..]; if (key.len > 0) { - if (value.len > 0) { - this.map.put(key, value) catch unreachable; - } else { - this.map.put(key, empty_string_value) catch unreachable; - } + this.map.put(key, value) catch unreachable; } } else { if (env.len > 0) { - this.map.put(env, empty_string_value) catch unreachable; + this.map.put(env, "") catch unreachable; } } } diff --git a/test/cli/run/env.test.ts b/test/cli/run/env.test.ts index 3ed300477..1c2d51e8e 100644 --- a/test/cli/run/env.test.ts +++ b/test/cli/run/env.test.ts @@ -385,6 +385,16 @@ test(".env with zero length strings", () => { expect(stdout).toBe("||0|0"); }); +test("process with zero length environment variable", () => { + const dir = tempDirWithFiles("process-issue-zerolength", { + "index.ts": "console.log(`'${process.env.TEST_ENV_VAR}'`);", + }); + const { stdout } = bunRun(`${dir}/index.ts`, { + TEST_ENV_VAR: "", + }); + expect(stdout).toBe("''"); +}); + test(".env in a folder doesn't throw an error", () => { const dir = tempDirWithFiles("dotenv-issue-3670", { ".env": { |