parcel-bundler / parcel

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

If a target name contains a slash, parcel fails with a hard-to-diagnose error message #9001

Open danieltroger opened 1 year ago

danieltroger commented 1 year ago

A valid target would look like this in package.json:

    "server_commonjs": {
      "isLibrary": true,
      "source": "src/index.ts",
      "optimize": false,
      "context": "node",
      "outputFormat": "commonjs",
      "distDir": "dist/server/commonjs"
    }

When an accident is made using string replace and it looks like this instead:

    "/dist/server_commonjs": {
      "isLibrary": true,
      "source": "src/index.ts",
      "optimize": false,
      "context": "node",
      "outputFormat": "commonjs",
      "distDir": "dist/server/commonjs"
    }

Parcel fails with the quite unhelpful error message:

Error: Cannot use 'in' operator to search for 'key' in undefined

  TypeError: Cannot use 'in' operator to search for 'key' in undefined
      at getJSONSourceLocation (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/unplugged/@parcel-diagnostic-npm-2.8.3-2e1d38a83b/node_modules/@parcel/diagnostic/lib/diagnostic.js:178:19)
      at EntryResolver.resolveEntry (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/unplugged/@parcel-core-npm-2.8.3-1751106c31/node_modules/@parcel/core/lib/requests/EntryRequest.js:214:64)
      at async Object.run (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/unplugged/@parcel-core-npm-2.8.3-1751106c31/node_modules/@parcel/core/lib/requests/EntryRequest.js:75:16)
      at async RequestTracker.runRequest (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/unplugged/@parcel-core-npm-2.8.3-1751106c31/node_modules/@parcel/core/lib/RequestTracker.js:756:20)
      at async AssetGraphBuilder.runEntryRequest (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/unplugged/@parcel-core-npm-2.8.3-1751106c31/node_modules/@parcel/core/lib/requests/AssetGraphRequest.js:816:18)
      at async $b0fd219fea43bcac$export$2e2bcd8739ae039._runFn (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/cache/@parcel-utils-npm-2.8.3-13fd8c7807-69edf3e7c3.zip/node_modules/@parcel/utils/lib/index.js:33579:13)
      at async $b0fd219fea43bcac$export$2e2bcd8739ae039._next (/Users/daniel/Documents/depict/2-depict.ai/browser-tags-v2/.yarn/cache/@parcel-utils-npm-2.8.3-13fd8c7807-69edf3e7c3.zip/node_modules/@parcel/utils/lib/index.js:33572:9)

🚨 Build failed.

Expected behavior: better error message

I figured it out by adding console.log statements to parcel. in EntryRequest in @parcel/core keyPath is built like this

let keyPath = `/targets/${targetName}/source${Array.isArray(target.source) ? `/${i}` : ''}`;

which became very weird due to my accidental added slashes.

Sorry for not filling out the issue template I wanted to be quick, lmk if it's too terrible

danieltroger commented 1 year ago

Still reproducible, download, extract, yarn && yarn parcel build

parcel-repro.zip

mischnic commented 1 year ago

Looks like say there's a encodeJSONKeyComponent missing in the string concatenation you mentioned:

https://github.com/parcel-bundler/parcel/blob/eeb3ba3de6e4fe037cd4f5d73939fa6867faa1f4/packages/core/diagnostic/src/diagnostic.js#L316-L317

sephialaureenciaa commented 3 months ago

Hi, is this done yet? I'd like to contribute to this!

sephialaureenciaa commented 3 months ago

Hi @mischnic, just to clarify, you're saying the above issue (where a target name contains slash) should not give an error and instead we should wrap the targetName with encodeJSONKeyComponent?

sephialaureenciaa commented 3 months ago

I've created a PR for this!