-
Notifications
You must be signed in to change notification settings - Fork 392
Description
The Convex CLI (convex dev
) always creates a .env.local
file and adds it to .gitignore
, even when the required environment variables (CONVEX_DEPLOYMENT
, CONVEX_URL
, etc.) are already available in the shell environment.
This behavior is problematic for users who:
- Use tools like
direnv
with.envrc
files - Manage secrets with external tools (1Password, AWS Secrets Manager, etc.)
- Have environment variables set via CI/CD or container orchestration
- Want to avoid leaving any environment settings in local filesystem
Current Behavior
The CLI creates .env.local
in these scenarios even when env vars are already set:
convex dev
(withCONVEX_DEPLOY_KEY
in environment)convex dev --url "$CONVEX_URL"
(with URL in environment)convex dev --url "$CONVEX_URL" --admin-key "$CONVEX_ADMIN_KEY"
(with both in environment)
Expected Behavior
The CLI should detect existing environment variables and skip file creation when:
CONVEX_DEPLOYMENT
is already set in the shell environment- The appropriate URL variable (e.g.,
NEXT_PUBLIC_CONVEX_URL
) is already set in the shell environment - Authentication is available via
CONVEX_DEPLOY_KEY
or CLI arguments
Reproduction Steps
-
Set environment variables (e.g., via direnv):
export CONVEX_DEPLOYMENT="dev:amazing-animal-123" export CONVEX_URL="https://amazing-animal-123.convex.cloud" export NEXT_PUBLIC_CONVEX_URL="https://amazing-animal-123.convex.cloud" export CONVEX_DEPLOY_KEY="dev:amazing-animal-123|eyJ..."
-
Run
convex dev
-
Observe that
.env.local
is created despite variables being available
Root Cause Analysis
Looking at the source code in npm-packages/convex/src/cli/lib/envvars.ts
:
The envVarWriteConfig
function only checks for environment variables in files (.env.local
, .env
) but not in the shell environment (process.env
):
// This only reads from files, not process.env
const existingFileContent = ctx.fs.readUtf8File(envFile);
const config = dotenv.parse(existingFileContent);
Similarly, writeDeploymentEnvVar
in deployment.ts
only checks file-based environment variables.