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

Env variables breaking build (even when comented) #1441

Closed
Antoine-lb opened this issue May 13, 2021 · 5 comments
Closed

Env variables breaking build (even when comented) #1441

Antoine-lb opened this issue May 13, 2021 · 5 comments

Comments

@Antoine-lb
Copy link

Describe the bug
build command breaking when adding import.meta.env.VITE_MESSAGE in the code, and still breaks even if the code is comented, I have to fully remove it for it to build.

The dev command works fine.

Tried without adapter and withadapter-vercel, but the problem percists.

Logs

vite v2.3.2 building SSR bundle for production...
✓ 7 modules transformed.
[rollup-plugin-dynamic-import-variables] Unexpected token (8:348)
file: /Users/me/Desktop/issue/src/routes/index.svelte:8:348
> Unexpected token (8:348)
SyntaxError: Unexpected token (8:348)
    at Object.pp$4.raise (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:15685:13)
    at Object.pp.unexpected (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:13377:8)
    at Object.pp.expect (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:13371:26)
    at Object.pp$3.parseObj (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:15321:12)
    at Object.pp$3.parseExprAtom (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:15060:17)
    at Object.pp$3.parseExprSubscripts (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:14887:19)
    at Object.pp$3.parseMaybeUnary (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:14864:17)
    at Object.parseMaybeUnary (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:19630:29)
    at Object.pp$3.parseExprOps (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:14799:19)
    at Object.pp$3.parseMaybeConditional (/Users/me/Desktop/issue/node_modules/rollup/dist/shared/rollup.js:14782:19)
error Command failed with exit code 1.

To Reproduce
(I was able to reproduce multiple times)

  1. Install with npm init svelte@next my-app (all default)
  2. run yarn to install depencencies
  3. Add <h1>Hello, {import.meta.env.VITE_MESSAGE}</h1> in ./src/routes/index.svelte
  4. Create .env file (in root of the project) and add VITE_MESSAGE="World" to it
  5. yarn build

Expected behavior
It should build.

Information about your SvelteKit Installation:

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers
  System:
    OS: macOS 11.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
    Memory: 914.12 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.18.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
  Browsers:
    Brave Browser: 90.1.24.84
    Chrome: 90.0.4430.212
    Firefox: 88.0
    Safari: 14.0.2
  npmPackages:
    @sveltejs/kit: next => 1.0.0-next.107 
    svelte: ^3.34.0 => 3.38.2
  • Package.json
{
  "name": "~TODO~",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
    "build": "svelte-kit build",
    "preview": "svelte-kit preview"
  },
  "devDependencies": {
    "@sveltejs/kit": "next",
    "svelte": "^3.34.0"
  },
  "type": "module",
  "dependencies": {
    "@fontsource/fira-mono": "^4.2.2",
    "@lukeed/uuid": "^2.0.0",
    "cookie": "^0.4.1"
  }
}
  • Your adapter (e.g. Node, static, Vercel, Begin, etc...)
    Tried without adapter and withadapter-vercel, but the problem percists.

Severity
Seems pretty serious.

Additional context
The example is taken from the official FAQ env vars

@benmccann benmccann added the documentation Improvements or additions to documentation label May 13, 2021
@benmccann benmccann added this to the 1.0 milestone May 13, 2021
@benmccann benmccann added bug Something isn't working and removed bug Something isn't working documentation Improvements or additions to documentation labels May 13, 2021
@benmccann benmccann removed this from the 1.0 milestone May 13, 2021
@benmccann
Copy link
Member

I just tested and could not reproduce this issue. The only difference is that I used npm instead of yarn. Perhaps something is broken with yarn + Vite? We generally don't recommend using yarn. See https://kit.svelte.dev/faq

@Antoine-lb
Copy link
Author

Tried with npm and yarn multiple times. Update my mac to 11.3.1, upgraded npm to 7.13.0. They all end up with the exact same error. I'm able to reproduce it every single time.

For it to break there most be a .env file with a variable being imported in the file.

For example: VITE_MESSAGE breaks, but VITE_MESSAGE_2 does not break the code because I'm importing import.meta.env.VITE_MESSAGE and not import.meta.env.VITE_MESSAGE_2.

And even when I comment import.meta.env.VITE_MESSAGE in index.svelte it still breaks, I have to fully remove it for it to build. There most be something in the parsing for that behaviour to happen.

I have node v12.18.0, I don't really want to update because it may break other code bases that I have, but I guess is good enough right?

Ping me for any clarification

@benmccann
Copy link
Member

I'm not quite following what you're saying. Can you provide a repo that reproduces this?

@JBusillo
Copy link
Contributor

This is related to #1396. Vite's 'define' plugin indiscriminately replaces environment variables without escaping quotes when doing its transform. Since the entire sourcemap is represented as a string that's delimited by quotes ("), the replaced map is invalid. Since comments are contained within the map, you would still get the error.
I suggested a workaround within that issue.

@benmccann
Copy link
Member

I'm going to close this as a duplicate of #1396

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

No branches or pull requests

4 participants