Skip to content

Optimize env.ts and eliminate dependency cycles in webapp#151

Draft
seansica wants to merge 27 commits intohijohnnylin:mainfrom
mitre:2-environment-variable-handling-improvements
Draft

Optimize env.ts and eliminate dependency cycles in webapp#151
seansica wants to merge 27 commits intohijohnnylin:mainfrom
mitre:2-environment-variable-handling-improvements

Conversation

@seansica
Copy link
Contributor

Problem

Today, the webapp does not properly handle environment variables at runtime. There is no clear line of separation between runtime variables intended for the client versus the server. Parsing and validation are missing or minimal, and each variable is independently exported which is tedious to maintain and difficult to read.

In addition, there exists several dependency cycles in the webapp. The linter will catch them lest they be ignored by in-line bypass statements. This PR resolves two of them. The following still remain:

image

Fix

  • Introduces t3-oss/env-nextjs and Zod for parsing and validating environment variables at runtime.
  • All environment variables are now parsed by Zod at runtime
  • A console warning will log when the client accesses server-side variables (i.e., when a variable NOT prefixed with NEXT_PUBLIC_. ships to the client). This will (hopefully) help to eventually eliminate server-side variables from shipping to the client altogether (just a best practice).
  • There's also buildtime validation logic included for server-side conditions. For example, the webapp will not start if an inference key is not set.
  • Constants have been moved/aggregated in a new module, lib/consants.ts
  • Runtime variables which were derived from environment variables are being set by Zod transforms. This is nice because they are handled exactly like every other environment variable in lib/env.ts.
  • All processed environment variables are exported behind a single env object; this way we don't have to export every environment variable individually (see screenshot)

Testing

I can successfully build the app via npm run build:localhost on an Ubuntu VM. There are no linting errors. However, given how many files are touched, additional, extensive testing is recommended.

…orts

- Introduce ts-oss/ts-env for handling runtime environment variables
- Introduce Zod for parsing and validating runtime environment variables

BREAKING CHANGE: Individual environment variable exports are no longer available.
Import { env } instead and use env.VARIABLE_NAME to access values.
- lib/env.ts was refactored to stop exporting individual env variables
- Refactored all dependent modules to import { env } and use
env.VARIABLES_NAME pattern
Refactored remaining dependent modules to import { env } and use
env.VARIABLES_NAME pattern
This is the recommended setting in TypeScript 5.0+ for applications that use a bundler
…nstants

- Declare config values which are derived in createEnv using Zod transforms
- Migrate constants to separate module

BREAKING CHANGE: Downstream services must update their imports
Additionally remove old OPENROUTER_BASE_URL declaration in one of the routers
Resolves a linting error that came up during build
…ces to new module

- Resolves linting error 'import/no-cycle' caused by a dependency loop between lib/with-user.ts and lib/db/user.ts
- Decouples mutually dependent interfaces into new module, lib/types/auth.ts

BREAKING CHANGE: Breaks a ton of downstream routers that rely on RequestOptionalUser, RequestAuthedUser, and RequestAuthedAdminUser.
These will need to be refactored to import from the new lib/types/auth module.
- Resolves the following two linting errors in autointerp-shared.ts
- Cannot find module '@anthropic-ai/sdk/resources' or its corresponding type declarations.ts(2307)
- Cannot find module 'openai/resources' or its corresponding type declarations.ts(2307)
This popped up as a linting error at some point.
This popped up as a linting error at some point.
…and authOptions

- Moved functions that were causing the cycle to a new module, lib/auth/utils.ts
- Refactored all downstream/dependent modules to import from the new path

Tested and confirmed app can be built via 'npm run build:localhost'
- Update import paths for constants (some were still importing from env)
- Add missing environment variables GRAPH_SERVER_QWEN3_4B and GRAPH_RUNPOD_SERVER_QWEN3_4B
…ports

  - Replace direct env var imports with centralized env object
  - Add missing import statements for error constants and types
  - Remove duplicate ASSET_BASE_URL import
  - Fix import organization and formatting inconsistencies
  - Remove trailing whitespace and formatting issues
@seansica seansica marked this pull request as draft September 26, 2025 19:56
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.

1 participant