withastro / astro

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

```astro:env``` breaks in Content Collection ```config.ts``` #11754

Closed davemasterdave closed 2 months ago

davemasterdave commented 3 months ago

Astro Info

Astro                    v4.14.2
Node                     v20.14.0
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/node
Integrations             @astrojs/tailwind
                         @astrojs/solid-js
                         @astrojs/react

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

No response

Describe the Bug

Astro environments are not working in the configuration for Content Collections config.ts. It seems that the generation code for the Content Collections is running before the generation code for the environment variables runs, therefore the content collection generation process is breaking? It seems that the environments needs to be fully generated first before the content collections generate?

[ERROR] [types] [GenerateContentTypesError] `astro sync` command failed to generate content collection types: Cannot find module 'astro:env/client' imported from './src/content/config.ts'
Stack trace:
    at syncContentCollections (file:///.../node_modules/astro/dist/core/sync/index.js:156:11)
    at async sync (file:///.../node_modules/astro/dist/core/sync/index.js:45:10)
    at async runCommand (file:///.../node_modules/astro/dist/cli/index.js:153:27)
  Caused by:
  Cannot find module 'astro:env/client' imported from '.../src/content/config.ts'
    at nodeImport (file:///.../node_modules/vite/dist/node/chunks/dep-NjL7WTE1.js:52919:19)
    at eval (.../src/content/config.ts:5:37)

What's the expected result?

Environment variables are useful for dynamically configuring where content should be loaded from, and the astro:env module should be accessible for the code based content configuration

Link to Minimal Reproducible Example

pending

Participation

github-actions[bot] commented 2 months ago

Hello @davemasterdave. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

cielorz commented 2 months ago

+1, meet same issue with astro:env

PS: currently I use import.meta.env as a temporary workaround.

davemasterdave commented 2 months ago

+1, meet same issue with astro:env

PS: currently I use import.meta.env as a temporary workaround.

@cielorz I am actually trying to switch the adaptor to run on Cloudflare Pages/Edge instead of Node.js, the import.meta.env workaround unfortunately does not work on Cloudflare it seems. What do you recommend?

twodft commented 2 months ago

+1, meet same issue with astro:env PS: currently I use import.meta.env as a temporary workaround.

@cielorz I am actually trying to switch the adaptor to run on Cloudflare Pages/Edge instead of Node.js, the import.meta.env workaround unfortunately does not work on Cloudflare it seems. What do you recommend?

I use Cloudflare adapter either, import.meta.env should work fine. this code works in my project.

import { createDirectus, rest, staticToken } from '@directus/sdk';

import { type DirectusSchema } from './schemas/';

// import { DIRECTUS_API_URL } from 'astro:env/client';
// import { DIRECTUS_AUTH_TOKEN } from 'astro:env/server';
// Use import.meta.env as a workaround
const DIRECTUS_API_URL = import.meta.env.DIRECTUS_API_URL;
const DIRECTUS_AUTH_TOKEN = import.meta.env.DIRECTUS_AUTH_TOKEN;

const directus = createDirectus<DirectusSchema>(DIRECTUS_API_URL)
    .with(rest())
    .with(staticToken(DIRECTUS_AUTH_TOKEN));

export { directus };
florian-lefebvre commented 2 months ago

Reproduction:

florian-lefebvre commented 2 months ago

I think it's caused by https://github.com/withastro/astro/blob/main/packages/astro/src/env/vite-plugin-env.ts#L34. Instead of returning early if we sync, we should just skip validation