parcel-bundler / parcel

The zero configuration build tool for the web. πŸ“¦πŸš€
https://parceljs.org
MIT License
43.5k stars 2.27k forks source link

Error: Got unexpected undefined #8375

Open chin2km opened 2 years ago

chin2km commented 2 years ago

πŸ› bug report

Trying to migrate a very large repository (private to a company) from webpack to parcel, I'm facing this frequent build error Error: Got unexpected undefined when running parcel watch and the error log doesn't say much more than that. After adding some manual error logging at the source in parcel core in node module I have some more info. Its getting thrown when running svg,jpg,png files through the compressors in this function in @pracel/core.

We loved parcel in our tests and plan to use parcel in dev mode as of now (with plan to use in production later), but this error blocks us. Tried several fixes suggestion from various issues in the repo already but none seems to fix this particular issue.

Any suggestions/help to get around this is highly appreciated.

example complete error

 console: >> errorrrr {
   filePath: 'packages/application-desktop/dist/assets/apple-app-store-badge_nl.8fed9113.svg',
   err: Error: Got unexpected undefined
       at nullthrows (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/nullthrows/nullthrows.js:7:15)
       at Object.run (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/requests/DevDepRequest.js:133:54)
       at RequestTracker.runRequest (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/RequestTracker.js:725:34)
       at Object.runRequest (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/RequestTracker.js:777:23)
       at runDevDepRequest (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/requests/DevDepRequest.js:127:13)
       at runCompressor (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/requests/WriteBundleRequest.js:240:47)
       at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
       at async Promise.all (index 0)
       at async writeFiles (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/requests/WriteBundleRequest.js:207:3)
       at async Object.run (/Users/chintu.mohandas/Documents/Company/frontend-web/node_modules/@parcel/core/lib/requests/WriteBundleRequest.js:179:3) {
     framesToPop: 1
   },
   compressors: [
     {
       plugin: [Object],
       version: '2.7.0',
       resolveFrom: 'node_modules/@parcel/config-default/index.json',
       range: undefined,
       name: '@parcel/compressor-raw',
       keyPath: '/compressors/*/0'
     }
   ]
 }

The error happens when the svg,jpg,png files are run through @parcel/compressor-raw. Is there a way to skip using any compressor at all during dev mode?

.parcelrc

{
  "extends": [
    "@parcel/config-default"
  ],

  "resolveFrom": ".",
}

πŸŽ› Configuration (.babelrc, package.json, cli command)

package.json

"scripts": {
    "client:dev": "parcel watch --target=client",
    "server:dev": "parcel watch --target=server",
    "develop:beta": "concurrently 'yarn client:dev' 'yarn server:dev'"
  },
  "targets": {
    "client": {
      "context": "browser",
      "source": "./src/index.html",
      "distDir": "./dist/assets",
      "publicUrl": "/assets/"
    },
    "server": {
      "context": "node",
      "outputFormat": "esmodule",
      "source": "./src/server-entry.tsx",
      "distDir": "./dist/server",
      "publicUrl": "/assets/",
      "includeNodeModules": false,
      "sourceMap": true
    }
  },

πŸ€” Expected Behavior

No errors in final compression stage

😯 Current Behavior

Described above

πŸ’ Possible Solution

A Way to skip compressors for file globs?

πŸ”¦ Context

We are trying a build a very large server rendered react application with parcel.

πŸ’» Code Sample

Happens in a private repo.

🌍 Your Environment

Software Version(s)
Parcel 2.7.0
Node 16.14.2 18.7.0
npm/Yarn yarn 1.22.19
Operating System macOS Mac M1 Pro
sznowicki commented 2 years ago

It seems it's happening only on ARM. We're debugging the same problem over here. Same codebase, works on Intel Mac, doesn't work on M1 Mac.

sznowicki commented 2 years ago

Our debugging so far showed that it's somewhat related to how parcel uses cache.

We have a massive file that exports around 1000 react components. When we comment all exports out, it works in a dev mode. Then if we uncomment group by group it still works. After uncommenting around 300 exports, it still works, then we stop the dev process, rm -rf .parcel-cache and again parcel throws that error OP posted.

It feels like there's some sort of overflow somewhere. Weird it's only on ARM though.

chin2km commented 2 years ago

@sznowicki I can confirm most of that. This seems to happen only on M1 Mac. And ours is also a huge mono-repo with tons of JS and static assets file.

I also thought its mostly related to cache, but I run into the same issue even with --no-cache flag.

Already tried things like setting PARCEL_WORKER_BACKEND=process and PARCEL_WORKERS and various combination of .parcelrc but none seems to fix these issues.

sznowicki commented 2 years ago

From my observation "no-cache" makes it even worse. I suspect there's some weird memory problem. By "related to cache" I meant that if more is cached then problem disappears.

sznowicki commented 2 years ago

Now I got the same errors on my Intel Mac.

What changed is only we updated a dependency which holds all our corporate icons. The file count grew from ~1200 files to > 2400.

It worked until I changed the version of this dependency from 1.0.0-dev.1 to 1.0.0. From now on I get those unexpected undefined too.

Eventually I got it working:

  1. start dev -> fail
  2. start again -> fail
  3. start again -> works
  4. remove .parcel-cache -> start again -> still works

ARM Mac fails consistently. It really feels like some odd race condition somewhere.

