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

Optional Environment Variables #7364

Open
Blankeos opened this issue Jul 30, 2024 · 12 comments
Open

Optional Environment Variables #7364

Blankeos opened this issue Jul 30, 2024 · 12 comments
Labels
bug Something isn't working stale

Comments

@Blankeos
Copy link

System Info

System:
OS: macOS 14.5
CPU: (8) arm64 Apple M2
Memory: 88.97 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v20.14.0/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
pnpm: 9.5.0 - ~/.nvm/versions/node/v20.14.0/bin/pnpm
bun: 1.1.21 - ~/.bun/bin/bun
Browsers:
Chrome: 127.0.6533.73
Safari: 17.5

Details

When trying to access env vars via:
process.env.PUBLIC_NICE
import.meta.env.PUBLIC_NICE

And I don't have PUBLIC_NICE in my .env. It throws an error instead of just being undefined.

Afaik in Vite, I'm allowed to have optional env variables. I usually setup my config in a typescript file with default values for local like:

export const config = {
   NICE: process.env.PUBLIC_NICE ?? "my default";
};

Reproduce link

No response

Reproduce Steps

  1. Make an rspack project

  2. Go to App.tsx and write process.env.PUBLIC_NICE somewhere

  3. Run the app (expect: error)

  4. Add PUBLIC_NICE="Hello" to .env

  5. Run the app (expect: no errors)

@Blankeos Blankeos added bug Something isn't working pending triage The issue/PR is currently untouched. labels Jul 30, 2024
@CPunisher
Copy link
Contributor

Maybe you can define an empty process.env with DefinePlugin.

https://rspack.dev/zh/plugins/webpack/define-plugin
https://rsbuild.dev/config/source/define

new rspack.DefinePlugin({
  "process.env": {},
}),

@abenhamdine
Copy link

abenhamdine commented Aug 1, 2024

it's indeed a non documented breaking change in rspack beta 1.0.0 beta0, see web-infra-dev/rsbuild#3029

@LingyuCoder
Copy link
Contributor

Currently Rspack/Webpack does not support import.meta.env. You can use DefinePlugin and process.env instead. The previous version of Rspack would replace import.meta.env.xxxx with undefined, this may result in entering an unexpected branch without knowing.

@LingyuCoder LingyuCoder removed the pending triage The issue/PR is currently untouched. label Aug 1, 2024
@abenhamdine
Copy link

abenhamdine commented Aug 1, 2024

Currently Rspack/Webpack does not support import.meta.env. You can use DefinePlugin and process.env instead. The previous version of Rspack would replace import.meta.env.xxxx with undefined, this may result in entering an unexpected branch without knowing.

Hmm rsbuild/rspack supports import.meta.env, we use it in our code.
But since rsbuild beta 6 (rspack 1.0.0 beta 0), if the variable is not defined in the environment, import.meta.env is replaced by undefined, thus trying to access undefined.PUBLIC_MY_VARIABLE results in a error.

@LingyuCoder
Copy link
Contributor

Currently Rspack/Webpack does not support import.meta.env. You can use DefinePlugin and process.env instead. The previous version of Rspack would replace import.meta.env.xxxx with undefined, this may result in entering an unexpected branch without knowing.

Hmm rsbuild/rspack supports import.meta.env, we use it in our code.
But since rsbuild beta 6 (rspack 1.0.0 beta 0), if the variable is not defined in the environment, import.meta.env is replaced by undefined, thus trying to access undefined.PUBLIC_MY_VARIABLE results in a error.

ok, we will find a better way to handle this

Copy link

stale bot commented Sep 30, 2024

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Sep 30, 2024
@LingyuCoder
Copy link
Contributor

bump

@jkrems
Copy link

jkrems commented Nov 12, 2024

It's worth noting that rspack is the outlier here: The following code will work across all runtimes and bundlers but breaks in rspack.

expect(import.meta.env?.SOME_MISSING_VAR ?? 42).toBe(42);

See "import⁠.meta.env?.X" row in https://bundlers.dev/javascript/modules/import.meta/.

@stale stale bot removed the stale label Nov 12, 2024
@hardfist
Copy link
Contributor

It's worth noting that rspack is the outlier here: The following code will work across all runtimes and bundlers but breaks in rspack.

expect(import.meta.env?.SOME_MISSING_VAR ?? 42).toBe(42);
See "import⁠.meta.env?.X" row in bundlers.dev/javascript/modules/import.meta.

yeah it's a bug and we're gonna fix it

@takki2602
Copy link

I'm not quite sure if this fits here exactly but I can imagine that it is related to the same bug.
When I log import.meta.env I always get undefined. Even if I get a value back with import.meta.env.test.

I have also noticed the following which irritates me and where I expect different behavior.

console.log(import.meta.env) // undefined

console.log(import.meta.env['PUBLIC_TEST']) // value
console.log(import.meta.env.PUBLIC_TEST]) // value


let x = 'PUBLIC_TEST';
console.log(import.meta.env[x])  // Cannot read properties of undefined (reading 'PUBLIC_TEST')

Perhaps this can also be taken into account.

@grvdisco
Copy link

It's worth noting that rspack is the outlier here: The following code will work across all runtimes and bundlers but breaks in rspack.
expect(import.meta.env?.SOME_MISSING_VAR ?? 42).toBe(42);
See "import⁠.meta.env?.X" row in bundlers.dev/javascript/modules/import.meta.

yeah it's a bug and we're gonna fix it

I was wondering if there have been any updates on this issue? 🤞

Copy link

stale bot commented Jan 27, 2025

This issue has been automatically marked as stale because it has not had recent activity. If this issue is still affecting you, please leave any comment (for example, "bump"). We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@stale stale bot added the stale label Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

8 participants