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

[Bug]: import.meta.env undefined since beta6 #3029

Closed
abenhamdine opened this issue Jul 26, 2024 · 12 comments
Closed

[Bug]: import.meta.env undefined since beta6 #3029

abenhamdine opened this issue Jul 26, 2024 · 12 comments

Comments

@abenhamdine
Copy link

abenhamdine commented Jul 26, 2024

Version

System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 5800H with Radeon Graphics
    Memory: 1.77 GB / 15.36 GB
  Browsers:
    Edge: Chromium (123.0.2420.65)
  npmPackages:
    @rsbuild/core: 1.0.1-beta.6 => 1.0.1-beta.6
    @rsbuild/plugin-eslint: 1.0.0 => 1.0.0
    @rsbuild/plugin-react: 1.0.1-beta.6 => 1.0.1-beta.6
    @rsbuild/plugin-type-check: 1.0.1-beta.6 => 1.0.1-beta.6

Details

Since beta6, our app breaks because import.meta.env is no more accessible in the code at runtime :

The error occurs at the following code :

let isDialogOpened: boolean = false

// line 43 here after
if (import.meta.env.PUBLIC_SENTRY_DSN) {
	Sentry.init({
		dsn: import.meta.env.PUBLIC_SENTRY_DSN,
		environment: import.meta.env.PUBLIC_MODE,
// ...etc
App.tsx:43 Uncaught TypeError: Cannot read properties of undefined (reading 'PUBLIC_SENTRY_DSN')
    at ./src/App.tsx (App.tsx:43:1)
    at __webpack_require__ (validators.ts:82:1)
    at fn (validators.ts:82:1)
    at ./src/index.tsx (getInfosForValidationAbsence.ts:108:1)
    at __webpack_require__ (validators.ts:82:1)
    at validators.ts:82:1
    at __webpack_require__.O (validators.ts:82:1)
    at validators.ts:82:1
    at validators.ts:82:1

if we revert back to beta 5, everything works fine again.

Reproduce link

Sorry I will try to post a reproduction later

Reproduce Steps

yarn run dev

@abenhamdine abenhamdine added the 🐞 bug Something isn't working label Jul 26, 2024
@chenjiahan
Copy link
Member

This problem should be introduced in Rspack 1.0.0-beta.0. If there is a reproduction, it will be very helpful to fix the bug.

Copy link
Contributor

Hello @abenhamdine. Please provide a reproduction repository or online demo. For background, see Why reproductions are required. Thanks ❤️

@abenhamdine
Copy link
Author

abenhamdine commented Jul 26, 2024

I fail to reproduce the error in a basic project : it works fine with a simple react project with App.tsx and one line of code.

So the issue is probably triggered by a combination of modules/configs/whatever but I can't figure out where it comes from

I tried to replace import.meta.env with process.env but same result : ReferenceError: process is not defined

Any hint/help would be useful !

@chenjiahan
Copy link
Member

You can try to remove all the source code in your project and only keep the related lines of code. Then check what will happen.

@abenhamdine
Copy link
Author

You can try to remove all the source code in your project and only keep the related lines of code. Then check what will happen.

Good idea : I succeed in reproducing the bug : https://github.com/abenhamdine/repro-rsbuild-beta6

Please pay attention that you have to be in the folder /client to install and start the app

@chenjiahan
Copy link
Member

This undefined issue is introduced in Rspack's web-infra-dev/rspack#7229. The current output is align with webpack v5.

  • Input:
if (import.meta.env.PUBLIC_SENTRY_DSN) {
  • Output (Rspack 1.0 alpha):
if (undefined) {
  • Output (Rspack 1.0 beta.0 and webpack v5):
if (/* unsupported import.meta.env.PUBLIC_SENTRY_DSN */ undefined.PUBLIC_SENTRY_DSN) {
  • Output (Vite v5)
var define_import_meta_env_default = { BASE_URL: "/", MODE: "production", DEV: false, PROD: true, SSR: false };
if (define_import_meta_env_default.PUBLIC_SENTRY_DSN) {
  console.log(1);
}

@LingyuCoder Is undefined.PUBLIC_SENTRY_DSN expected? This will cause a runtime error, perhaps we should use a more robust output.

@chenjiahan
Copy link
Member

@abenhamdine PUBLIC_SENTRY_DSN is not defined in your .env.development file.

If this value is empty in development, it is recommended to define it as PUBLIC_SENTRY_DSN="" rather than leaving it undefined.

@abenhamdine
Copy link
Author

@abenhamdine PUBLIC_SENTRY_DSN is not defined in your .env.development file.

If this value is empty in development, it is recommended to define it as PUBLIC_SENTRY_DSN="" rather than leaving it undefined.

Oh yes you're right, it solves the issues, thx !

it is recommended to define it as PUBLIC_SENTRY_DSN="" rather than leaving it undefined.

I would even say it's now required to define it because of this new behaviour.

@LingyuCoder
Copy link
Contributor

LingyuCoder commented Jul 28, 2024

Is undefined.PUBLIC_SENTRY_DSN expected? This will cause a runtime error, perhaps we should use a more robust output.

Just aligned with webpack. Perhaps this needs futher discussion.

@chenjiahan
Copy link
Member

chenjiahan commented Jul 29, 2024

I understand that Rspack and webpack do not support import.meta.env by default because it is not part of the specification.

The question is, why replace import.meta.env with undefined instead of leaving it as is?

Copy link
Contributor

github-actions bot commented Aug 4, 2024

As the issue was labelled with need reproduction, but no response in 5 days. This issue will be closed. Feel free to comment and reopen it if you have any further questions. For background, see Why reproductions are required.

由于该 issue 被标记为 "需要重现",但在 5 天内没有回应,因此该 issue 将被关闭。如果你有任何进一步的问题,请随时发表评论并重新打开该 issue。背景请参考 为什么需要最小重现

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2024
@chenjiahan chenjiahan reopened this Aug 4, 2024
@chenjiahan
Copy link
Member

Tracking in: web-infra-dev/rspack#7364

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants