parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.37k stars 2.27k forks source link

[@parcel/transformer-typescript-types]: Meaningless "Got unexpected undefined" error message #7669

Open jakubmazanec opened 2 years ago

jakubmazanec commented 2 years ago

I encountered "Got unexpected undefined" error. I don't know what exactly causes it, but I created repo with minimal reproduction.

🎛 Configuration

package.json

{
  "name": "parcel-bug",
  "main": "dist/index.js",
  "source": "src/index.ts",
  "types": "dist/index.d.ts",
  "scripts": {
    "build": "parcel build"
  },
  "dependencies": {
    "next": "^12.0.10"
  },
  "devDependencies": {
    "@parcel/packager-ts": "^2.2.1",
    "@parcel/transformer-typescript-types": "^2.2.1",
    "parcel": "^2.2.1",
    "typescript": "^4.5.5"
  }
}

tsconfig.json

{
  "extends": "./tsconfig.options.json",
  "compilerOptions": {
    "composite": true,
    "esModuleInterop": true,
    "exactOptionalPropertyTypes": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "lib": ["DOM", "DOM.Iterable", "ES2021"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitOverride": true,
    "noImplicitReturns": true,
    "noUncheckedIndexedAccess": true,
    "rootDir": "./src",
    "strict": true,
    "target": "ES2021",
  },
  "include": ["src/**/*"],
  "references": []
}

Source code:

import type { NextWebVitalsMetric } from 'next/app';

declare module 'next/app' {
  export interface AppInitialProps {
    pageProps: unknown;
  }
}

export async function reportWebVitals({ value, name }: NextWebVitalsMetric) {}

🤔 Expected Behavior

Parcel builds with no error? I think this minimal reproduction shouldn't actually build (because of e.g. missing React), but the errors should be more descriptive and useful.

😯 Current Behavior

Parcel errors:

× Build failed.

@parcel/transformer-typescript-types: Got unexpected undefined

  Error: Got unexpected undefined
  at nullthrows (D:\projects\parcel-test\node_modules\nullthrows\nullthrows.js:7:15)
  at TSModuleGraph.propagate
  (D:\projects\parcel-test\node_modules\@parcel\transformer-typescript-types\lib\TSModuleGraph.js:271:48)
  at shake (D:\projects\parcel-test\node_modules\@parcel\transformer-typescript-types\lib\shake.js:42:35)
  at D:\projects\parcel-test\node_modules\@parcel\transformer-typescript-types\lib\TSTypesTransformer.js:139:33
  at transformation (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107043:24)
  at transformRoot (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107070:82)
  at Object.transformNodes (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107054:78)
  at emitDeclarationFileOrBundle (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107768:43)
  at emitSourceFileOrBundle (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107671:13)
  at forEachEmittedFile (D:\projects\parcel-test\node_modules\typescript\lib\typescript.js:107397:30)

💻 Code Sample

Repo with reproduction. Clone and run npm install and npm run build.

🌍 Your Environment

Software Version(s)
Parcel 2.2.1
Node 16.13.2
npm 8.3.1
Operating System Windows 10 Enterprise 20H2
zapplebee commented 2 years ago

https://github.com/parcel-bundler/parcel/blob/e294eafd9a49c056fb4b223c5ace5c0653428ede/packages/transformers/typescript-types/src/shake.js#L39

With the debugger, it looks like here is the node remains undefined.

rathpc commented 2 years ago

I also ran into this issue recently although slightly different

@parcel/transformer-typescript-types: Cannot read properties of undefined (reading 'unshift')
  TypeError: Cannot read properties of undefined (reading 'unshift')

the modifiers on the node in my case was undefined and I have found that this is only introduced when you have a typescript version >= 4.6.0.

@jakubmazanec I notice you have "typescript": "^4.5.5" can you take a look in your yarn.lock/package-lock.json and see what version is actually in your project? I suspect if you explicitly set a version like "typescript": "4.5.5" your problem might go away like mine did.

This does however not explain the problem since that is a minor version bump of TS and I would have expected a library depending on that to not be affected. If it is, then I think this lib needs a strict version associated to prevent this issue until the code causing the error is updated with the newer version of TS.

jakubmazanec commented 2 years ago

@rathpc I reported this 2 months ago, when TypeScript 4.6 wasn't released yet. But I'll try anyway.

jakubmazanec commented 2 years ago

I installed and run the repo with TypeScript 4.6.3 and also with TypeScript 4.5.5, same error.

Best if somebody took a look at that PR...

rathpc commented 2 years ago

I installed and run the repo with TypeScript 4.6.3 and also with TypeScript 4.5.5, same error.

Best if somebody took a look at that PR...

Ah ok, thanks for testing my theory at least!

gradddev commented 2 years ago

I got the same error. Don't know is it the same bug or not, but I figured out the following:

  1. Let's import some type from some npm package:

    // index.ts
    import { FC } from "react";
  2. Use this type and export:

    // index.ts
    export type MyComponent = FC<{}>;
  3. Create a file with the same name of the module in the first step:

    // react.ts
    export default "";
  4. Try to import something from the file that created in the third step:

    
    // index.ts
    import { FC } from "react";

// @parcel/transformer-typescript-types: Got unexpected undefined import {} from "./react";

export type MyComponent = FC<{}>;



It also works with other modules. React is just an example. 

Versions:
- `parcel`: 2.5.0
- `@parcel/transformer-typescript-types`: 2.5.0
- `@parcel/packager-ts`: 2.5.0
- `typescript`: 4.6.3
- `node`: 17.8.0

Repo with reproduction: https://github.com/alexgraddev/parcel2-typescript-types-bug
danieltroger commented 2 years ago

Would also appreciate a fix for this!

Swizec commented 2 years ago

I ran into this problem and figured out what the error means. So at least I can fix it in user-land code, but a more helpful message would be nice.

It happens when you get a squiggly line on a type import.

CleanShot 2022-08-25 at 15 42 34@2x
Swizec commented 2 years ago

Also looks like there's something with index re-exports

this doesn't work:

// src/index.ts
import { customTheme } from './themes'

// src/themes/index.ts
export * from './customTheme'

// src/themes/customTheme.ts
export const customTheme = ...

this works:

// src/index.ts
import { customTheme } from './themes/customTheme'

// src/themes/index.ts
export * from './customTheme'

// src/themes/customTheme.ts
export const customTheme = ...
ivanbanov commented 2 years ago

I had the same problem that was mentioned here https://github.com/parcel-bundler/parcel/issues/7669#issuecomment-1106567699, and found another issue when renaming it.

Name Works
react
_react
react_

It seems like leading _ is not welcome 🤔

ted-palmer commented 1 year ago

I'm having the same problem. Here is a reproducible repo. Would love if this could get resolved, or if anyone has any solutions. Thanks!

danieltroger commented 1 year ago

I'm still seeing this every now and then, don't think it has been resolved

jakubmazanec commented 11 months ago

I updated all dependencies in the repo with the bug reproduction; the bug still exists.

jakubmazanec commented 5 months ago

I updated Parcel and TypeScript in the repo with the bug reproduction; the bug still exists.