fix: avoid EISDIR crash when .env is a directory#721
Draft
posthog[bot] wants to merge 1 commit into
Draft
Conversation
readApiKeyFromEnv guarded each candidate path with fs.existsSync, which returns true for directories too, so a project with a directory named .env or .env.local (common with Python virtualenvs) crashed the subsequent readFileSync with EISDIR. Read directly and skip on any read error instead. Generated-By: PostHog Code Task-Id: 2e86216f-b802-4c88-88b5-0118cea5c89a
🧙 Wizard CIRun the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands: Test all apps:
Test all apps in a directory:
Test an individual app:
Show more apps
Results will be posted here when complete. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Users running the wizard in a project where
.envor.env.localis a directory (common when a Python virtualenv is named.env) hit an uncaughtEISDIR: illegal operation on a directory, readcrash that aborts the API-key-from-env lookup.readApiKeyFromEnv()guarded each candidate path withfs.existsSync(), which returnstruefor directories as well as files, so the subsequentfs.readFileSync()threwEISDIR.Why: keep the wizard's env-key fallback working instead of crashing for these project layouts.
Changes
In
src/utils/env-api-key.ts, read each candidate directly inside atry/catchand continue to the next on any read error (coversEISDIRfor directories andENOENTfor missing files), ultimately returningundefined.Test plan
Added
src/utils/__tests__/env-api-key.test.tscovering the normal.env/.env.localreads, precedence, and the directory case (no throw, falls through).pnpm build && pnpm test && pnpm fixall pass.Created with PostHog Code from an inbox report.