diff options
author | 2023-07-19 23:04:07 -0700 | |
---|---|---|
committer | 2023-07-19 23:04:07 -0700 | |
commit | 8a13e024734f1571e294a72f5a0ab2835eac4b8f (patch) | |
tree | 9184a9f0321166c8746d693f49d0bcefa8fdfcee | |
parent | ed8be46a7bf00321b4a10571e245e8fd744676d9 (diff) | |
download | bun-8a13e024734f1571e294a72f5a0ab2835eac4b8f.tar.gz bun-8a13e024734f1571e294a72f5a0ab2835eac4b8f.tar.zst bun-8a13e024734f1571e294a72f5a0ab2835eac4b8f.zip |
Fixes #3670 (#3698)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/env_loader.zig | 21 | ||||
-rw-r--r-- | test/cli/run/env.test.ts | 12 |
2 files changed, 29 insertions, 4 deletions
diff --git a/src/env_loader.zig b/src/env_loader.zig index 406002731..3e2f371a5 100644 --- a/src/env_loader.zig +++ b/src/env_loader.zig @@ -484,12 +484,12 @@ pub const Loader = struct { var file = dir.openFile(base, .{ .mode = .read_only }) catch |err| { switch (err) { - error.FileNotFound => { + error.IsDir, error.FileNotFound => { // prevent retrying @field(this, base) = logger.Source.initPathString(base, ""); return; }, - error.FileBusy, error.DeviceBusy, error.AccessDenied, error.IsDir => { + error.Unexpected, error.FileBusy, error.DeviceBusy, error.AccessDenied => { if (!this.quiet) { Output.prettyErrorln("<r><red>{s}<r> error loading {s} file", .{ @errorName(err), base }); } @@ -508,14 +508,27 @@ pub const Loader = struct { const stat = try file.stat(); const end = stat.size; - if (end == 0) { + if (end == 0 or stat.kind != .file) { @field(this, base) = logger.Source.initPathString(base, ""); return; } var buf = try this.allocator.alloc(u8, end + 1); errdefer this.allocator.free(buf); - const amount_read = try file.readAll(buf[0..end]); + const amount_read = file.readAll(buf[0..end]) catch |err| switch (err) { + error.Unexpected, error.SystemResources, error.OperationAborted, error.BrokenPipe, error.AccessDenied, error.IsDir => { + if (!this.quiet) { + Output.prettyErrorln("<r><red>{s}<r> error loading {s} file", .{ @errorName(err), base }); + } + + // prevent retrying + @field(this, base) = logger.Source.initPathString(base, ""); + return; + }, + else => { + return err; + }, + }; // The null byte here is mostly for debugging purposes. buf[end] = 0; diff --git a/test/cli/run/env.test.ts b/test/cli/run/env.test.ts index e6ee99dd2..8ae824607 100644 --- a/test/cli/run/env.test.ts +++ b/test/cli/run/env.test.ts @@ -318,3 +318,15 @@ test(".env with zero length strings", () => { const { stdout } = bunRun(`${dir}/index.ts`); expect(stdout).toBe("||0|0"); }); + +test(".env in a folder doesn't throw an error", () => { + const dir = tempDirWithFiles("dotenv-issue-3670", { + ".env": { + ".env.local": "FOO=''\n", + }, + "index.ts": "console.write('hey')", + "package.json": '{ "name": ' + '"test"' + " }", + }); + const { stdout } = bunRun(`${dir}/index.ts`); + expect(stdout).toBe("hey"); +}); |