aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.34k stars 50 forks source link

vite silently fails with `vite-tsconfig-paths@4.3.1` #137

Closed LucidityDesign closed 9 months ago

LucidityDesign commented 9 months ago

If using the latest release of vite-tsconfig-paths vite silently fails.

I cannot run vite, vitest or vite build. It just fails without giving any information. I tried vite v5.0.12, v5.0.11, v4.5.2 and a beta.

Unfortunately I can't give much more, but maybe this issue helps people who have similar problems. vite-tsconfig-paths@4.2.3 works

aleclarson commented 9 months ago

It just fails without giving any information.

Did you run it with DEBUG="vite-tsconfig-paths"?

LucidityDesign commented 9 months ago
$ DEBUG="vite-tsconfig-paths" npx vite
  vite-tsconfig-paths options.root   == undefined +0ms
  vite-tsconfig-paths project root   == /Users/username/pathToRoot +1ms
  vite-tsconfig-paths workspace root == /Users/username/pathToRoot +0ms
  vite-tsconfig-paths projects: [
  '/Users/username/pathToRoot/tsconfig.json'
] +7ms
aleclarson commented 9 months ago

What does your tsconfig look like?

msalettes commented 9 months ago

Hello, i think it is related to the upgrade of tsconfck to ^3.0.1. I had the same problem on my project.

A workaround , is to set parseNative: true in your plugin configuration.

...
tsconfigPaths({ parseNative: true }),
...
msalettes commented 9 months ago

I believe this version of tsconfck has an issue parsing your tsconfig.json file. By setting tsconfigPaths({ parseNative: true }), you instruct it to use the TypeScript compiler for parsing your tsconfig.json. That's my understanding.

sreedhap commented 9 months ago

Faced the same issue, specifying the root option where tsconfig.json can be found, solved the issue for us. Check if that helps you too!

{
   plugins: [tsconfigPaths({ root: tsconfigDir }), react()],
   ...
}
nodegin commented 9 months ago

Same here.. using parseNative: true does fix for me

aleclarson commented 9 months ago

Hard to debug this without a tsconfig.json example.

Pinging @dominikg (creator of tsconfck) so he can keep an eye on this.

Maxim-Mazurok commented 9 months ago

Having the same issue with nx.

I do have root already.

Adding parseNative doesn't help.

Here's an example config:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "module": "CommonJS",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "types": ["vitest"]
  },
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.library.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

base:

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "noUnusedLocals": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "ESNext",
    "module": "CommonJS",
    "lib": ["ESNext", "DOM"],
    "esModuleInterop": true,
    "baseUrl": ".",
    "strictNullChecks": true,
    "skipLibCheck": true,
    "paths": {
      "@nx/blabla": ["packages/blabla/src/index.ts"],
    }
  },
  "exclude": ["node_modules", "tmp"]
}

library:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "declaration": true,
    "types": ["node"]
  },
  "include": ["src/**/*.ts", "src/**/*.js"],
  "exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"]
}

spec:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
  },
  "include": [
    "vite.config.ts",
    "src/**/*.test.ts",
    "src/**/*.spec.ts",
    "src/**/*.test.tsx",
    "src/**/*.spec.tsx",
    "src/**/*.test.js",
    "src/**/*.spec.js",
    "src/**/*.test.jsx",
    "src/**/*.spec.jsx",
    "src/**/*.d.ts"
  ]
}

Hope this helps to debug

aleclarson commented 9 months ago

I'm closing this, seeing as the OP hasn't replied to my request and I don't want people dropping their possibly unrelated issues in here. A workaround has been proposed. If that doesn't help, your issue is likely different, so start a new thread.

lionelhorn commented 8 months ago

@aleclarson. Here is a full repro for what may be the same issue.

Context

Nx mono repo Node.js v18.18.2 Windows

Repro

Repro steps similar with both versions and branches From repo's root:

pnpm install
nx run deepkit:dev

Working with 4.2.3

Code runs properly. https://github.com/lionelhorn/deepkit-nx-nextjs/tree/vite-tsconfig-paths-4.2.3

