feat(app-tools): add --env-dir support across dev/build/serve#8482
feat(app-tools): add --env-dir support across dev/build/serve#8482GiveMe-A-Name wants to merge 10 commits into
Conversation
Load env files from a custom CLI directory while preserving default root behavior. Keep env handling consistent in dev, build, and serve, with integration tests added.
🦋 Changeset detectedLatest commit: 3b8737c The changes in this PR will be included in the next version bump. This PR includes changesets to release 116 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for modernjs-byted ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
CI failure diagnosis and fix:
|
Rsdoctor Bundle Diff Analysis📊 Quick Summary
Generated by Rsdoctor GitHub Action |
Extract shared path containment helpers and reuse them across CLI, build, and server env loading. Keep external MODERN_ENV_DIR when no CLI override is passed and tighten integration assertions.
Remove local safe env-dir helpers in build and server load paths. Use shared resolver utility directly to keep logic centralized.
Document --env-dir for modern dev, build, and serve. Update both English and Chinese app command documentation.
There was a problem hiding this comment.
Pull request overview
This PR adds first-class --env-dir <dir> support to Modern.js app-tools commands so .env* files can be loaded from a custom directory early in CLI startup, and the same directory can be used consistently for build-time copying and production server runtime loading.
Changes:
- Add early argv parsing for
--env-dirand use it to load env files before command execution. - Add safe path helpers (
isPathInside,resolveInsideOrFallback) and apply them to env dir resolution/copying. - Add integration tests + server-core unit tests and update CLI docs (EN/ZH) to document
--env-dir.
Reviewed changes
Copilot reviewed 29 out of 31 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/integration/env-dir/tsconfig.json | Adds TS config for new integration fixture app. |
| tests/integration/env-dir/tests/tsconfig.json | Adds TS config for integration test compilation. |
| tests/integration/env-dir/tests/index.test.ts | Integration coverage for dev/build/serve --env-dir behavior + precedence. |
| tests/integration/env-dir/src/modern-app-env.d.ts | Adds app-tools type reference for fixture app. |
| tests/integration/env-dir/src/App.tsx | Fixture UI prints env vars for assertions. |
| tests/integration/env-dir/src/App.css | Fixture styling (not behavior-related). |
| tests/integration/env-dir/package.json | Declares fixture app dependencies/scripts for integration run. |
| tests/integration/env-dir/modern.config.ts | Uses base test config for fixture app. |
| tests/integration/env-dir/env/.env | Adds base env file in custom env directory. |
| tests/integration/env-dir/env/.env.development | Adds dev-mode env file in custom env directory. |
| tests/integration/env-dir/env/.env.production | Adds prod-mode env file in custom env directory. |
| tests/integration/env-dir/env/.env.local | Adds local env file in custom env directory. |
| tests/integration/env-dir/.browserslistrc | Browserslist for the fixture app. |
| pnpm-lock.yaml | Registers the new integration fixture as a workspace importer. |
| packages/toolkit/utils/src/cli/ensure.ts | Adds isPathInside + resolveInsideOrFallback helpers. |
| packages/toolkit/plugin/src/server/run/types.ts | Extends server create options with envDir. |
| packages/toolkit/plugin/src/cli/run/run.ts | Early parse of --env-dir into MODERN_ENV_DIR. |
| packages/toolkit/plugin/src/cli/run/create.ts | Loads env files from resolved envDir (with fallback). |
| packages/solutions/app-tools/src/utils/types.ts | Adds envDir to dev/build/start option types. |
| packages/solutions/app-tools/src/locale/zh.ts | Adds zh description for --env-dir. |
| packages/solutions/app-tools/src/locale/en.ts | Adds en description for --env-dir. |
| packages/solutions/app-tools/src/commands/index.ts | Exposes --env-dir on dev, build, serve. |
| packages/solutions/app-tools/src/commands/build.ts | Copies .env* from envDir into dist safely. |
| packages/solutions/app-tools/src/commands/serve.ts | Passes envDir into prod server creation options. |
| packages/server/core/tests/fixtures/serverEnvDir/env/.env | Adds server-core envDir fixture base env file. |
| packages/server/core/tests/fixtures/serverEnvDir/env/.env.prod | Adds server-core envDir fixture mode-specific env file. |
| packages/server/core/tests/adapters/loadEnv.test.ts | Adds unit coverage for envDir loading + escape fallback. |
| packages/server/core/src/adapters/node/helper/loadEnv.ts | Loads server env from resolved envDir (with fallback). |
| packages/document/docs/zh/apis/app/commands.mdx | Documents --env-dir in dev/build/serve CLI docs (ZH). |
| packages/document/docs/en/apis/app/commands.mdx | Documents --env-dir in dev/build/serve CLI docs (EN). |
| .changeset/icy-ravens-draw.md | Changeset entry for patch releases across impacted packages. |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
我们同样需要支持 deploy 命令,以及如何在服务端(例如 netlify)启动 server 的时候使用这些参数,就和 .env 文件那样 |
Co-Authored-By: Aiden
Co-Authored-By: Aiden
|
@zllkjc 这块已补齐:
|
Co-Authored-By: Aiden
Co-Authored-By: Aiden

#7634
PR Summary
This PR improves `--env-dir` handling across the app command lifecycle and makes the behavior more consistent.
What’s included
Validation