Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
6 tasks done
semoal opened this issue Mar 28, 2024 · 2 comments · Fixed by #5476
Closed
6 tasks done

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

semoal opened this issue Mar 28, 2024 · 2 comments · Fixed by #5476
Labels
feat: workspace Issues and PRs related to the workspace feature p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@semoal
Copy link

semoal commented Mar 28, 2024

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

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) feat: workspace Issues and PRs related to the workspace feature and removed pending triage labels Mar 28, 2024
@semoal
Copy link
Author

semoal commented Mar 28, 2024

I've tried to fix it but no luck.. there's one moment that env empties and couldn't follow where is deleted/lost.
While this issue gets fixed -- no hurry. There' a workaround using dotenv-flow:

In the vitest|vite.config.m|ts file just put that, and will load the variables correctly from the root or inside the package.

import dotenvFlow from "dotenv-flow";
const env = dotenvFlow.config({ path: import.meta.dirname });

@hi-ogawa
Copy link
Contributor

The underlying issue might be same as #5016. Manually loading dotenv should be a good workaround.

To avoid mixing different environment variables from multiple projects, it might be better to load it during setupFiles, which is what I suggested in #5003 (reply in thread)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: workspace Issues and PRs related to the workspace feature p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants