withastro / astro

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

astro:env does not generate .astro/env.d.ts in develoment mode #11380

Closed xino1010 closed 3 months ago

xino1010 commented 4 months ago

Astro Info

Using last experimental type-safe environment variables features, types are not generated automatically in development mode.

import { getSecret } from "astro:env/server";

Cannot find module 'astro:env/server' or its corresponding type declarations.ts(2307)

I have to execute

npm run build

to remove the ts error.

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

No response

Describe the Bug

Code works but intellisense show an error.

Captura de pantalla 2024-06-30 a las 9 34 13

Looking the file env.d.ts

/// <reference path="../.astro/env.d.ts" />
/// <reference types="astro/client" />

new line is added but the file is not generated under .astro folder.

Same happens for backend vars.

Captura de pantalla 2024-06-30 a las 9 45 17

but if you execute npm run build, the error dissapears because typings file is generated.

What's the expected result?

Generate automatically env.d.ts file on develoment mode.

declare module "astro:env/client" {
  export const API_URL: string;
}

declare module "astro:env/server" {
  export const DB_HOST: string;
  export const DB_NAME: string;
  export const DB_PASS: string;
  export const DB_PORT: number;
  export const DB_USER: string;

  export const getSecret: (key: string) => string | undefined;
}

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-ey4hsq?file=.env

Participation

mogeko commented 4 months ago

You should run pnpm astro sync.

xino1010 commented 4 months ago

You should run pnpm astro sync.

should it be the expected behaviour? or astro could be generate types file automatically on dev mode?

florian-lefebvre commented 4 months ago

Yes dev should run sync under the hood so there may be a bug, I'll have a look

florian-lefebvre commented 4 months ago

I think the issue lies here https://github.com/withastro/astro/blob/main/packages/astro/src/core/sync/index.ts#L87. If there are no content collections, the env sync code may not be run. @xino1010 do you want to try fixing it?

ematipico commented 4 months ago

Ah yeah, that's because sync was originally meant for content collections. Now we are change its semantics, so we should remove that check at this point, or add an OR check for astro:env

xino1010 commented 4 months ago

I think the issue lies here https://github.com/withastro/astro/blob/main/packages/astro/src/core/sync/index.ts#L87. If there are no content collections, the env sync code may not be run. @xino1010 do you want to try fixing it?

No sorry, it exceed of my knowledge

Trombach commented 4 months ago

@florian-lefebvre I'm happy to submit a PR for this if that's ok?

florian-lefebvre commented 4 months ago

@Trombach sure do it! Feel free to reach out on Discord in #contribute if you need help

nemanjam commented 2 months ago

I get this too, they are simply undefined, I use "astro": "^4.14.4".

image

yarn dev, yarn sync, yarn build all of them fail.

username@computer3:~/Desktop/nemanjam.github.io$ yarn sync
yarn run v1.22.22
$ astro sync
8:47:02 AM [vite] Pre-transform error: Failed to load url astro:env/client (resolved id: astro:env/client) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:47:02 AM [vite] Pre-transform error: Failed to load url astro:env/server (resolved id: astro:env/server) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?

username@computer3:~/Desktop/nemanjam.github.io$ yarn build
yarn run v1.22.22
$ astro build
8:47:08 AM [vite] Pre-transform error: Failed to load url astro:env/client (resolved id: astro:env/client) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:47:08 AM [vite] Pre-transform error: Failed to load url astro:env/server (resolved id: astro:env/server) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:47:08 AM [vite] Pre-transform error: Failed to load url @/schemas/config (resolved id: @/schemas/config) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?

username@computer3:~/Desktop/nemanjam.github.io$ yarn dev
yarn run v1.22.22
$ astro dev
8:45:00 AM [vite] Pre-transform error: Failed to load url astro:env/client (resolved id: astro:env/client) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:45:00 AM [vite] Pre-transform error: Failed to load url astro:env/server (resolved id: astro:env/server) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:45:00 AM [vite] Pre-transform error: Failed to load url @/schemas/config (resolved id: @/schemas/config) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:45:00 AM [vite] Pre-transform error: Failed to load url @/utils/log (resolved id: @/utils/log) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:45:00 AM [vite] Pre-transform error: Failed to load url @/utils/validation (resolved id: @/utils/validation) in /home/username/Desktop/nemanjam.github.io/src/config.ts. Does the file exist?
8:45:00 AM [vite] Error when evaluating SSR module /src/config.ts: failed to import "astro:env/client"
|- Error: Cannot find module 'astro:env/client' imported from '/home/username/Desktop/nemanjam.github.io/src/config.ts'
    at nodeImport (file:///home/username/Desktop/nemanjam.github.io/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52945:19)
    at ssrImport (file:///home/username/Desktop/nemanjam.github.io/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52812:22)
    at eval (/home/username/Desktop/nemanjam.github.io/src/config.ts:3:50)
    at instantiateModule (file:///home/username/Desktop/nemanjam.github.io/node_modules/vite/dist/node/chunks/dep-BzOvws4Y.js:52870:11)
florian-lefebvre commented 2 months ago

@nemanjam 👋 Can you open a new issue? Thank you!

nemanjam commented 2 months ago

Here it is:

https://github.com/withastro/astro/issues/11816