[PR #608] fix: make dotenv loading optional and configurable #612

Open
opened 2026-02-16 01:16:32 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langgenius/dify-plugin-daemon/pull/608
Author: @weichun-wang
Created: 2/13/2026
Status: 🔄 Open

Base: mainHead: fix/dotenv-loading-592


📝 Commits (2)

  • 9370e70 fix: make dotenv loading optional and configurable
  • 0a1c921 refactor: replace dotenv magic strings with constants

📊 Changes

2 files changed (+74 additions, -2 deletions)

View changed files

📝 .env.example (+5 -0)
📝 cmd/server/main.go (+69 -2)

📄 Description

Summary

  • Make dotenv loading robust for container/self-hosted deployments by introducing controlled startup behavior.
  • Avoid crash loops when .env is absent while keeping strict failures for invalid dotenv mode/path and malformed dotenv files.
  • Preserve env precedence: existing OS/container env vars are not overridden by .env.

Changes

  • Update startup env loading in cmd/server/main.go:
    • Add loadDotEnv() with:
      • DIFY_DOTENV_MODE=optional|require|disabled (default: optional)
      • DIFY_ENV_FILE (default: .env)
    • Behavior:
      • optional: load if file exists, ignore if missing
      • require: fail when missing or invalid
      • disabled: skip dotenv loading
    • Use godotenv.Read() + os.Setenv() only for missing keys, so injected env remains highest priority.
  • Document new knobs in .env.example.

Why

0.5.3-local introduced strict dotenv loading at startup, which can panic in Docker/K8s when /app/.env is not present even though environment variables are already injected by runtime.
This change makes startup behavior explicit and production-safe while preserving strict configuration validation via existing envconfig + Validate() flow.

Compatibility

  • Backward compatible for local development (optional default).
  • Better for Docker/K8s: can set DIFY_DOTENV_MODE=disabled to rely purely on runtime env injection.
  • No changes to required config validation semantics.

Fixes #592

Local branch and commit:

  • Branch: fix/dotenv-loading-592
  • Commit: 9370e70

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langgenius/dify-plugin-daemon/pull/608 **Author:** [@weichun-wang](https://github.com/weichun-wang) **Created:** 2/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/dotenv-loading-592` --- ### 📝 Commits (2) - [`9370e70`](https://github.com/langgenius/dify-plugin-daemon/commit/9370e70f741fd7c6cbacf34aaa0bd3bddf0d089e) fix: make dotenv loading optional and configurable - [`0a1c921`](https://github.com/langgenius/dify-plugin-daemon/commit/0a1c921e59e0be202af91d2113f92d2549b3530f) refactor: replace dotenv magic strings with constants ### 📊 Changes **2 files changed** (+74 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+5 -0) 📝 `cmd/server/main.go` (+69 -2) </details> ### 📄 Description ## Summary - Make dotenv loading robust for container/self-hosted deployments by introducing controlled startup behavior. - Avoid crash loops when `.env` is absent while keeping strict failures for invalid dotenv mode/path and malformed dotenv files. - Preserve env precedence: existing OS/container env vars are not overridden by `.env`. ## Changes - Update startup env loading in `cmd/server/main.go`: - Add `loadDotEnv()` with: - `DIFY_DOTENV_MODE=optional|require|disabled` (default: `optional`) - `DIFY_ENV_FILE` (default: `.env`) - Behavior: - `optional`: load if file exists, ignore if missing - `require`: fail when missing or invalid - `disabled`: skip dotenv loading - Use `godotenv.Read()` + `os.Setenv()` only for missing keys, so injected env remains highest priority. - Document new knobs in `.env.example`. ## Why `0.5.3-local` introduced strict dotenv loading at startup, which can panic in Docker/K8s when `/app/.env` is not present even though environment variables are already injected by runtime. This change makes startup behavior explicit and production-safe while preserving strict configuration validation via existing `envconfig + Validate()` flow. ## Compatibility - Backward compatible for local development (`optional` default). - Better for Docker/K8s: can set `DIFY_DOTENV_MODE=disabled` to rely purely on runtime env injection. - No changes to required config validation semantics. Fixes #592 Local branch and commit: - Branch: fix/dotenv-loading-592 - Commit: 9370e70 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 01:16:32 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-plugin-daemon#612