Vite config rel path : apps/deepkit/vite.config.ts

  vite-tsconfig-paths options.root   == C:\PROG\_experiments\_deepkit\deepkit-nx-nextjs +0ms
  vite-tsconfig-paths project root   == C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/apps/deepkit +0ms
  vite-tsconfig-paths workspace root == undefined +0ms
  vite-tsconfig-paths projects: [
  'C:\\PROG\\_experiments\\_deepkit\\deepkit-nx-nextjs\\apps\\deepkit\\tsconfig.json',
  'C:\\PROG\\_experiments\\_deepkit\\deepkit-nx-nextjs\\apps\\nextjs\\tsconfig.json',
  'C:\\PROG\\_experiments\\_deepkit\\deepkit-nx-nextjs\\packages\\utils\\tsconfig.json'
] +2ms
  vite-tsconfig-paths config loaded: {
  configPath: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/apps/nextjs/tsconfig.json',
  config: {
    extends: '../../tsconfig.base.json',
    compilerOptions: {
      jsx: 'preserve',
      allowJs: true,
      esModuleInterop: true,
      allowSyntheticDefaultImports: true,
      strict: true,
      forceConsistentCasingInFileNames: true,
      noEmit: true,
      resolveJsonModule: true,
      isolatedModules: true,
      incremental: true,
      plugins: [ { name: 'next' } ],
      types: [ 'jest', 'node' ],
      module: 'esnext',
      rootDir: '../..',
      sourceMap: true,
      declaration: false,
      moduleResolution: 'node',
      emitDecoratorMetadata: true,
      experimentalDecorators: true,
      importHelpers: true,
      target: 'ES2022',
      lib: [ 'esnext', 'dom' ],
      skipLibCheck: true,
      skipDefaultLibCheck: true,
      baseUrl: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs',
      paths: {
        '@lionelhorn/deepkit': [ 'apps/deepkit/src/index.ts' ],
        '@lionelhorn/utils': [ 'packages/utils/src/index.ts' ]
      }
    },
    include: [
      '**/*.ts',
      '**/*.tsx',
      '**/*.js',
      '**/*.jsx',
      '../../apps/nextjs/.next/types/**/*.ts',
      '../../dist/apps/nextjs/.next/types/**/*.ts',
      'next-env.d.ts',
      '.next/types/**/*.ts'
    ],
    exclude: [
      'node_modules',
      'jest.config.ts',
      '**/*.spec.ts',
      '**/*.test.ts'
    ],
    compileOnSave: false
  }
} +5ms
  vite-tsconfig-paths compiled globs: {
  includers: [
    /^\.\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/,
    /^\.\/((?:[^/]*(?:\/|$))*)([^/]*)\.tsx$/,
    /^\.\/((?:[^/]*(?:\/|$))*)([^/]*)\.js$/,
    /^\.\/((?:[^/]*(?:\/|$))*)([^/]*)\.jsx$/,
    /^\.\.\/\.\.\/apps\/nextjs\/\.next\/types\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/,
    /^\.\.\/\.\.\/dist\/apps\/nextjs\/\.next\/types\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/,
    /^\.\/next-env\.d\.ts\/((?:[^/]*(?:\/|$))*)$/,
    /^\.\/next-env\.d\.ts$/,
    /^\.\/\.next\/types\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/
  ],
  excluders: [
    /^\.\/node_modules\/((?:[^/]*(?:\/|$))*)$/,
    /^\.\/jest\.config\.ts\/((?:[^/]*(?:\/|$))*)$/,
    /^\.\/jest\.config\.ts$/,
    /^\.\/((?:[^/]*(?:\/|$))*)([^/]*)\.spec\.ts$/,
    /^\.\/((?:[^/]*(?:\/|$))*)([^/]*)\.test\.ts$/
  ]
} +1ms
  vite-tsconfig-paths config loaded: {
  configPath: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/apps/deepkit/tsconfig.lib.json',
  config: {
    extends: './tsconfig.json',
    compilerOptions: {
      outDir: '../../dist/out-tsc',
      module: 'ES2022',
      declaration: true,
      types: [ 'node' ],
      jsx: 'react-jsx',
      rootDir: '../..',
      sourceMap: true,
      moduleResolution: 'node',
      emitDecoratorMetadata: true,
      experimentalDecorators: true,
      importHelpers: true,
      target: 'ES2022',
      lib: [ 'esnext', 'dom' ],
      skipLibCheck: true,
      skipDefaultLibCheck: true,
      baseUrl: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs',
      paths: {
        '@lionelhorn/deepkit': [ 'apps/deepkit/src/index.ts' ],
        '@lionelhorn/utils': [ 'packages/utils/src/index.ts' ]
      }
    },
    exclude: [ 'vite.config.ts', 'src/**/*.spec.ts', 'src/**/*.test.ts' ],
    include: [ 'src/**/*.ts', 'src/**/*.tsx' ],
    compileOnSave: false
  }
} +1ms
  vite-tsconfig-paths compiled globs: {
  includers: [
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/,
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.tsx$/
  ],
  excluders: [
    /^\.\/vite\.config\.ts\/((?:[^/]*(?:\/|$))*)$/,
    /^\.\/vite\.config\.ts$/,
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.spec\.ts$/,
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.test\.ts$/,
    /^\.\.\/\.\.\/dist\/out-tsc\/((?:[^/]*(?:\/|$))*)$/
  ]
} +0ms
  vite-tsconfig-paths config loaded: {
  configPath: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/apps/deepkit/tsconfig.json',
  config: {
    extends: '../../tsconfig.base.json',
    include: [ 'src/**/*.ts', 'src/**/*.tsx' ],
    references: [ { path: './tsconfig.lib.json' } ],
    reflection: true,
    compileOnSave: false,
    compilerOptions: {
      rootDir: '../..',
      sourceMap: true,
      declaration: false,
      moduleResolution: 'node',
      emitDecoratorMetadata: true,
      experimentalDecorators: true,
      importHelpers: true,
      target: 'ES2022',
      module: 'ES2022',
      lib: [ 'esnext', 'dom' ],
      skipLibCheck: true,
      skipDefaultLibCheck: true,
      baseUrl: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs',
      paths: {
        '@lionelhorn/deepkit': [ 'apps/deepkit/src/index.ts' ],
        '@lionelhorn/utils': [ 'packages/utils/src/index.ts' ]
      }
    },
    exclude: [ '../../node_modules', '../../tmp' ]
  }
} +1ms
  vite-tsconfig-paths compiled globs: {
  includers: [
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/,
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.tsx$/
  ],
  excluders: [
    /^\.\.\/\.\.\/node_modules\/((?:[^/]*(?:\/|$))*)$/,
    /^\.\.\/\.\.\/tmp\/((?:[^/]*(?:\/|$))*)$/
  ]
} +0ms
  vite-tsconfig-paths config loaded: {
  configPath: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/packages/utils/tsconfig.lib.json',
  config: {
    extends: './tsconfig.json',
    compilerOptions: {
      outDir: '../../dist/out-tsc',
      declaration: true,
      types: [ 'node' ],
      rootDir: '../..',
      sourceMap: true,
      moduleResolution: 'node',
      emitDecoratorMetadata: true,
      experimentalDecorators: true,
      importHelpers: true,
      target: 'ES2022',
      module: 'ES2022',
      lib: [ 'esnext', 'dom' ],
      skipLibCheck: true,
      skipDefaultLibCheck: true,
      baseUrl: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs',
      paths: {
        '@lionelhorn/deepkit': [ 'apps/deepkit/src/index.ts' ],
        '@lionelhorn/utils': [ 'packages/utils/src/index.ts' ]
      }
    },
    exclude: [ 'vite.config.ts', 'src/**/*.spec.ts', 'src/**/*.test.ts' ],
    include: [ 'src/**/*.ts' ],
    files: [],
    compileOnSave: false
  }
} +0ms
  vite-tsconfig-paths compiled globs: {
  includers: [ /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.ts$/ ],
  excluders: [
    /^\.\/vite\.config\.ts\/((?:[^/]*(?:\/|$))*)$/,
    /^\.\/vite\.config\.ts$/,
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.spec\.ts$/,
    /^\.\/src\/((?:[^/]*(?:\/|$))*)([^/]*)\.test\.ts$/,
    /^\.\.\/\.\.\/dist\/out-tsc\/((?:[^/]*(?:\/|$))*)$/
  ]
} +1ms
  vite-tsconfig-paths config loaded: {
  configPath: 'C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/packages/utils/tsconfig.json',
  config: {
    extends: '../../tsconfig.base.json',
    files: [],  

[...]
2024-03-02T15:08:09.457Z [LOG] HTTP listening at http://0.0.0.0:8080
2024-03-02T15:08:09.457Z [LOG] Debugger enabled at http://0.0.0.0:8080/_debug/
2024-03-02T15:08:09.457Z [LOG] Server started.

Not working with 4.3.1

https://github.com/lionelhorn/deepkit-nx-nextjs/tree/vite-tsconfig-paths-4.3.1

> nx run deepkit:dev

vite.config.ts development
C:\PROG\_experiments\_deepkit\deepkit-nx-nextjs\apps\deepkit
  vite-tsconfig-paths options.root   == C:\PROG\_experiments\_deepkit\deepkit-nx-nextjs +0ms
  vite-tsconfig-paths project root   == C:/PROG/_experiments/_deepkit/deepkit-nx-nextjs/apps/deepkit +1ms
  vite-tsconfig-paths workspace root == undefined +0ms
  vite-tsconfig-paths projects: [
  'C:\\PROG\\_experiments\\_deepkit\\deepkit-nx-nextjs\\apps\\deepkit\\tsconfig.json',
  'C:\\PROG\\_experiments\\_deepkit\\deepkit-nx-nextjs\\apps\\nextjs\\tsconfig.json',
  'C:\\PROG\\_experiments\\_deepkit\\deepkit-nx-nextjs\\packages\\utils\\tsconfig.json'
] +5ms

Setting parseNative to true

tsconfigPaths({ parseNative: true }),

Adding that option makes the program run again but doc says it can slow down start up quite a bit.

    /**
     * Enable use of `tsconfck.parseNative` function, which delegates the
     * loading of `tsconfig.json` files to the TypeScript compiler. You'll
     * probably never need this, but I added it just in case.
     *
     * ⚠️ This option can slow down Vite's startup time by as much as
     * 600ms, due to the size of the TypeScript compiler. Only use it when
     * necessary.
     */