-
Notifications
You must be signed in to change notification settings - Fork 0
Fix environment variables validation error during deployment#2
Fix environment variables validation error during deployment #2
Conversation
🧙 Sourcery is reviewing your pull request!
Tips and commands
Interacting with Sourcery
- Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
- Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with@sourcery-ai issueto create an issue from it. - Generate a pull request title: Write
@sourcery-aianywhere in the pull
request title to generate a title at any time. You can also comment
@sourcery-ai titleon the pull request to (re-)generate the title at any time. - Generate a pull request summary: Write
@sourcery-ai summaryanywhere in
the pull request body to generate a PR summary at any time exactly where you
want it. You can also comment@sourcery-ai summaryon the pull request to
(re-)generate the summary at any time. - Generate reviewer's guide: Comment
@sourcery-ai guideon the pull
request to (re-)generate the reviewer's guide at any time. - Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the
pull request to resolve all Sourcery comments. Useful if you've already
addressed all the comments and don't want to see them anymore. - Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull
request to dismiss all existing Sourcery reviews. Especially useful if you
want to start fresh with a new review - don't forget to comment
@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request
summary, the reviewer's guide, and others. - Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Co-authored-by: larp0 <204380501+larp0@users.noreply.github.com>
larp0
commented
Jun 3, 2025
@l r
Overall Summary of PR
This PR improves the deployment CLI by better handling environment variable sync errors returned by the server in the body of a 200 response. Previously, only a success boolean on the upload result was checked, but this change adds an extra condition for error in the response body to detect error cases. It also updates the zod schema EnvironmentVariableResponseBody to support either a successful response { success: true } or an error response { error: string; variableErrors?: any[] }. Finally, it adds comprehensive tests to verify this new schema.
Mermaid Diagrams
1. Whole Repo High-Level Component Diagram (Simplified)
flowchart TD
%% ------------ Nodes ------------
A["CLI Commands e.g. deploy.ts"]
B["API Client apiClient.js"]
C["Build System (buildWorker.js, buildImage.js)"]
D["Config Loader (config.js)"]
E["Utilities"]
F["Env Var Utilities (localEnvVars.js, dotEnv.js)"]
G["Logging Utilities (logs.js, cliOutput.js)"]
H["Git Utilities (gitMeta.js)"]
I["Session Management (session.js)"]
J["Temp Directory (tempDirectories.js)"]
K["External Tools (docker called via tinyexec)"]
L["Schema & Types (core/src/v3/schemas/api.ts)"]
%% ------------ Links ------------
A --> B
A --> C
A --> D
A --> E
A --> K
E --> F
E --> G
E --> H
E --> I
E --> J
B --> L
%% ------------ Styling -----------
classDef external fill:#f9f,stroke:#333,stroke-width:1px,color:#333
class K external
This diagram shows the CLI command as the main orchestrator, calling the API client for server interactions and triggering local build and deployment processes. Utilities support env var loading, logging, git metadata, and session handling. The schema/types package defines API request/response shapes for type safety and validation.
2. Diagram of What Changed in This PR
flowchart TD
%% ---------- Nodes ----------
A["_deployCommand function"]
B["SyncEnvVarsWithServer call"]
C["UploadResult Response Handling"]
F["spinner stop success message"]
I["EnvironmentVariableResponseBody Schema"]
J["EnvironmentVariableResponseBody Tests"]
%% ---------- Old logic ----------
subgraph "Old logic"
D{"uploadResult.success?"}
C --> D
D -- false --> E["failDeploy with uploadResult.error"]
D -- true --> F
end
%% ---------- New logic ----------
subgraph "New logic"
G{"Does uploadResult.data contain error?"}
C --> G
G -- true --> H["failDeploy with uploadResult.data.error"]
G -- false --> F
end
%% ---------- Edges ----------
A --> B
B --> C
B --> I
I --> J
The key modification is the new conditional branch on the response body to check for errors even if HTTP success is true. The schema evolves from a simple { success: boolean } to a discriminated union between success and error objects, and tests verify all cases including invalid inputs.
3. Mermaid Diagram for Tech Debt & Proposed Improvements
flowchart TB
%% -------- Nodes --------
A["_deployCommand (massive function)"]
B["2000\+ lines of deploy.ts file (monolith)"]
C["Tech Debt: Difficult to maintain and test"]
D["Duplication: Spinner and error handling repeated"]
E["Loose typing between HTTP layers and CLI layers"]
F["Refactor: Break _deployCommand into smaller, testable functions"]
G["Improve types & validation on API Client responses"]
H["Use discriminated unions and exhaustive checks"]
I["Introduce handler classes or state machines for deployment stages"]
J["Better lifecycle and error management"]
%% -------- Links --------
A --> B
B --> C
B --> D
B --> E
C --> F
D --> F
E --> G
G --> H
F --> I
I --> J
%% -------- Clickable nodes --------
click F "https://github.com/triggerdotdev/cli/issues/123" "Example Refactor Issue"
click G "https://zod.dev" "Zod Validation Guide"
The massive _deployCommand function is a notable tech debt hotspot. It mixes deployment logic, error handling, spinner UI updates, and helps bugs slip through. Future steps should modularize deployment phases and introduce more comprehensive, typed handling of server responses to avoid odd conditions like error-in-200-responses.
Code Review & Roasting
Deployment Changes
- Good: Adding an
else if ("error" in uploadResult.data)makes the CLI MUCH more resilient to server responses where the status is 200 but the body has an error. This edge case can occur and had been ignored so far. - Roast:
- How do you expect anyone to maintain this giant
_deployCommandfunction? I bet you have to scroll 1000 lines to find where you want to fix a bug. - Checking
"error" in uploadResult.datawithout a type guard or schema validation can be dangerous if the response shape changes in future. You've added a schema but didn't fully integrate type checking here. - The use of
failDeployto bail out is fine, but throwingSkipLoggingErrorafter looks awkward and force throws control flow. A structured approach would be cleaner. - Why so many optional command options and flags that are hidden? Hidden options make usability a nightmare unless you document them very well somewhere else.
- Use consistent internationalization and error formatting.
"env vars"pluralization is manually coded; consider a small utility for this kind of UX polish.
- How do you expect anyone to maintain this giant
Schema Changes
- Good:
- Switching from a simple boolean response schema to a discriminated union for the environment variable API response is a solid step to increase robustness.
- The schema supports optional nested error details (
variableErrors) which is nice for diagnostics.
- Roast:
- The schema treats
success: trueas a literal only, but what if the API ever returnssuccess: falsewith more detail? Your tests explicitly rejectsuccess: false. Is that aligned with the API spec? - You use
z.array(z.any()).optional()forvariableErrors. Why not a more specific schema? This weakens the typing and lets weird payloads slip through. - The test suite is all positive/snapshot style but you skip negative checking for fields like extra properties or malformed types.
- Some tests repeat similar code – consider parametrizing tests with a table-driven approach to reduce noise.
- No newline at end of file is a minor style nitpick but still worth fixing.
- The schema treats
Summary
This PR thoughtfully addresses a neglected edge case in env var sync responses. The addition of proper discriminated union schemas and test coverage is a nice quality boost. However, the core deploy.ts remains a monstrous, unmaintainable mess, full of hidden options and tangled logic that frankly makes adding new features or fixing bugs a nightmare.
You boys should really:
- Break that
_deployCommandinto smaller units. - Use more expressive types to avoid unsafe
inchecks. - Expand your test coverage to guard against future schema changes.
- Remove or better document hidden flags to improve CLI UX.
Otherwise, congrats! At least the code won't crash immediately in the face of weird env var server errors anymore. That's progress.
If you want me to draw detailed call stacks, dependency graphs, or suggest automated code mod strategies to untangle your CLI deploy beast, just ask!
...ment variabl ... (truncated)
0xrinegade
commented
Feb 16, 2026
ClaudeV encountered an error while processing this PR.\n\nTask: This PR fixes the deployment error: "Failed to fetch environment variables: 200 Validation error: Required" that occurs when syncing environment variables during deployment.\n\n## Problem\n\nThe error occ\n... (truncated)\nStatus: Failed (exit code 1)
Uh oh!
There was an error while loading. Please reload this page.
This PR fixes the deployment error: "Failed to fetch environment variables: 200 Validation error: Required" that occurs when syncing environment variables during deployment.
Problem
The error occurred due to a schema mismatch between the server's error responses and the client's expectations:
/api/v1/projects/$projectRef/envvars/$slug/importreturns error responses like{ error: "...", variableErrors: [...] }with HTTP 200 statusEnvironmentVariableResponseBodyschema only accepted{ success: boolean }successfield was missingSolution
Schema Changes
EnvironmentVariableResponseBodyto be a union type that accepts both:{ success: true }{ error: string, variableErrors?: any[] }Client Logic Changes
Testing
The fix is minimal and surgical - it only affects the specific validation error without changing any other behavior, ensuring backward compatibility while resolving the deployment issue.
Fixes #1.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
binaries.prisma.shIf you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.