Skip to content

[Feature Request] Infer the test environment from the command varlock run launches #1145

Open

Description

@timche

Description

When varlock run launches a test runner, infer the test environment from the command itself, so VARLOCK_ENV is test without passing a flag.

Motivation

The Bun guide now recommends varlock run -- bun test. But varlock run resolves before the runner starts, and it's the runner that sets NODE_ENV=test, so VARLOCK_ENV comes out as development. Repro on 1.21.0:

# @defaultSensitive=false
# ---
ENV_NAME=$VARLOCK_ENV
// x.test.ts
import { test } from "bun:test";
test("env", () => console.log("ENV_NAME =", process.env.ENV_NAME, "NODE_ENV =", process.env.NODE_ENV));

varlock run -- bun test prints ENV_NAME = development NODE_ENV = test.

The builtin variables docs cover this and recommend passing the flag explicitly (VARLOCK_ENV=test varlock run -- vitest), so every test script in every package of a monorepo carries it. Forgetting it on one package runs its tests against development config without any error.

Proposed Solution

In varlock run, when VARLOCK_ENV is not already set, look at the command being run: bun test, vitest, jest, node --test, mocha (and the same through bunx/npx) resolve with VARLOCK_ENV=test. An explicit VARLOCK_ENV or @currentEnv value still wins. It could be an opt-in root decorator if guessing from argv is too implicit.

Alternatives

  • Pass the flag in every test script (what the docs recommend today).
  • A varlock test -- <cmd> shorthand that sets the test environment.

Additional Information

The same timing problem applies to anything that sets its own NODE_ENV after startup, but test runners are by far the common case.

Activity

  1. pullfrog commented on Sep 28, 2026

    @pullfrog
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions