Skip to content

feat(app-tools): add --env-dir support across dev/build/serve#8482

Open
GiveMe-A-Name wants to merge 10 commits into
mainfrom
feat/env-dir-cli-support
Open

feat(app-tools): add --env-dir support across dev/build/serve#8482
GiveMe-A-Name wants to merge 10 commits into
mainfrom
feat/env-dir-cli-support

Conversation

@GiveMe-A-Name
Copy link
Copy Markdown
Member

@GiveMe-A-Name GiveMe-A-Name commented Mar 25, 2026

#7634


PR Summary

This PR improves `--env-dir` handling across the app command lifecycle and makes the behavior more consistent.

What’s included

  • Added first-class `--env-dir` support to the `deploy` command.
  • Ensured `deploy` passes its options through to the build step, so `--env-dir` is respected in deploy workflows.
  • Refactored CLI env-dir flow to use explicit option propagation instead of relying on process-level env variable plumbing.
  • Kept default fallback behavior unchanged when `--env-dir` is not provided (the framework uses its normal working-directory-based resolution).
  • Strengthened integration coverage for deploy + env-dir behavior to avoid false positives.
  • Updated command documentation (EN/ZH) so `deploy --env-dir` is clearly documented.

Validation

  • Unit tests for env loading pass.
  • Integration tests for env-dir scenarios (dev/build/serve/deploy) pass.
  • Lint/format checks pass for touched files.

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-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 25, 2026

🦋 Changeset detected

Latest commit: 3b8737c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 116 packages
Name Type
@modern-js/app-tools Patch
@modern-js/plugin Patch
@modern-js/utils Patch
@modern-js/server-core Patch
@modern-js/adapter-rstest Patch
@modern-js/plugin-bff Patch
@modern-js/plugin-ssg Patch
@modern-js/plugin-styled-components Patch
@modern-js/plugin-i18n Patch
@modern-js/image Patch
@modern-js/runtime Patch
@modern-js/plugin-polyfill Patch
@integration-test/alias-set Patch
app-document Patch
async-entry-test Patch
tmp Patch
bff-api-app Patch
bff-client-app Patch
bff-indep-client-app Patch
bff-hono Patch
integration-clean-dist-path Patch
integration-compatibility Patch
integration-custom-dist-path Patch
custom-file-system-entry Patch
integration-custom-template Patch
deploy Patch
deploy-server Patch
dev-server Patch
integration-disable-html Patch
entries-app-builder Patch
app-custom-entries Patch
app-custom-routes-runtime Patch
app-custom Patch
app-entry Patch
app-route Patch
app-entry-server Patch
tmp-env-dir Patch
i18n-app-csr-html-lang Patch
i18n-app Patch
i18n-app-ssr-html-lang Patch
i18n-app-ssr Patch
i18n-custom-i18n-wrapper Patch
i18n-mf-app-provider Patch
i18n-mf-component-provider Patch
i18n-mf-consumer Patch
i18n-routes Patch
i18n-routes-ssr Patch
@integration-test/image-component Patch
main-entry-name Patch
nonce Patch
pure-esm-project Patch
routes-match Patch
routes Patch
app-rsbuild-hooks Patch
rsc-csr-app Patch
rsc-csr-routes Patch
rsc-ssr-app Patch
rsc-ssr-routes Patch
basic-app-rstest-browser Patch
basic-app-rstest Patch
runtime-custom-plugin Patch
runtime-custom-config-plugin Patch
select-mul-entry-test Patch
select-one-entry-test Patch
server-config Patch
server-json-script Patch
server-monitors Patch
server-prod Patch
server-routes Patch
@source-code-build/app Patch
ssg-fixtures-mega-list-routes Patch
ssg-fixtures-nested-routes Patch
ssg-fixtures-simple Patch
ssg-fixtures-web-server Patch
ssr-base-async-entry-test Patch
ssr-base-json-test Patch
ssr-base-test Patch
ssr-base-fallback-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
rsc-closing-tags-test Patch
ssr-script-loading Patch
ssr-useid-test Patch
ssr-streaming-inline-test Patch
ssr-streaming-test Patch
integration-storybook Patch
styled-components-stream Patch
styled-components-string Patch
integration-tailwindcss-v2 Patch
integration-tailwindcss-v3 Patch
integration-tailwindcss-v4 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Patch
@modern-js/builder Patch
@modern-js/plugin-data-loader Patch
@modern-js/render Patch
@modern-js/bff-core Patch
@modern-js/create-request Patch
@modern-js/prod-server Patch
@modern-js/server Patch
@modern-js/server-utils Patch
@modern-js/i18n-utils Patch
@modern-js/runtime-utils Patch
@scripts/release-node Patch
@modern-js/server-runtime Patch
@modern-js/create Patch
@modern-js/main-doc Patch
@modern-js/tsconfig Patch
@modern-js/bff-runtime Patch
@modern-js/sandpack-react Patch
@modern-js/types Patch
@modern-js/rslib Patch
@scripts/prebundle Patch
@scripts/rstest-config Patch

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

@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 25, 2026

Deploy Preview for modernjs-byted ready!

Name Link
🔨 Latest commit 79ed288
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-byted/deploys/69ca40bbf1753700088e122f
😎 Deploy Preview https://deploy-preview-8482--modernjs-byted.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 5 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@GiveMe-A-Name
Copy link
Copy Markdown
Member Author

CI failure diagnosis and fix:

  • Root cause: ut-mac failed at pnpm install with frozen-lockfile because lockfile was out of sync after adding tests/integration/env-dir/package.json.
  • Fix: pushed commit 71da6fa to update pnpm-lock.yaml.
  • Status: checks were retriggered and are running on the latest commit.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 25, 2026

Rsdoctor Bundle Diff Analysis

📊 Quick Summary
Project Total Size Change
bundle-diff 2.9 MB 0

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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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-dir and 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.

Comment thread packages/toolkit/utils/src/cli/ensure.ts
Comment thread packages/solutions/app-tools/src/commands/serve.ts
Comment thread packages/toolkit/plugin/src/cli/run/run.ts Outdated
@zllkjc
Copy link
Copy Markdown
Member

zllkjc commented Mar 26, 2026

我们同样需要支持 deploy 命令,以及如何在服务端(例如 netlify)启动 server 的时候使用这些参数,就和 .env 文件那样

@GiveMe-A-Name
Copy link
Copy Markdown
Member Author

@zllkjc 这块已补齐:

  1. deploy 命令已支持 --env-dir,并在 deploy -> build 流程透传;
  2. 服务端启动场景补充了 envDir 缺省时对 MODERN_ENV_DIR 的回退读取;
  3. 已补对应测试与文档。

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.

3 participants