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

bun's .env automatic loading feature doesn't work well with Vite's one #14912

Closed
7 tasks done
supermacro opened this issue Nov 8, 2023 · 5 comments
Closed
7 tasks done
Labels
wontfix This will not be worked on

Comments

@supermacro
Copy link

Describe the bug

I am using the build function to programmatically build my vite project.

I have a build.ts file that contains the build({ ... }) function invocation.

I've set the mode to 'production', however my .env.production is not being recognized and vite is incorrectly using the .env.local file.

I made a repro repository which contains two env files:

image

When I run the build via bun run build.ts (could have also have been via ts-node), and I search for https://myproduction.dev this value is not found. However, when I search for http://localhost:8080, I can see that the value is found.

image

Reproduction

https://gitlab.com/giioo/vite-build-bug-repro

Steps to reproduce

To reproduce, follow these instructions:

  • using either bun or ts-node, run the build.ts file
    • e.g. bun run ./build.ts
  • Notice that the built JS file inside of dist/assets/ does not reference the env from .env.production, but rather from .env.local even though the mode has been set to production for the build function inside of build.ts

System Info

System:
    OS: Linux 6.5 Pop!_OS 22.04 LTS
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 5.93 GB / 15.49 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm
    pnpm: 8.9.0 - ~/.local/share/pnpm/pnpm
    bun: 1.0.7 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 118.1.59.120
    Chrome: 118.0.5993.88
  npmPackages:
    @vitejs/plugin-react: ^4.0.3 => 4.1.1 
    vite: ^4.4.5 => 4.5.0

Used Package Manager

pnpm

Logs

No response

Validations

@sapphi-red
Copy link
Member

Running node --no-warnings=ExperimentalWarning --loader ts-node/esm build.ts in the root directory works for me. https://myproduction.dev is found in the built file.
I tried with both Windows and WSL.

@sapphi-red sapphi-red added the cannot reproduce The bug cannot be reproduced label Nov 8, 2023
@supermacro
Copy link
Author

Are you able to repro if using bun?

@sapphi-red
Copy link
Member

Ah, it did reproduce with bun run.

It's happening because bun loads .env.local and Vite prioritizes environment vars over vars in .env files.
But it seems there's no way to disable the automatic .env load in bun (oven-sh/bun#5515).

I'm not sure what Vite can do here.

@sapphi-red sapphi-red removed pending triage cannot reproduce The bug cannot be reproduced labels Nov 9, 2023
@sapphi-red sapphi-red changed the title build function does not use appropriate env file based on 'mode' option bun's .env automatic loading feature doesn't work well with Vite's one Nov 9, 2023
@bluwy
Copy link
Member

bluwy commented Nov 9, 2023

I'd vote closing this one. We don't explicitly support Bun and this doesn't seem like something we should special-case.

@patak-dev
Copy link
Member

Let's close this one. I also think it should be handled on Bun's side

@patak-dev patak-dev closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 24, 2023
@sapphi-red sapphi-red added the wontfix This will not be worked on label Apr 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants