feat: warn and add opt-out flag for browse CLI .env auto-loading#2361
Merged
Conversation
A user reported .env values in one project being silently overridden by a stale shell-level key from another project, since browse auto-loads a project's .env file into process.env with no way to disable it. Add BROWSE_LOAD_DOTENV to opt in/out of auto-loading explicitly, and print a one-time deprecation warning to stderr when a value is actually sourced from .env with the toggle left unset -- auto-loading will be disabled by default in a future release. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: c888e25 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
There was a problem hiding this comment.
1 issue found across 4 files
Confidence score: 5/5
packages/cli/tests/cli-cloud-contract.test.tsonly validatesBROWSE_LOAD_DOTENV="0", so a future change could accidentally break the documented"false"/"no"opt-out behavior without being caught, potentially confusing users who rely on README examples. This PR looks safe to merge, but add explicit tests for"false"and"no"to lock the contract in.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/cli/tests/cli-cloud-contract.test.ts">
<violation number="1" location="packages/cli/tests/cli-cloud-contract.test.ts:737">
P3: The README documents `0`, `false`, and `no` as equivalent opt-out values for `BROWSE_LOAD_DOTENV`, but the tests only cover `"0"`. Consider adding test coverage for `"false"` and `"no"` to ensure the documented behavior is exercised and doesn't regress.</violation>
</file>
Architecture diagram
sequenceDiagram
participant User as User Shell
participant CLI as browse CLI
participant DotEnv as dotenv Module
participant ProcessEnv as process.env
participant Stderr as stderr Output
participant Backend as Backend API
Note over User,Backend: Browse CLI startup flow with .env auto-loading
User->>CLI: Run browse command (e.g., browse cloud projects list)
CLI->>DotEnv: Check for .env in current directory
alt .env file exists
DotEnv->>DotEnv: Parse .env file
alt BROWSE_LOAD_DOTENV unset (implicit default)
DotEnv->>ProcessEnv: Load .env variables into process.env
DotEnv->>DotEnv: Check if any .env vars are NEW (not already in process.env)
alt New vars applied
DotEnv-->>Stderr: Print deprecation warning listing variable names
else All vars already set in process.env
DotEnv-->>Stderr: No warning (silent load)
end
else BROWSE_LOAD_DOTENV = "1" / "true" / explicit value
DotEnv->>ProcessEnv: Load .env variables into process.env
DotEnv-->>Stderr: No warning (explicit opt-in)
else BROWSE_LOAD_DOTENV = "0" / "false" / "no"
DotEnv->>DotEnv: Skip .env loading entirely
DotEnv-->>Stderr: No warning (explicit opt-out)
end
else No .env file
DotEnv-->>CLI: No .env to load
end
CLI->>ProcessEnv: Read BROWSERBASE_API_KEY
alt Key is set
CLI->>Backend: API call with API key
Backend-->>CLI: Response
CLI-->>User: Success output
else Key is missing
CLI-->>User: Error: Missing API key
end
Note over DotEnv,Stderr: Future release: default flips to skip .env loading
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Extend the opt-out test to also exercise "false" and "no" (plus their uppercase forms) alongside "0", matching what the README documents as equivalent opt-out values. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Ships the deprecation warning as a patch so it reaches users fast; the follow-up PR that flips the default will be a minor bump.
pirate
reviewed
Jul 14, 2026
pirate
approved these changes
Jul 14, 2026
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.
Summary
browsehas auto-loaded a.envfile from the current directory on startup since it moved into this monorepo, with no way to turn it off. A user reported.envvalues in one project being silently overridden by a stale shell-level key from another project -- the shell-levelBROWSERBASE_API_KEYfrom an earlier project took precedence over the correct key in a different project's.env, so session replays went to the wrong Browserbase account.The agreed direction (per internal eng review) is that CLI tools used across many unrelated projects/directories shouldn't auto-load
.envat all -- onlyprocess.envshould matter, and it's on the user toexportvars or source.envthemselves. Ripping that out in one PR would break the existing install base that relies on the implicit behavior, so this PR is the non-breaking first step:browsestill loads.envby default.BROWSE_LOAD_DOTENVenv var makes that behavior explicitly toggleable:0/false/no(case-insensitive) skips.envloading entirely today; any other explicit value (e.g.1) keeps loading with no nagging..envfile actually applies a variable not already inprocess.env, we print a one-time deprecation warning to stderr naming the variable(s), so people relying on the implicit default get advance notice.A future PR (not this one) will flip the default of
BROWSE_LOAD_DOTENVto off, once the warning has had time to reach users.This complements #2360, which adds a
browse doctorwarning whenprocess.envand.env/.env.localvalues disagree -- a useful on-demand diagnostic, but it doesn't touch the auto-load behavior itself. This PR does not depend on #2360.E2E Test Matrix
Built the CLI locally (
pnpm turbo run build --filter=browse) and ran the builtbin/run.jsdirectly in a scratch temp dir containing a.envwith a fakeBROWSERBASE_API_KEY.node bin/run.js doctor --jsonin scratch dir with.envcontainingBROWSERBASE_API_KEY=<fake>,BROWSE_LOAD_DOTENVunset[browse] Loaded BROWSERBASE_API_KEY from .env. Auto-loading .env is deprecated and will be disabled by default in a future release -- export these variables in your shell instead, or set BROWSE_LOAD_DOTENV=1 to keep this behavior explicitly once that happens. Set BROWSE_LOAD_DOTENV=0 to opt out today. Run \browse doctor` to check for conflicts with your shell environment./ stdoutverdict: "ok"` / exit 0.envand now warns exactly once, naming the applied var.node bin/run.js doctor --remotein same dir withBROWSE_LOAD_DOTENV=0[fail] browserbase BROWSERBASE_API_KEY is not set/ no stderr / exit 1.enventirely -- the key never reachesprocess.env, no warning printed (explicit choice).node bin/run.js doctor --remotein same dir withBROWSE_LOAD_DOTENV=1[ok] browserbase BROWSERBASE_API_KEY is set/ no stderr / exit 0.envwith zero nagging.pnpm exec vitest run tests/cli-cloud-contract.test.ts tests/doctor.test.tsTest Files 2 passed (2)/Tests 69 passed (69).env-loads-config contract (unchanged default) plus: warns when unset+applied, silent whenBROWSE_LOAD_DOTENV=1, and fails auth (key never loaded, no warning) for each of0/false/no/FALSE/NOasBROWSE_LOAD_DOTENV-- added the extra opt-out aliases per cubic review feedback below.pnpm exec eslint bin/run.js tests/cli-cloud-contract.test.ts+pnpm exec prettier --check bin/run.js README.md tests/cli-cloud-contract.test.ts+pnpm exec tsc --noEmit -p tsconfig.jsonLinear
STG-2579 (linked via branch name).