Skip to content

Add debug logging for env_file and secret file resolution - #913

Merged
hramezani merged 1 commit into
mainfrom
debug-log-file-resolution
Jul 16, 2026
Merged

hramezani merged 1 commit into
mainfrom
debug-log-file-resolution

Conversation

@hramezani

Copy link
Copy Markdown
Contributor

Closes #909

Summary

Extends the existing PYDANTIC_SETTINGS_DEBUG mechanism (added in #906) to also trace file resolution, so users can see which dotenv and secret files were probed, which were loaded, and which were skipped.

Previously, a missing env_file — e.g. a relative .env path that doesn't resolve from the current working directory — was silently skipped with no indication why, which is the exact confusion reported in #909 (and echoed across #4, #266, #354, #215, #795). This reuses the existing opt-in gate and pydantic_settings logger rather than introducing a parallel logging path.

Changes

  • DotEnvSettingsSource._read_env_files now emits DEBUG lines for each candidate env_file — Loading env file: <resolved path> or Env file not found, skipping: <resolved path>. The if is_file() branch previously had no else, so misses were invisible.
  • SecretsSettingsSource.get_field_value gets the same treatment for per-field secret files (which had an explicit # we currently don't return a warning for this comment).
  • The logger, _DEBUG_ENV_VAR, and _settings_debug_enabled() helper move into the dependency-free utils module so the sources can share them without a circular import. pydantic_settings.main re-imports them, so existing references keep working.
  • Docs and tests added.

Behavior

Opt-in and off by default — output only appears when PYDANTIC_SETTINGS_DEBUG is truthy and DEBUG-level logging is enabled on the pydantic_settings logger. No change to default behavior.

Env file not found, skipping: /home/app/.env.production
Loading env file: /home/app/.env
Secret file not found, skipping: /run/secrets/api_key

The resolved absolute paths can leak usernames/layout, so this sits behind the same opt-in gate covered by the existing sensitive-output warning in the docs.

Tests

Added test_debug_env_file_resolution, test_debug_env_file_resolution_disabled_by_default, and test_debug_secret_file_resolution. All debug tests pass; ruff check/format clean.

🤖 Generated with Claude Code

Extend the existing PYDANTIC_SETTINGS_DEBUG mechanism to trace file
resolution, so users can see which dotenv and secret files were probed,
which were loaded, and which were skipped. Previously a missing env_file
(e.g. a relative .env path that doesn't resolve from the current working
directory) was silently skipped with no indication why.

The logger, debug env-var gate, and helper move to the dependency-free
utils module so the dotenv and secrets sources can share them without a
circular import.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

Copilot AI left a comment

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.

Pull request overview

Extends the existing PYDANTIC_SETTINGS_DEBUG diagnostics to include dotenv (env_file) and secrets directory file resolution, so users can see which files were probed, loaded, or skipped when debugging settings behavior.

Changes:

  • Add DEBUG logs for dotenv file load/skip decisions in DotEnvSettingsSource._read_env_files.
  • Add DEBUG logs for per-field secret file load/skip decisions in SecretsSettingsSource.get_field_value.
  • Centralize the shared debug gate and logger in pydantic_settings.utils, with corresponding docs + tests.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test_settings.py Adds regression tests asserting debug logs appear only when PYDANTIC_SETTINGS_DEBUG is enabled and logging is at DEBUG.
pydantic_settings/utils.py Moves shared pydantic_settings logger + _settings_debug_enabled() helper into a dependency-light module for reuse.
pydantic_settings/sources/providers/secrets.py Emits debug logs when secret files are found/loaded or missing/skipped.
pydantic_settings/sources/providers/dotenv.py Emits debug logs when dotenv files are found/loaded or missing/skipped.
pydantic_settings/main.py Switches to importing the shared logger/debug helper from utils to avoid duplication and keep existing references working.
docs/index.md Documents the new file-resolution trace output under the existing debug flag section.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hramezani
hramezani merged commit 1d0ea26 into main Jul 16, 2026
20 checks passed
@hramezani
hramezani deleted the debug-log-file-resolution branch July 16, 2026 06:30
@hramezani hramezani mentioned this pull request Aug 7, 2026
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.

Add debug logging for env_file resolution (revisiting the idea from #4)

2 participants