Skip to content

fix(cli): explicitly reference env vars in native template for Metro inlining#988

Open
shkumbinhasani wants to merge 1 commit intoAmanVarshney01:mainfrom
shkumbinhasani:fix/expo-native-env-inline
Open

fix(cli): explicitly reference env vars in native template for Metro inlining#988
shkumbinhasani wants to merge 1 commit intoAmanVarshney01:mainfrom
shkumbinhasani:fix/expo-native-env-inline

Conversation

@shkumbinhasani
Copy link
Copy Markdown

@shkumbinhasani shkumbinhasani commented Mar 24, 2026

Summary

  • Replaces runtimeEnv: process.env with explicit per-variable references (process.env.EXPO_PUBLIC_*) in the native env template so Metro can inline them during production Hermes bytecode bundling
  • Without this, process.env resolves to an empty object in production iOS builds, causing Zod validation to throw and crash the app on startup

Details

Metro's transform for EXPO_PUBLIC_* environment variables only works on individual property accesses like process.env.EXPO_PUBLIC_SERVER_URL — it performs AST-level replacement. Passing process.env as a whole object to runtimeEnv results in an empty object in production Hermes bytecode bundles. This works in development because Metro dev server handles process.env differently.

Test plan

  • Generate a project with each native env combination (convex, non-convex, clerk, better-auth) and verify the native.ts output has explicit env var references
  • Build a production iOS release and confirm env vars are correctly inlined

Fixes #987

Summary by CodeRabbit

  • Bug Fixes
    • Updated environment variable configuration for generated project templates. Environment variables are now selectively included based on your selected backend (Convex or standard server) and authentication method (Clerk or Better Auth). This ensures only required variables are available at runtime, improving both security and project efficiency.

…inlining

Metro's transform for EXPO_PUBLIC_* env vars only works on individual
property accesses (process.env.EXPO_PUBLIC_X), not on passing process.env
as a whole object. In production Hermes bytecode bundles, process.env
resolves to an empty object, causing Zod validation to throw and crash
the app on startup.

Fixes AmanVarshney01#987
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 24, 2026

@shkumbinhasani is attempting to deploy a commit to the Better T Stack Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 24, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b805ddfa-70f6-4aa2-91d0-c8d618f938c1

📥 Commits

Reviewing files that changed from the base of the PR and between 597a74e and 769bc2b.

📒 Files selected for processing (2)
  • packages/template-generator/src/templates.generated.ts
  • packages/template-generator/templates/packages/env/src/native.ts.hbs

Walkthrough

The changes modify environment variable configuration in generated template files by replacing runtimeEnv: process.env with explicit conditional mappings for specific environment variables (EXPO_PUBLIC_CONVEX_URL, EXPO_PUBLIC_CONVEX_SITE_URL, EXPO_PUBLIC_SERVER_URL, EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY) selected based on backend and auth options.

Changes

Cohort / File(s) Summary
Environment Template Generation
packages/template-generator/src/templates.generated.ts, packages/template-generator/templates/packages/env/src/native.ts.hbs
Replaced broad process.env reference with conditional explicit environment variable mappings for Convex backends (EXPO_PUBLIC_CONVEX_URL, EXPO_PUBLIC_CONVEX_SITE_URL), standard backends (EXPO_PUBLIC_SERVER_URL), and Clerk authentication (EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY). Enables Metro to inline individual variables during production builds rather than passing entire process.env object.

Possibly related PRs

  • PR #723: Modifies template environment variable bindings to add and condition CONVEX_SITE_URL and gate backend/auth-specific public keys similarly across multiple template files.
  • PR #757: Adjusts env template logic for runtimeEnv/client mappings that selectively expose environment keys based on backend type configuration.
  • PR #982: Updates template-generator's environment runtime mappings for Convex, Better-Auth, and Clerk integrations, applying equivalent conditional environment variable selection logic.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main change: replacing implicit process.env with explicit environment variable references in the native template for Metro inlining.
Linked Issues check ✅ Passed The code changes meet all primary requirements from issue #987: replacing runtimeEnv: process.env with explicit mappings of EXPO_PUBLIC_* variables to enable Metro inlining in production iOS builds.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the native env template, focusing on fixing Metro inlining for production iOS builds as required by issue #987.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Expo native app crashes on iOS production builds due to process.env not being inlined by Metro

1 participant