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

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

Closed
1 task
xino1010 opened this issue Jun 30, 2024 · 11 comments · Fixed by #11415
Closed
1 task

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

xino1010 opened this issue Jun 30, 2024 · 11 comments · Fixed by #11415
Assignees
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: env related to the way astro handles `.env` or `import.meta.env` or `process.env` (scope)

Comments

@xino1010
Copy link

xino1010 commented Jun 30, 2024

Astro Info

  • astro: 4.11.3
  • node: 18.18.2

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

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jun 30, 2024
@florian-lefebvre florian-lefebvre self-assigned this Jun 30, 2024
@mogeko
Copy link

mogeko commented Jun 30, 2024

You should run pnpm astro sync.

@xino1010
Copy link
Author

xino1010 commented Jun 30, 2024

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
Copy link
Member

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

@florian-lefebvre florian-lefebvre added - P4: important Violate documented behavior or significantly impacts performance (priority) feat: env related to the way astro handles `.env` or `import.meta.env` or `process.env` (scope) and removed needs triage Issue needs to be triaged labels Jul 1, 2024
@florian-lefebvre
Copy link
Member

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
Copy link
Member

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
Copy link
Author

xino1010 commented Jul 1, 2024

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
Copy link

Trombach commented Jul 2, 2024

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

@florian-lefebvre
Copy link
Member

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

@nemanjam
Copy link

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
Copy link
Member

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

@nemanjam
Copy link

Here it is:

#11816

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: env related to the way astro handles `.env` or `import.meta.env` or `process.env` (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants