withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.42k stars 2.37k forks source link

Pre-transform error: Failed to load url astro:env/server #11606

Closed mariusbolik closed 1 month ago

mariusbolik commented 1 month ago

Astro Info

Astro                    v4.13.1
Node                     v20.16.0
System                   macOS (x64)
Package Manager          bun
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I installed the sentry astro integration. The integration needs an auth token in its configuration. But doen't seem to be possible to use astro:env in the astro.config.mjs file.

This is my config:

import { defineConfig, envField } from "astro/config";
import cloudflare from "@astrojs/cloudflare";
import sentry from "@sentry/astro";
import { SENTRY_AUTH_TOKEN } from 'astro:env/server';

export default defineConfig({
  output: "server",
  experimental: {
    actions: true,
    env: {
      schema: {
        SENTRY_AUTH_TOKEN: envField.string({
          context: "server",
          access: "secret",
          default: "",
        }),
      },
    },
  },
  adapter: cloudflare({}),
  integrations: [
    sentry({
      dsn: "https://18aba3b773c3ef1891e719e73a311ec4@o4505279164252160.ingest.us.sentry.io/4507712395214848",
      sourceMapsUploadOptions: {
        project: "pwa-astro",
        authToken: SENTRY_AUTH_TOKEN,
      },
    }),
  ],
});

This is the error message:

$ bun run astro dev
$ astro dev
10:35:35 AM [vite] Pre-transform error: Failed to load url astro:env/server (resolved id: astro:env/server) in /Users/marius/Projects/my-project/pwa-astro/astro.config.mjs. Does the file exist?
10:35:37 AM [vite] Error when evaluating SSR module /Users/marius/Projects/my-project/pwa-astro/astro.config.mjs: failed to import "astro:env/server"
|- Error: Cannot find module 'astro:env/server' imported from '/Users/marius/Projects/my-project/pwa-astro/astro.config.mjs'
    at nodeImport (file:///Users/marius/Projects/my-project/pwa-astro/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52726:19)
    at ssrImport (file:///Users/marius/Projects/my-project/pwa-astro/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52591:22)
    at eval (/Users/marius/Projects/my-project/pwa-astro/astro.config.mjs:6:37)
    at async instantiateModule (file:///Users/marius/Projects/my-project/pwa-astro/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52650:5)

[astro] Unable to load your Astro config

Cannot find module 'astro:env/server' imported from '/Users/marius/Projects/my-project/pwa-astro/astro.config.mjs'
  Stack trace:
    at nodeImport (file:///Users/marius/Projects/my-project/pwa-astro/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:52726:19)
    at eval (/Users/marius/Projects/my-project/pwa-astro/astro.config.mjs:6:37)
error: script "astro" exited with code 1
error: script "dev" exited with code 1

What's the expected result?

It should be possible to use secrets in astro.config.mjs

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-bs2nvk?file=astro.config.mjs

Participation

mariusbolik commented 1 month ago

Could you take a look at this? @florian-lefebvre :)

florian-lefebvre commented 1 month ago

Hi! astro:env/* is a virtual module so it can only be accessed in a context where it's loaded by vite. It's not the case of the astro config file, use process.env.X instead.

This has been asked several times but idk what we plan to do yet, feel free to subscribe to https://github.com/withastro/roadmap/pull/894 as we'll talk about it next week