ojkelly/yarn.build

`yarn bundle` appears to hang when used with `node_modules`

Closed this issue · 6 comments

Describe the bug
A clear and concise description of what the bug is.

I run yarn bundle from inside one of my workspace packages and nothing happens. The terminal just hangs forever.
I am using node_modules. Does this only work with plug and play?

To Reproduce

I made a sample repository here
To reproduce

clone repo
yarn install
yarn build
cd packages/lambda-gq-resolver
yarn bundle

Expected behavior
That the package is bundled as demonstrated here

Screenshots

Desktop (please complete the following information):

  • OS: macOS

I see now after looking at your example that bundling works for pnp only.
Maybe you could help me to understand the risks in moving to pnp a bit.
Could I run into issues where a package I want to use is not supported and I have to move back to node_modules

Yep this is intended to work with pnp, though it's possible it could work with node_modules.

Running your example repo it does stall without explaining why.

We should add some inline information there, either to let us know what step it got to or why it can't proceed.

It'd also be good to check if the project is using pnp and error out if not.

--

The simplest way to see if pnp has any risks to your project is to checkout a branch from your mainline, and switch the project over to pnp and see if anything breaks.

This project is intended to meet the bar set by Bazel. Fast and sound. Soundness is paramount to reproducible builds. But it's additional bookkeeping like say typescript.

So you'll find times where a package hasn't declared all of its imports in it's dependencies. In these cases yarn pnp will throw a fatal error at runtime, requiring you to track the dependencies in .yarnrc.yml as described here https://yarnpkg.com/configuration/yarnrc#packageExtensions.

That seems like a fuss, but it's important as it means you can build and run your code without requiring internet access. This is great when you want to guarantee what you built and tested is what's being deployed, or when you want control and safety around your dependencies. They wont change unless you want them too.

Additionally, pnp vendors your deps in zip files in your repo. This is key for the bundle command to be useful. As it essentially zips up a condensed version of your project with just what your workspace needs to run.

Pnp haș been around for a couple of years now, so most of the big projects work well with it. Things generally don't when they want to mess with the file system inside their package (not good for soundness anyway), or when they have complicated post install scripts (also not great for soundness).

I'm using pnp in production in a few places and its generally good. It does sometimes need attention for the reasons mentioned above.

Hi @ojkelly

Thanks a lot for your reply.

I made my sample repo (brach yarn-berry) simpler still (removing workspaces for now) and started trying to migrate to pnp

After running yarn dlx @yarnpkg/doctor I get some errors. I am really struggling with packageExtensions. I have tried to add some in .yarnrc.yml but I have not managed to make any of these errors go away.
Actually my project builds, bundles and runs fine but I'm really worried about these errors. I don't understand the risks the cause or how to fix them. And I know I really need to understand this well to be able to use pnp in the future.
Screenshot 2021-11-09 at 1 08 10 AM

Apart from these issues I am facing with pnp your bundling is working great, super fast and small package sizes. I really hope I can use this tool, thanks a lot for your hard work.

It's probably worth asking in #support-modern in the yarn discord about those errors.

Most likely, you just need to add some of them to your package.json. Possibly the packageExtensions will need to be defined differently depending which packages have the issue. Maybe also try adding them as dependencies?

Hey @ojkelly thanks for your help I fixed it.
Basically I need if the package complaining is a dependency then you need to add the peer dependency as a dependency to package.json too, thats it.
I didn't need to use packageExtensions at all so I am still not sure what I would use them for, maybe that will come clear later.

Brilliant!

packageExtensions is for when an external/npm package doesn't declare it's dependencies properly, and you need to adjust them. As far as I know what you add under that is essentially the same as if the package maintainer added it to package.json themselves.