Skip to content

Fix empty .env file handling when isOptional=true#129

Merged
java-james merged 3 commits intomasterfrom
copilot/fix-125
Aug 19, 2025
Merged

Fix empty .env file handling when isOptional=true#129
java-james merged 3 commits intomasterfrom
copilot/fix-125

Conversation

Copy link
Copy Markdown

Copilot AI commented Aug 19, 2025

When a .env file exists but is empty, DotEnv().load() with isOptional=true was throwing an EmptyEnvFileError, which is inconsistent with how missing files are handled (they don't throw when isOptional=true).

Problem

The load() method only caught FileNotFoundError but not EmptyEnvFileError. When an empty .env file exists:

  1. _getEntriesFromFile() successfully loads the file but finds it empty
  2. It throws EmptyEnvFileError
  3. The load() method doesn't catch this exception, so it propagates up even when isOptional=true

This creates inconsistent behavior where:

  • Missing file + isOptional=true → ✅ Works (no exception)
  • Empty file + isOptional=true → ❌ Throws EmptyEnvFileError

Solution

Added a catch block for EmptyEnvFileError that mirrors the exact same logic as the existing FileNotFoundError handler:

} on EmptyEnvFileError {
  if (isOptional) {
    linesFromFile = [];
  } else {
    rethrow;
  }
}

Now both missing and empty files behave consistently when isOptional=true - they both set linesFromFile = [] and continue processing normally.

Testing

Added comprehensive tests in test/empty_env_test.dart covering all scenarios:

  • Empty file with isOptional=true (should not throw)
  • Empty file with isOptional=false (should throw EmptyEnvFileError)
  • Missing file with isOptional=true (should not throw - existing behavior)
  • Missing file with isOptional=false (should throw FileNotFoundError - existing behavior)

Fixes #125.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl-ssl.google.com
    • Triggering command: wget -qO- REDACTED (dns block)
    • Triggering command: curl -fsSL REDACTED (dns block)
  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)
  • https://storage.googleapis.com/flutter_infra_release/releases/stable/linux/flutter_linux_3.24.5-stable.tar.xz
    • Triggering command: wget -q REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Copy Markdown
Author

Copilot AI commented Aug 19, 2025

@java-james 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 2 commits August 19, 2025 22:49
Co-authored-by: java-james <22756998+java-james@users.noreply.github.com>
Co-authored-by: java-james <22756998+java-james@users.noreply.github.com>
Copilot AI changed the title [WIP] Empty .env file throws Fix empty .env file handling when isOptional=true Aug 19, 2025
Copilot AI requested a review from java-james August 19, 2025 22:51
Copy link
Copy Markdown
Owner

@java-james java-james left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First time using code pilot to open PRs from issues :)

LGTM

@java-james java-james marked this pull request as ready for review August 19, 2025 22:59
@java-james java-james merged commit 5997a68 into master Aug 19, 2025
@java-james java-james deleted the copilot/fix-125 branch August 19, 2025 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty .env file throws

2 participants