Skip to content

fix: restore --env-file parent directory traversal and do not error in deno compile for not found env file#32686

Merged
dsherret merged 10 commits intodenoland:mainfrom
bartlomieju:fix/env-file-parent-traversal
Mar 13, 2026
Merged

fix: restore --env-file parent directory traversal and do not error in deno compile for not found env file#32686
dsherret merged 10 commits intodenoland:mainfrom
bartlomieju:fix/env-file-parent-traversal

Conversation

@bartlomieju
Copy link
Copy Markdown
Member

@bartlomieju bartlomieju commented Mar 13, 2026

Summary

  • Fixes a regression from refactor: replace dotenvy with deno_dotenv #32263 where replacing dotenvy with deno_dotenv lost the parent directory traversal behavior for --env-file
  • Adds find_env_file() helper that walks up parent directories when the specified env file isn't found at the given path, restoring the dotenvy::from_filename() behavior
  • Applied to all three env file loading paths: normal mode, watch mode, and deno compile

Closes #32683
Closes #30402

Test plan

  • New spec test env_file_parent_traversal verifies .env in parent dir is found when running from subdirectory
  • All existing env_file spec tests continue to pass

🤖 Generated with Claude Code

bartlomieju and others added 2 commits March 13, 2026 09:17
When dotenvy was replaced with deno_dotenv in denoland#32263, the parent
directory traversal behavior of `dotenvy::from_filename()` was lost.
This caused `--env-file` to no longer find .env files in parent
directories when running from a subdirectory.

Adds `find_env_file()` which walks up parent directories looking for
the named file, restoring the previous behavior.

Closes denoland#32683

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The .env file in the env_file_parent_traversal spec test was not
tracked by git due to the repo-level .gitignore rule, causing CI
failures across all platforms.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bartlomieju bartlomieju requested a review from dsherret March 13, 2026 11:52
Copy link
Copy Markdown
Contributor

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

I'll update this.

cli/util/env.rs Outdated
Comment on lines +19 to +20
/// looking for a file with the same name. This replicates the behavior of
/// dotenvy's `find()` function that was lost when switching to deno_dotenv.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Such an llm comment lol

cli/util/env.rs Outdated
Comment on lines +35 to +36
#[allow(clippy::disallowed_methods)]
env::current_dir().ok()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm going to clean up this PR. Too many things to discuss.

@dsherret dsherret changed the title fix: restore --env-file parent directory traversal fix: restore --env-file parent directory traversal and do not error in deno compile for not found env file Mar 13, 2026
Copy link
Copy Markdown
Contributor

@dsherret dsherret left a comment

Choose a reason for hiding this comment

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

LGTM. I hand wrote this.

@dsherret dsherret enabled auto-merge (squash) March 13, 2026 13:58
@dsherret dsherret disabled auto-merge March 13, 2026 15:13
);

// should load fine as in Node.js
process.loadEnvFile(envFilePath);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This previously wasn't working like node.

@dsherret dsherret enabled auto-merge (squash) March 13, 2026 16:29
@dsherret dsherret merged commit 8ac8de1 into denoland:main Mar 13, 2026
220 of 222 checks passed
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.

--env-file does not load .env from parent directories as documented Deno compile --env-file fails when specified file is not found

2 participants