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

Docusaurus 3.6.3: Typescript cannot find module @theme #10733

Closed
5 of 7 tasks
jackw opened this issue Dec 2, 2024 · 8 comments
Closed
5 of 7 tasks

Docusaurus 3.6.3: Typescript cannot find module @theme #10733

jackw opened this issue Dec 2, 2024 · 8 comments
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests)

Comments

@jackw
Copy link

jackw commented Dec 2, 2024

Have you read the Contributing Guidelines on issues?

Prerequisites

  • I'm using the latest version of Docusaurus.
  • I have tried the npm run clear or yarn clear command.
  • I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • I have tried creating a repro with https://new.docusaurus.io.
  • I have read the console error message carefully (if applicable).

Description

Updating docusaurus from 3.6.0 to 3.6.3 appears to break type aliasing (at least in our mono-repo). I cannot track this down but anything that imports from @theme doesn't resolve to what should be theme-classic types anymore. I can fix it by adding the following to tsconfig.json which seems a bit strange for a minor update.

  "compilerOptions": {
    "baseUrl": ".",
+    "types": ["@docusaurus/theme-classic"]
  },

I've tried bumping TS, removing global.d.ts, scaffolding a fresh docusaurus website with npx create-docusaurus@latest docusaurus-website classic --typescript and comparing files but I cannot figure it out.

Reproducible demo

grafana/plugin-tools#1312

Steps to reproduce

  1. clone grafana/plugin-tools
  2. check-out branch renovate/docusaurus-dependencies
  3. run npm i to install deps
  4. run npm typecheck
  5. See lots of error TS2307: Cannot find module '@theme/*' or its corresponding type declarations. errors

Expected behavior

For the type aliases to resolve correctly.

Actual behavior

Type aliases don't resolve resulting in lots of error TS2307: Cannot find module '@theme/*' or its corresponding type declarations. errors.

Your environment

Self-service

  • I'd be willing to fix this bug myself.
@jackw jackw added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels Dec 2, 2024
@ahnpnl
Copy link

ahnpnl commented Dec 3, 2024

I have the same issue

@slorber slorber removed the status: needs triage This issue has not been triaged by maintainers label Dec 3, 2024
@slorber
Copy link
Collaborator

slorber commented Dec 3, 2024

Thanks for reporting, this is related to #10694 (comment)

I'll study your Grafana tools repo and see if I can find a better setup that doesn't break sites.

@ahnpnl it can help if you also share a repro

@ahnpnl
Copy link

ahnpnl commented Dec 3, 2024

Hi, https://github.com/kulshekhar/ts-jest/tree/main/website you can use this repo and the failed PR is kulshekhar/ts-jest#4638

@jackw
Copy link
Author

jackw commented Dec 3, 2024

Thanks for reporting, this is related to #10694 (comment)

I'll study your Grafana tools repo and see if I can find a better setup that doesn't break sites.

Thanks for the info and the link @slorber. So the current workaround would be the following in the websites tsconfig (as that is how it was previously):

"types": [
  "node",
  "@docusaurus/module-type-aliases",
  "@docusaurus/theme-classic"
],

@slorber
Copy link
Collaborator

slorber commented Dec 5, 2024

I still think the change we did was right: our base TS config shouldn't explicitly restrict types, and keep TS default behavior.

However, removing types made it break your sites. It's because of a specific reason: since nowhere in your code there is any import XYZ from '@docusaurus/theme-classic', then TS doesn't know that it has to load ambient theme types provided by this package.

There are multiple solutions, such as:

  • adding a types field yourself (but this comes with the side-effect of whitelisting the types, not just including the ones we need)
  • a triple-slash direct at the very top of a .d.ts: /// <reference types="@docusaurus/theme-classic" />
  • an import of @docusaurus/theme-classic anywhere in your code

But to me the root of the problem is due to the half-typed setup of your sites, mostly because you use JS for your config files instead of TS.

By default, our init template imports theme/preset types automatically: https://github.com/facebook/docusaurus/blob/main/examples/classic-typescript/docusaurus.config.ts

In particular:

import type * as Preset from '@docusaurus/preset-classic';

As soon as you properly type your config file according to the preset you use, all the necessary ambient theme files will become available for typechecking.

I think we should keep the current base TS setup, and instead you should improve your site's typesafety. So unless there's a good reason, I'm going to close this issue. Please let me know if this works for you.

I thought about using typeRoots but it didn't really work as I expected. Also if we declared something like @docusaurus/** in typeRoots, we would possibly load ambient theme types of packages you don't even use, so I don't think it's a better option. Explicitly importing the types that you actually use is better.

@slorber slorber closed this as not planned Won't fix, can't repro, duplicate, stale Dec 5, 2024
@slorber slorber added the closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests) label Dec 5, 2024
@slorber
Copy link
Collaborator

slorber commented Dec 5, 2024

Note, if you want to keep a JS config file, you can get the same benefit with JSDoc, that is now provided by default in our init template:

/** @type {import('@docusaurus/types').Config} */
const config = {
  ...generalConfig,
  url: 'https://grafana.com/',
  baseUrl: 'developers/plugin-tools/',
  plugins,
  presets: [
    [
      'classic',
      /** @type {import('@docusaurus/preset-classic').Options} */
      ({
        docs: {
          ...presetsDocs,
          routeBasePath: '/',
        },
        theme: presetsTheme,
        blog: false,
      }),
    ],
  ],
}

Important: make sure to wrap the option object with extra () otherwise the type doesn't get applied properly and the ambient types are not loaded.

@slorber
Copy link
Collaborator

slorber commented Dec 5, 2024

@ahnpnl same for you. Your config is in TS, and you should rather import/use the preset type:

import type {Config} from '@docusaurus/types';
import type * as Preset from '@docusaurus/preset-classic';

const config: Config = {
  title: 'My Site',
  presets: [
    [
      'classic',
      {
        docs: {
          
        },
        blog: {
          
        },
      } satisfies Preset.Options,
    ],
  ],
};

Somehow I think it's a feature that Docusaurus doesn't typecheck in your case: it's a sign that your TS setup is not ideal, not typechecking preset options 😄

@jackw
Copy link
Author

jackw commented Dec 6, 2024

I think we should keep the current base TS setup, and instead you should improve your site's typesafety. So unless there's a good reason, I'm going to close this issue. Please let me know if this works for you.

I see no reason not to move our configs to .ts files. @slorber thanks so much for taking the time to write up such a detailed reply with a selection of solutions to the problem. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution closed: wontfix A fix will bring significant overhead, or is out of scope (for feature requests)
Projects
None yet
Development

No branches or pull requests

3 participants