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

Setting shamefully-hoist=false will make nitro generated types fail #23984

Closed
awdr74100 opened this issue Oct 27, 2023 · 8 comments
Closed

Setting shamefully-hoist=false will make nitro generated types fail #23984

awdr74100 opened this issue Oct 27, 2023 · 8 comments

Comments

@awdr74100
Copy link
Contributor

Environment

  • Operating System: Darwin
  • Node Version: v20.9.0
  • Nuxt Version: 3.8.0
  • CLI Version: 3.9.1
  • Nitro Version: 2.7.0
  • Package Manager: pnpm@8.9.2
  • Builder: -
  • User Config: devtools, typescript, runtimeConfig, experimental, modules
  • Runtime Modules: @nuxtjs/tailwindcss@6.8.1
  • Build Modules: -

Reproduction

repo: https://github.com/awdr74100/nuxt-bug-reproduction

nuxt.config.ts

export default defineNuxtConfig({
  devtools: { enabled: false },
  runtimeConfig: {
    resend: {
      apiKey: "",
    },
  },
});

.npmrc

shamefully-hoist=false

Describe the bug

const config = useRuntimeConfig();

_resend = new Resend(config.resend.apiKey);

shamefully-hoist=true

(property) RuntimeConfig.resend: {
    apiKey: string;
}

shamefully-hoist=false

(index) NitroRuntimeConfig[string]: any

Additional context

No response

Logs

No response

@awdr74100 awdr74100 changed the title Setting the shamefully-hoist attribute to false will cause the useRuntimeConfig type to become invalid Setting shamefully-hoist=false will make useRuntimeConfig() fail to get the types Oct 28, 2023
@danielroe danielroe changed the title Setting shamefully-hoist=false will make useRuntimeConfig() fail to get the types Setting shamefully-hoist=false will make nitro generated types fail Oct 30, 2023
@dargmuesli
Copy link
Member

A workaround is to install nitropack as devDependency. (@danielroe this is what we had a chat about)

@danielroe
Copy link
Member

We could add nitropack to tsconfig paths, as well as some other key packages (at least the ones we auto-import), which might also mean we don't need to use full path in unimport, etc.

@woldtwerk
Copy link

my current workaround is:

public-hoist-pattern[]=*nitropack*

#25112 (comment)

@danielwaltz
Copy link

Maybe already implied by the title, but I figured it was worth explicitly mentioning that this also prevents TypeScript from being aware of import.meta.env.

In my projects using pnpm, trying to access import.meta.env without an implemented workaround gives this type error:

image

I think this is especially damaging as env is missing entirely instead of at least being typed as Record<string, any>, possibly leading people to believe that you are unable to access environment variables with import.meta at all and requiring you to use either @ts-ignore or @ts-expect-error comments to disable TypeScript completely on those lines if you are already aware of its existence.

I see this is a possible barrier/blocker to getting Nuxt users to migrate from using process to import.meta, although it is good that it only impacts people using pnpm without configuring shamefully-hoist!

@danielroe
Copy link
Member

This should be resolved in v3.11+.

@Sun-ZhenXing
Copy link

This problem seems to still exist in Nuxt v3.11.2. I am using pnpm 9.1.2.

@danielroe
Copy link
Member

Would you provide a reproduction? 🙏

@danielwaltz
Copy link

Took a crack at a reproduction myself because unfortunately I am also still observing this in my pnpm backed projects.

https://stackblitz.com/edit/pnpm-import-meta-env-typescript-reproduction?file=app.vue

Loading up that stackblitz and running pnpm typecheck should reveal the issue. The console.log shows the contents of import.meta.env at runtime as expected, but TypeScript errors with the following message:

app.vue:4:25 - error TS2339: Property 'env' does not exist on type 'ImportMeta'.

This error also appears inside your editor, though it looks like stackblitz doesn't support that.

Also if it helps my personal site is using pnpm + nuxt nightly with the new app/ directory structure and can be used as an alternative reproduction if desired. Just for giggles I like keeping it on the bleeding edge of Nuxt because I think it's fun to check out all the awesome work you and the whole team are doing early. 🤓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Later
Development

No branches or pull requests

6 participants