kit icon indicating copy to clipboard operation
kit copied to clipboard

`import.meta.env` undefined after upgrade from 405 to 432

Open sidharthv96 opened this issue 3 years ago • 11 comments

Describe the bug

import.meta.env is undefined after upgrading to v432, causing vitest to fail.

image

Reproduction

PR of the upgrade branch. Only contains changes for the upgrade.

  • https://github.com/mermaid-js/mermaid-live-editor/pull/983 (Closed with @brev's fix)
  • https://github.com/mermaid-js/mermaid-live-editor/pull/994

Logs

❯ node_modules/@sveltejs/kit/src/runtime/app/env.js:4:41
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/lib/util/serde.test.ts [ src/lib/util/serde.test.ts ]
      2|  * @type {import('$app/env').browser}
      3|  */
      4| export const browser = !import.meta.env.SSR;
       |                                         ^
      5| 
      6| /**
 ❯ async /Users/sidv/dev/mermaid-live-editor/src/lib/util/stats.ts:1:256
 ❯ async /Users/sidv/dev/mermaid-live-editor/src/lib/util/state.ts:5:31
 ❯ async /Users/sidv/dev/mermaid-live-editor/src/lib/util/serde.test.ts:5:31

TypeError: CTest Files  1 failed | 1 passed (2)
     Tests  1 passed (1)
  Start at  10:12:58
  Duration  1.53s (transform 974ms, setup 110ms, collect 795ms, tests 15ms)

annot read properties of undefined (reading 'SSR')
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

System Info

System:
    OS: macOS 12.3
    CPU: (10) arm64 Apple M1 Pro
    Memory: 66.52 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.17.0 - /var/folders/~~/node
    Yarn: 1.22.19 - /var/folders/~~/yarn
    npm: 8.18.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.101
    Firefox: 103.0.2
    Safari: 15.4
  npmPackages:
    @sveltejs/adapter-static: 1.0.0-next.39 => 1.0.0-next.39 
    @sveltejs/kit: 1.0.0-next.432 => 1.0.0-next.432 
    svelte: 3.49.0 => 3.49.0 
    vite: 3.0.9 => 3.0.9 

Severity

blocking an upgrade

Additional Information

No response

sidharthv96 avatar Aug 25 '22 05:08 sidharthv96

Possibly the same underlying reason as in #5952

dummdidumm avatar Aug 25 '22 09:08 dummdidumm

I have been able to get around this (and related bugs) in Vitest by mocking $app/env, in my test setup file:

// ./test/setup.ts
beforeAll(() => {
  vi.mock('$app/env', () => ({
    browser: 'window' in globalThis,
  }))
})

brev avatar Aug 27 '22 00:08 brev

@brev thanks a lot! Fixed my issue temporarily.

sidharthv96 avatar Aug 27 '22 07:08 sidharthv96

+1 Dealing with the same issue

alex-madhive avatar Aug 29 '22 18:08 alex-madhive

I added the code snippet that @brev mentioned, and although it fixed some tests, I'm still getting a couple tests failing due to the same error. Any thoughts? P.S.: It's not the glob pattern that's wrong.

Here is my vite config:

import { sveltekit } from "@sveltejs/kit/vite";

/** @type {import('vite').UserConfig} */
const config = {
  plugins: [sveltekit()],
  server: {
    port: 3000,
    strictPort: true
  },
  optimizeDeps: {
    include: ["fast-fuzzy"]
  },
  ssr: {
    external: ["firebase"]
  },
  build: {
    target: "esnext"
  },
  test: {
    deps: {
      inline: [
        "luxon",
        "@peculiar/asn1-schema",
        "layercake",
        "@sentry/browser",
        "@sentry/tracing",
        "@sentry/utils",
      ]
    },
    environment: "jsdom",
    environmentOptions: {
      jsdom: {
        pretendToBeVisual: true
      }
    },
    globals: true,
    include: ["src/test/**/*.test.ts"],
    reporter: "verbose",
    setupFiles: [
      "./vitest/setup-files/import-testing-library.ts",
      "./vitest/setup-files/mock-logger.ts",
      "./vitest/setup-files/mock-mad-fire.ts",
      "./vitest/setup-files/mock-window.ts",
      "./vitest/setup-files/setup-crypto.ts",
      "./vitest/setup-files/temp-window-fix.ts"
    ]
  }
};

export default config;

alex-madhive avatar Aug 29 '22 18:08 alex-madhive

Tested after #6398 closed #5952, but this issue is still present. PR: https://github.com/mermaid-js/mermaid-live-editor/pull/994/files

sidharthv96 avatar Aug 30 '22 03:08 sidharthv96

Oh, I just accidentally stumbled across this, related?

https://github.com/sveltejs/kit/releases/tag/%40sveltejs%2Fkit%401.0.0-next.445

[breaking] rename $app/env to $app/environment, to disambiguate with $env/... (https://github.com/sveltejs/kit/pull/6334)

brev avatar Aug 31 '22 04:08 brev

@brev Nope, implemented this but still broken on my end.

alex-madhive avatar Aug 31 '22 15:08 alex-madhive

Also broken here

hnrq avatar Oct 11 '22 05:10 hnrq

We're also seeing this issue

pzuraq avatar Nov 04 '22 18:11 pzuraq

Adding

ssr: {
		noExternal: ['@sveltejs/kit']
	}

to the vite.config.js fixes the issue for me, although I'm not sure if this has other unintended side effects - one I know for sure is if you have @sveltejs/kit as a linked dependency ("../../kit") then it'll probably result in duplicated modules, causing confusing bugs.

dummdidumm avatar Dec 05 '22 09:12 dummdidumm

Is this still happening? If so, we're going to need a repro — not a branch of an existing project, but a proper standalone minimal reproduction

Rich-Harris avatar Jan 23 '23 05:01 Rich-Harris

Closing as we don't have a minimal reproduction to work with. Feel free to open a new issue with a repro

Rich-Harris avatar Mar 07 '23 15:03 Rich-Harris

This was the solution for us https://vitest.dev/guide/in-source.html

eunukasiko avatar Jul 04 '23 13:07 eunukasiko