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

Add new option to prevent setting process.env.NODE_ENV #1334

Closed
4 tasks done
Dunqing opened this issue May 19, 2022 · 3 comments
Closed
4 tasks done

Add new option to prevent setting process.env.NODE_ENV #1334

Dunqing opened this issue May 19, 2022 · 3 comments

Comments

@Dunqing
Copy link
Member

Dunqing commented May 19, 2022

Clear and concise description of the problem

I want to prevent vitest from setting process.env.NODE_ENV.

Because vite supports setting process.env.NODE_ENV in env files, e.g. .env files. But vite only allows setting when process.env.NODE_ENV is undefined.

I want to prevent vitest set NODE_ENV.

Because vite support process.env.NODE_ENV set in env file, e.g: .env file. but vite only allow to set when process.env.NODE_ENV is undefined

Related code

process.env.NODE_ENV ??= options.mode || 'test'

Related pr:

vitejs/vite#8218

Suggested solution

Add preventSetNodeEnv option, default is false.
Don't set process.env.NODE_ENV when preventSetNodeEnv is true.

Alternative

No response

Additional context

vitejs/vite#8218 (comment)

Validations

@sheremet-va
Copy link
Member

Your examples shows that NODE_ENV is not set, if it was already set. So, what is the problem here?

@Dunqing
Copy link
Member Author

Dunqing commented May 19, 2022

If it is already set, causing NODE_ENV in the env file to not work.

https://github.com/vitejs/vite/pull/8218/files#diff-11e17761d4ecfee8f8fde15c6d79b7bc0260176396a30dfd8e6f6bbaf5af4745R399

@Dunqing
Copy link
Member Author

Dunqing commented May 23, 2022

I found the workaround.
We can override process.env.NODE_ENV with the env option.

if (ctx.config.env)
Object.assign(process.env, ctx.config.env)

@Dunqing Dunqing closed this as completed May 23, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants