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

Removal of baseUrl from .nuxt/tsconfig.json in nuxt v3.7.0 breaks Cypress tsconfig.json path aliasing #26512

Closed
JasonLandbridge opened this issue Mar 27, 2024 · 4 comments

Comments

@JasonLandbridge
Copy link
Sponsor

JasonLandbridge commented Mar 27, 2024

Environment

Working directory: /mnt/PROJECTS/ProjectA 12:37:32 PM
Nuxt project info: 12:37:32 PM


  • Operating System: Linux
  • Node Version: v18.19.1
  • Nuxt Version: 3.7.4
  • CLI Version: 3.11.1
  • Nitro Version: 2.9.5
  • Package Manager: npm@9.8.0
  • Builder: -
  • User Config: ssr, srcDir, devServer, runtimeConfig, app, modules, quasar, typescript, macros, lodash, i18n, css, imports, alias, components, vite, nitro, build, hooks, telemetry
  • Runtime Modules: nuxt-quasar-ui@2.0.4, @vueuse/nuxt@10.2.1, @nuxtjs/i18n@8.0.0-beta.13, @vue-macros/nuxt@1.3.18, nuxt-lodash@2.5.0, @nuxt/test-utils/module@3.12.0, @pinia/nuxt@0.4.11
  • Build Modules: -

Reproduction

Reproduction link: https://github.com/JasonLandbridge/Cypress-Path-Alias-Reproduction

Describe the bug

When using Nuxt in combination with Cypress and Cypress-Vite it is recommended to have the following tsconfig layout:
Cypress Typescript support:

<Project folder>
├── .nuxt
│   └── tsconfig.json
├── cypress
│   └── tsconfig.json
├── src
└── tsconfig.json

Where each tsconfig.json extends from the other to allow Typescript path aliasing to work correctly in Cypress E2E tests.

This worked great until Nuxt v3.7.0 where in PR #22410 the baseUrl was removed from the .nuxt/tsconfig.json

However, this baseUrl is crucial for Cypress to correctly translate path aliasing to the real path.

Consider the following tsconfig.json files:

// .nuxt/tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@mock": [
        "../src/mock-data"
      ],
      "@mock/*": [
        "../src/mock-data/*"
      ],
  },
}
// [ROOT]/tsconfig.json
{
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "noImplicitAny": false,
    "types": [
      "unplugin-vue-define-options/macros-global",
      "@nuxtjs/i18n",
      "@pinia/nuxt",
      "vitest/importMeta",
      "vitest/globals",
      "@testing-library/cypress",
      "cypress"
    ]
  }
}
// cypress/tsconfig.json
{
  "extends": "../tsconfig.json", // Needed for the path aliases
  "compilerOptions": {
    "types": ["cypress", "cypress-signalr-mock", "@testing-library/cypress", "node"],
    "baseUrl": "./",
    "sourceMap": false,
    "target": "ES2019"
  },
  "include": ["**/*.ts", "../node_modules/cypress/types", "../node_modules/@types/mocha"],
}

The project path in this case is
/mnt/PROJECTS/ProjectA/

Before v3.7.0

vite:resolve-details [fs] /mnt/PROJECTS/ProjectA/src/mock-data/mock-base 
-> /mnt/PROJECTS/ProjectA/src/mock-data/mock-base.ts +0ms

After v3.7.0

  vite:resolve-details [fallthrough] @mock +0ms
 # The Project folder is now missing from the path
  vite:resolve-details [fallthrough] /mnt/PROJECTS/src/mock-data +0ms

But, when I manually add "baseUrl": "./", to .nuxt/tsconfig.json, when on v3.7.0, then everything works again as it should and the paths get resolved correctly.

It seems as if there is a ../ to many when resolving the paths in the newer Nuxt versions.

Would it be possible to have a nuxt.config.ts option that will add back "baseUrl": "./", to .nuxt/tsconfig.json or is there some other workaround possible?

Thanks!

Copy link
Contributor

Would you be able to provide a reproduction? 🙏

More info

Why do I need to provide a reproduction?

Reproductions make it possible for us to triage and fix issues quickly with a relatively small team. It helps us discover the source of the problem, and also can reveal assumptions you or we might be making.

What will happen?

If you've provided a reproduction, we'll remove the label and try to reproduce the issue. If we can, we'll mark it as a bug and prioritize it based on its severity and how many people we think it might affect.

If needs reproduction labeled issues don't receive any substantial activity (e.g., new comments featuring a reproduction link), we'll close them. That's not because we don't care! At any point, feel free to comment with a reproduction and we'll reopen it.

How can I create a reproduction?

We have a couple of templates for starting with a minimal reproduction:

👉 https://stackblitz.com/github/nuxt/starter/tree/v3-stackblitz
👉 https://codesandbox.io/s/github/nuxt/starter/v3-codesandbox

A public GitHub repository is also perfect. 👌

Please ensure that the reproduction is as minimal as possible. See more details in our guide.

You might also find these other articles interesting and/or helpful:

@JasonLandbridge
Copy link
Sponsor Author

@danielroe I've created an reproduction link with instructions here: https://github.com/JasonLandbridge/Cypress-Path-Alias-Reproduction

Please let me know if I can provide more information!

@danielroe
Copy link
Member

It feels like this is an upstream issue in cypress/vite-tsconfig-paths - they need to support resolving relative paths within a file that is extended.

Here's an example of how to do that:

https://github.com/nuxt/module-builder/blob/main/src/commands/build.ts#L279-L295

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2024
@JasonLandbridge
Copy link
Sponsor Author

Workaround: aleclarson/vite-tsconfig-paths#123 (comment)

In the root tsconfig.json

{
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./.nuxt/",  //<-- Add this
  }

then inherrit from this tsconfig.json in /cypress/tsconfig.json

{
  "extends": "../tsconfig.json", // Needed for the path aliases
}

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

No branches or pull requests

2 participants