Parcel: The cache is driving me insane!

Created on 27 Jul 2018  路  5Comments  路  Source: parcel-bundler/parcel

馃悰 bug report

Bundles built with parcel keep doing really weird stuff, until rm -rf .cache which fixes everything.

馃帥 Configuration (.babelrc, package.json, cli command)

.babelrc

{
  "presets": ["react-app"],
  "plugins": ["babel-plugin-transform-dirname-filename"]
}

package.json

{
  "scripts": {
    "dev": "node build --watch",
    "build": "node build"
  },
  "dependencies": {},
  "devDependencies": {
    "babel-plugin-transform-dirname-filename": "^1.1.0",
    "babel-preset-react-app": "^3.1.2",
    "parcel-bundler": "^1.9.4"
  }
}

build.js

const Bundler = require('parcel-bundler')
const path = require('path')

const entry = path.join(__dirname, './src/index.js')

const watch = process.argv.slice(2).includes('--watch')
const options = {
  outDir: '../salamander-apollo/generated',
  outFile: 'spot',
  watch: watch,
  minify: false,
  target: 'node',
  sourceMaps: true
}

const TextAsset = require.resolve('./build-TextAsset')
const bundler = new Bundler(entry, options)
bundler.addAssetType('.sh', TextAsset)
bundler.bundle()

build-TextAsset.js

Because I couldn't get fs.readFileSync() to work as documented

const {Asset} = require('parcel-bundler')

class TextAsset extends Asset {
  constructor(name, options) {
    super(name, options)
    this.type = 'js'
  }

  parse(content) {
    return content
  }

  generate() {
    return `module.exports = \`${this.ast}\``
  }
}

module.exports = TextAsset

馃 Example 1

This function was getting called with an empty object at startup and I couldn't find the call-site (new Error().stack just had unhelpful regenerator-runtime stuff and search didn't turn up anything). I removed module.exports = terminateInstance to make sure the function wasn't accessible, but it was still getting called and the package continued to build without errors. I removed .cache and the behaviour stopped.

modules/instance/terminateInstance.js

const {ec2} = require('/service')
const createTrace = require('/misc/createTrace')

const terminateInstance = async ({instanceId, primaryStorageVolume}) => {
  let traceStop
  const trace = createTrace({
    module: 'spot.instance.terminateInstance',
    values: {instanceId, primaryStorageVolume}
  })

  traceStop = trace({type: 'terminate-instance'})
  await ec2.terminateInstances({InstanceIds: [instanceId]})
  traceStop()

  traceStop = trace({type: 'delete-volume'})
  await ec2.deleteVolume({VolumeId: primaryStorageVolume})
  traceStop()

  return true
}

馃 Example 2

While investigating "Example 1" I tried modifying the index.js file which imports it. When commenting out these lines I started getting an altogether different error at startup: the absolute import, template, inside of createInstance was undefined. Undoing the comment fixed the issue. Removing .cache fixed everything altogether.

modules/instance/index.js

const createInstance = require('./createInstance')
const describeInstances = require('./describeInstances')
const terminateInstance = require('./terminateInstance')

module.exports = {
  // createInstance,
  // describeInstances,
  // terminateInstance
}

modules/instance/startInstance

const template = require('/misc/template')

/* ... */

const configureInstance = template(require('./configureInstance.sh'))

/* ... */

const createInstance = async ({
  onDemand,
  userId,
  instanceType,
  primaryStorageSize,
  primaryStorageSnapshot,
  userPublicKey,
  salamanderPrivateKey
}) => {
  /* ... */
}

module.exports = createInstance

馃拋 Possible Solution

I have a hunch some of the module numbers are getting mixed up in the final build, with some being reused accidentally. Perhaps that's connected? This stuff seems mostly investigatory and is annoyingly hard to reproduce on a consistent basis.

It'd be fantastic if parcel was reliable.

馃敠 Context

It seems like the cache gets "corrupted" somehow every couple days or so.

We adopted parcel a week ago to convert an independently deployed service into an importable library, this simplifies our server infrastructure.

馃捇 Code Sample

Hmm... can't really share any more code than I have already because of confidentiality.

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.9.4
| Node | 10.1.0
| npm/Yarn | 6.1.0
| Operating System | Ubuntu 16.04 LTS

Bug Waiting

Most helpful comment

@DeMoorJasper This has been fine since upgrading to the latest version of Parcel, thanks!

All 5 comments

Update to latest parcel version, bundle ids have been updated to cause less issues.
Cache has come a long way and will continue to improve, feel free to create minimal reproductions if you find any more bugs or contribute possible fixes.

Sidenote, if it really just doesn't seem to play nicely with your code use --no-cache

Thanks for the guidance, I've upgraded and will see how it goes for a few days & write back if anything turns left. :slightly_smiling_face:

@DeMoorJasper This has been fine since upgrading to the latest version of Parcel, thanks!

Just so you know, we will switch to webpack after MVP.

It was a huge mistake to use parcel.

@yumindeckard whatever floats your titanic.

No need to be hostile against a project though...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Niggler picture Niggler  路  3Comments

mnn picture mnn  路  3Comments

oliger picture oliger  路  3Comments

termhn picture termhn  路  3Comments

algebraic-brain picture algebraic-brain  路  3Comments