Skip to content

Refactor envloader for base directories and restart - #3

Merged
waltercacau merged 5 commits into
mainfrom
refactor/base-dir-and-restart
Nov 12, 2026
Merged

waltercacau merged 5 commits into
mainfrom
refactor/base-dir-and-restart

Conversation

@waltercacau

Copy link
Copy Markdown
Contributor

Summary

Refactors envloader before the first release:

  • Replaces the Bazel-specific BUILD_WORKING_DIRECTORY handling with WithBaseDir(dir). Empty/unset base directories fall back to the process working directory; absolute paths ignore the base.
  • Restarts the process by default after at least one env file is loaded, so packages whose init() functions already ran see the new environment.
  • Adds NoRestart() for debuggers, tests, and Windows.
  • Keeps the ENV_LOADER_LOADED_ENVS guard, RequireAll(), missing-file behavior, load order, and godotenv.Overload semantics.
  • Adds base-directory and restart tests, including a swappable internal exec hook.
  • Rewrites the README to be build-system agnostic and document the new options.

Validation

  • make test
  • gofmt -l . is empty
  • Windows test binary compiles (GOOS=windows go test -c)

@waltercacau
waltercacau requested a review from a team as a code owner November 12, 2026 20:57
@pr-inbox-app

pr-inbox-app Bot commented Nov 12, 2026

Copy link
Copy Markdown

🚀 Ready for Review? 🚀

Mark your PR as ready and automatically assign the right reviewers.

Mark Ready Button
Let reviewers know this PR is ready for review

@olive-agent

olive-agent Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Olive Review: ✅ Approved

Posted by: GitHub @olive-agent[bot]
Reviewed commit: 696966d15fc0583f119c29529c69bdf36cfceeb1
Base ref: origin/main
Merge base: 70bb432462c2bbaebac3649486f34a9daa0daf36

Review Summary

The pre-release refactor adds explicit WithBaseDir path resolution and makes process restart the default after loading env files so package initialization can see the loaded values. Callers may need to update Bazel path handling or pass NoRestart(). End-to-end or performance testing evidence could not be verified from the PR description. Unique current findings: 0. Selected preexisting concerns: 0.

📣 Human Attention

  • 📣 On Unix, MustLoadEnv now replaces the process after loading at least one file, preserving arguments and passing the updated environment. Callers that need execution to continue after loading must pass NoRestart().
  • 📣 Relative env-file paths no longer automatically use BUILD_WORKING_DIRECTORY. Bazel callers must pass WithBaseDir(os.Getenv("BUILD_WORKING_DIRECTORY")) to preserve invocation-directory resolution; otherwise paths use the process working directory.
  • 📣 On non-Unix platforms, MustLoadEnv now panics after loading an existing env file unless NoRestart() is passed. Loading remains a no-op when ENV_LOADER_LOADED_ENVS=true, and no restart occurs when no files are loaded.

Component Walkthrough

Legend: green = new; blue = changed; gray = existing context; blue arrows = changed handoffs.

  • MustLoadEnv entry — envloader/envloader.go:68 (existing context) — The public entry applies caller options and invokes the loader, providing the root for file loading and restart.
  • Environment loading — envloader/envloader.go:100 (changed) — The loader now resolves relative paths against the configured base directory or working directory, overloads existing files in order, and sets the inherited guard against repeated loading.
  • Unix process restart — envloader/exec_unix.go:20 (new) — The new Unix restart implementation replaces the process with the same arguments and updated environment so package initialization sees the loaded values.
  • Changed handoff: Environment loading → Unix process restart — envloader/envloader.go:135 — After loading at least one file and setting the guard, the loader now invokes restart with the executable resolved before loading, unless NoRestart is enabled.
  • Removed behavior: Automatic BUILD_WORKING_DIRECTORY selection — envloader/envloader.go:55 — The loader no longer reads BUILD_WORKING_DIRECTORY automatically; callers must explicitly supply it through WithBaseDir to retain that path-resolution behavior.
flowchart TD
N_entry["MustLoadEnv entry"]:::existing
N_load["Environment loading"]:::changed
N_reexec["Unix process restart"]:::new
N_entry --> N_load
N_load --> N_reexec
classDef new fill:#1a7f37,stroke:#04300f,color:#fff,stroke-width:2,fill-opacity:.8
classDef changed fill:#0b62c4,stroke:#032a5c,color:#fff,stroke-width:2,fill-opacity:.8
classDef existing fill:#4a5563,stroke:#10151a,color:#fff,stroke-width:2,fill-opacity:.8
linkStyle 1 stroke:#38bdf8,stroke-width:3
Loading
Loop Summary

Two prior review rounds yielded four concerns with explicit disposition replies: three claim fixes and one accepts the current behavior. The latest fix claim references reviewed commit 696966d; these human claims require independent verification.

PR Inbox Meta
{
  "changeComplexity": "complex",
  "hasHumanAttention": true
}

Comment thread envloader/exec_unix.go Resolved
Comment thread envloader/envloader.go Resolved
@waltercacau

Copy link
Copy Markdown
Contributor Author

olive review

Comment thread envloader/exec_unix.go Resolved
t.Run("EmptyFileListDoesNotNeedWorkingDirectory", func(t *testing.T) {
dir := t.TempDir()
t.Chdir(dir)
require.NoError(t, os.Remove(dir))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2]

Skip the deleted-working-directory cases on Windows. Windows prevents deletion of the process's current directory, so this assertion and the equivalent assertion in AbsolutePathDoesNotNeedWorkingDirectory fail before exercising the loader. These tests are unconditional even though Windows remains supported with NoRestart().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We are not currently running the test suite on Windows, only cross-compiling, so skipping the deleted-working-directory cases is not necessary right now. Leaving the tests unchanged.

@waltercacau

Copy link
Copy Markdown
Contributor Author

olive review

@waltercacau
waltercacau merged commit 6cbcc9c into main Nov 12, 2026
5 checks passed
@waltercacau
waltercacau deleted the refactor/base-dir-and-restart branch November 12, 2026 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants