parcel-bundler / parcel

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

Package self import broken with `source` field #5604

Open mischnic opened 3 years ago

mischnic commented 3 years ago

🐛 bug report

When importing from a different package, the package.json#source field is applied. But if the imported package then imports itself again (by importing the folder containing package.json, main is used instead of source.

Probably a regression caused by https://github.com/parcel-bundler/parcel/pull/5508

🤔 Expected Behavior

Compiles successfully.

😯 Current Behavior

🚨 Build failed.
@parcel/core: Failed to resolve '../' from './b/src/index.js'
proejct/b/src/index.js:3:22
  2 |
> 3 | export {a as b} from "../";
>   |                      ^^^^^
  4 | // export * from "./other.js";
  5 |
unknown: Could not load './lib/index.js' from module 'b' found in package.json#main
proejct/b/package.json:5:11
  4 |   "source": "src/index.js",
> 5 |   "main": "lib/index.js"
>   |           ^^^^^^^^^^^^^^ './lib/index.js' does not exist, did you mean './src/index.js'?'
  6 | }
  7 |

💻 Code Sample

monorepo where this package is imported:

// packages/b/package.json
{
  "name": "b",
  "version": "0.0.0",
  "source": "src/index.js",
  "main": "lib/index.js"
}

// packages/b/src/index.js
export const a = 1;
export { a as b } from "../";

https://github.com/mischnic/parcel-issue-5604

🌍 Your Environment

Software Version(s)
Parcel 6551001c2ed3a42125fefda5881399f85c501f12
aladdin-add commented 3 years ago

hit the issue when using immer - it has pkg.source.

this causes a build failing, as __DEV__ was used in immer/src while parcel did not have the var.

baronnoraz commented 3 years ago

I'm not sure if this is related, but I'm working with Antd 4. One of the dependencies that gets pulled in is compute-scroll-into-view@1.0.16

The package.json has the following...

{
"main": "dist/index.js",
"module": "dist/index.module.js",
"source": "src/index.ts"
}

However if I look at package structure, only the dist directory exists. So I get the error...

@parcel/resolver-default: Could not load './src/index.ts' from module 'compute-scroll-into-view' found in package.json#source

This is where my ignorance shows. I don't know if the package was published wrong, the resolution order is wrong ("source" shouldn't be evaluated before "main"), or if the resolver should fallback and try a different property if say "source" isn't found.

It's probably a package publishing problem, but I wanted to share in case there is an issue. If I remove the "source" entry, everything compiles fine.

mischnic commented 3 years ago

@baronnoraz -> https://github.com/parcel-bundler/parcel/issues/5784