chin2km commented 2 years ago
  1. start dev -> fail
  2. start again -> fail
  3. start again -> works
  4. remove .parcel-cache -> start again -> still works

same for me. But if you remove the dist folder in addition to .parcel-cache the fails starts over. Overall in my tries I only had 10-20% success-rate when starting dev mode.

ARM Mac fails consistently

Same.

andrewvmail commented 2 years ago

I have a project that ran fine on an M1 mac 16GB and get this error on an M1 mac 8GB...

sznowicki commented 2 years ago

We did some additional tests and it really seems like if you try enough times it eventually builds up enough cache to not expose this (probable) memory issue and start the dev process (with PARCEL_WORKER_BACKEND=process).

We just tried it on Intel and ARM Macs. After 5 - 6 times of starting parcel it finally took off.

MSpiechowicz commented 2 years ago

Within one project with a lot of files using this flag PARCEL_WORKER_BACKEND=process helps, but in the other which does not have this amount of files it does not help and the same issue appears.

I would say that this is a major bug related to the M1 architecture or some memory leak/optimization.

eMerzh commented 2 years ago

hye, just shiming in, but i'm trying to upgrade some outdated repo to current version of parcel, and in the process i stumble upon this too...

i'm on mac intel, so it's not just arm....

also suspect memory/file count/size related... not much else to suggest atm ... still digging

HcroakerDev commented 1 year ago

Still getting this issue on parcel v2.8.0. Any idea on how to fix?

mischnic commented 1 year ago

Please share a reproduction if you have a situation where this happens consistently. That would help with getting this fixed

gorakong commented 1 year ago

possible fix: https://github.com/parcel-bundler/parcel/pull/6644

chin2km commented 1 year ago

possible fix: #6644

looking forward to this fix πŸŽ‰

ppqn commented 1 year ago

For me, it's the two for loops starting here:

https://github.com/parcel-bundler/parcel/blob/v2/packages/core/core/src/requests/DevDepRequest.js#L187

image

the devDepRequest object sometimes doesn't have invalidateOnFileChange and/or invalidateOnFileCreate and thus it throws that undefined error.

I just put an if block around each for those loops making sure the desired value exist and my code compiles fine.

arty-name commented 1 year ago

@mischnic Here’s a reproduction case for the error on the public repository. The issue only started appearing with the upgrade to 2.9.0 though.

calebsakala commented 1 year ago

I got (and FIXED) an extremely similar error ("Parcel build error got unexpected null") when I accidentally added ES6 syntax to a JavaScript file that had CommonJS syntax elsewhere. I added some ES6 "export" statements, when elsewhere in the file, my code was using a require(). I found which file was problematic by relocating all the files I suspected to a faraway folder, and then retrying the build. If the build worked, then I knew that all the remaining files were okay.

I added in the suspect files one by one, building after each add. When the build threw an error, I examined the newly added file and commented out all of its code - and then I built again (just to see if the build would work). I slowly uncommented the code until an error was triggered. By this process, I narrowed it down to two HTML files -> two modules -> two imports -> one import -> one file -> a couple of lines within the file and once I realised that I was mixing ES6 with commonJS, I fixed it and it worked just fine again. There isn't really a lot about this error online but I hope that if somebody sees this, it'll help them fix their error too.

lukeg90 commented 1 year ago

Still getting this error on 2.10.0

arty-name commented 7 months ago

Still getting this error on v2.12.0

calebsakala commented 7 months ago

I got this error when I mixed commonJS imports and ES6 syntax in one of the files in my projects last year. Maybe just check that you're not making the same mistake as me. I created a duplicate of my project and then ran the duplicate (reproducing the error), then I moved parts of my project to my Desktop folder by folder until there was no "unexpected undefined" error.

Then, I knew that the "corrupted" files were in the folders I removed; so I put the folders back one by one until the unexpected undefined error occurred again. That helped me identify which folder was corrupted. Then, inside of the "criminal" folder, I moved the files to my Desktop one by one until the unexpected undefined error no longer occurred; then I knew that the corrupted file was amongst the ones I removed, so I put them back in one by one until I got the error again.

When I got the error again, I had found the messed up file, and I scanned it for any irregularities. I had mixed features specific to CommonJS and features specific to ES6 in the file. Once I fixed those to be ONE of the two but not both, my files bundled correctly.

On Wed, 10 Apr 2024 at 15:03, Tom Adler @.***> wrote:

Still getting this error on v2.12.0 https://github.com/BTE-Trusted-Entity/didsign.io/pull/154

β€” Reply to this email directly, view it on GitHub https://github.com/parcel-bundler/parcel/issues/8375#issuecomment-2047365356, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZ4PSPTVE3YLDPAI6FKCHGTY4UTALAVCNFSM554FXQ62U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBUG4ZTMNJTGU3A . You are receiving this because you commented.Message ID: @.***>

arty-name commented 7 months ago

While it could help in some codebases, it’s not the case in 3 of mine projects, so a cleanup is not a solution :(

mcurros2 commented 3 weeks ago

Just adding my two cents here.

In my case the problem was hit in a library that re-exports folders. Ie. ComponentGroup/component/componentItems.

If some component in components items imports everything from /ComponentGroup you may hit this. Maybe, because at some point it can't determine the correct order for dependencies.

Fixed that importing each dependency from their correct folder: from ComponentGroup, ComponentGroup/component instead everything from ComponentGroup.