Skip to content

Vitest workspace run doesn't pick .env from individual projects #5448

@semoal

Description

@semoal

Describe the bug

I have a monorepo with different projects in packages/*, when we run vitest inside of this packages the .env.{mode} it's picked correctly, but when running in the root of the monorepo, 0 variables are caught.

We have a vitest.workspace.mts file in the root with the following:

export default ["packages/*", "apps/*"];

We define the vitest.config.ts in the packages/ like this:

import tsconfigPaths from "vite-tsconfig-paths";
import { defineProject } from "vitest/config";

export default defineProject({
  envPrefix: ["SHERPAS_", "DATABASE_", "FLY_", "PRIMARY_"],
  plugins: [
    tsconfigPaths({
      root: "../..",
    }),
  ],
  test: {
    hookTimeout: 20_000,
    exclude: [
      "node_modules",
      "dist",
      ".idea",
      ".git",
      ".cache",
      "src/tests/**/*.{ts,tsx}",
    ],
    environment: "jsdom",
  },
});

Initially created as discussion here: #5446

Reproduction

stackblitz.com/edit/vitest-dev-vitest-pxuer9?file=packages%2Flib1%2F.env.test

System Info

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M2 Pro
    Memory: 72.27 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/Library/Caches/fnm_multishells/70491_1711450154965/bin/node
    npm: 10.2.4 - ~/Library/Caches/fnm_multishells/70491_1711450154965/bin/npm
    pnpm: 8.15.5 - ~/Library/pnpm/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Chrome: 123.0.6312.86
    Safari: 17.0
  npmPackages:
    vitest: ^1.4.0 => 1.4.0

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: workspaceIssues and PRs related to the workspace featurep3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions