Skip to content

Update bootstrap command (fixes #11, #176) - #237

Merged
hzoo merged 15 commits into
lerna:masterfrom
awakesecurity:enhanced-bootstrap
Sep 8, 2016
Merged

hzoo merged 15 commits into
lerna:masterfrom
awakesecurity:enhanced-bootstrap

Conversation

@rygine

@rygine rygine commented Jun 21, 2016 •

Copy link
Copy Markdown
Contributor
  • Symlink packages to each package's node_modules folder
  • Symlink all binaries to packages that depend on them
  • Minor refactor of bootstrap prepublish step
  • Update FileSystemUtilities.symlink to allow type
  • Move hasMatchingDependency and hasDependencyInstalled to Package
  • Removed linkedFiles property of Repository
  • Refactor bootstrap unit tests
  • Add unit tests for .hasMatchingDependency and .runScript methods of Package

@rygine

rygine commented Jun 21, 2016

Copy link
Copy Markdown
Contributor Author

this is a complete refactor of the bootstrap command. i haven't tested on Windows, but it seems like it would work with admin access. not sure the junction type is appropriate when linking directories. would love to get some feedback from Windows developers.

my company is using this right now, and it's working great for us (we're on Mac).

i'm sure there's optimizations that can be done. please take a look at the code and let me know what you think.

Comment thread src/commands/BootstrapCommand.js Outdated
Object.keys(dependencies)
.forEach(dest => {
const destLocation = dest === "__ROOT__" ?
this.repository.rootPath :

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be safer to put into this.repository.packagesLocation. It's possible that the repo root has a dependency on a package from within the repo, which would conflict with the symlink. If the __ROOT__ deps went into packages/ then they would have a clean directory with no potential for conflict.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, without a package.json in the /packages folder, NPM won't install anything there. using the --prefix and --global flags doesn't help much either as it creates /lib and /etc folders inside /packages.

@gigabo

gigabo commented Jun 23, 2016

Copy link
Copy Markdown
Contributor

This is awesome. It's a big change. It helps with the deep-require problem (#11), but we've got at least one module in react-server that doesn't play well with it somehow (still investigating). I wonder how common that is...

@gigabo

gigabo commented Jun 23, 2016

Copy link
Copy Markdown
Contributor

This renders lerna exec npm outdated useless. Npm reports all as MISSING. 😬

@jamiebuilds

jamiebuilds commented Jun 23, 2016 •

Copy link
Copy Markdown
Contributor

This worries me, it's completely changing how packages interact with node_modules in a very breaking way.

@rygine

rygine commented Jun 24, 2016

Copy link
Copy Markdown
Contributor Author

i could take out the "optimization" part and install dependencies in each package's folder like before, then symlink the packages to packages/node_modules. perhaps that would make more sense.

@rygine

rygine commented Jun 24, 2016

Copy link
Copy Markdown
Contributor Author

@gigabo interesting that NPM doesn't traverse directories like Node to look for dependencies. i guess it would make the command a bit more complex than it needs to be. but i think it would be helpful in determining installed versions that same way as Node resolves dependencies.

@rygine

rygine commented Jun 28, 2016

Copy link
Copy Markdown
Contributor Author

i'm thinking that maybe we could symlink the external dependencies to each package's node_modules folder, which i think would fix the npm outdated issue.

@rygine
rygine force-pushed the enhanced-bootstrap branch 2 times, most recently from 6ea3607 to 6513b96 Compare June 28, 2016 23:43
@giuseppeg

giuseppeg commented Jul 2, 2016 •

Copy link
Copy Markdown

This may be the worst idea ever – I am a npm noob – and may not work for everybody but...
What if one extracts the shared dependencies and promotes them to root dependencies and then adds the root's node_modules/.bin to the PATH env variable?
E.g.

// packages/pkg1/package.json
"dependencies": {
  "lodash": "^4.0.0",
  "babel": "6.0.0"
}
// packages/pkg2/package.json
"dependencies": {
  "lodash": "^4.0.0",
  "babel": "5.0.0"
}

becomes

// package.json
"dependencies": {
  "lodash": "^4.0.0"
}
// packages/pkg1/package.json
"dependencies": {
  "babel": "6.0.0"
}
// packages/pkg2/package.json
"dependencies": {
  "babel": "5.0.0"
}

@sveisvei

sveisvei commented Jul 5, 2016 •

Copy link
Copy Markdown

Im super-keen on getting support for linked binaries(#192), as well as support for more than one entry/main file via symlinks.

What is the blocker on this issue atm?

@rygine

rygine commented Jul 5, 2016 •

Copy link
Copy Markdown
Contributor Author

@sveisvei this PR needs a refactor, which i'm hoping to sort out this week. PR #192 handles binary linking and is ready to go.

@doug-wade

doug-wade commented Jul 5, 2016 •

Copy link
Copy Markdown
Contributor

@giuseppeg I think that would work, but there is one rub: binaries on PATH take precedence over locally-installed dependencies, meaning that if you promote a dep to the root, then you can't override it for a local package. What we do internally at Redfin is specify the path to the monorepo root in package.json, which isn't pretty, but it works, c.f.

{
  "scripts": {
    "test": "../../ava/cli.js tests.js"
  }
}

which means that if we want to upgrade some packages to a new version of ava, we can do so by using their locally-installed version. I'm not sure how strong of an argument that is against what you're proposing, but it is, at least, something to consider.

@giuseppeg

giuseppeg commented Jul 7, 2016 •

Copy link
Copy Markdown

@doug-wade are you sure about that?

~/project/packages/my-pkg$ PATH=$PATH:../../node_modules/.bin npm run my-script

works for me i.e. it uses the local bin.
Npm adds ./node_modules/.bin, I append ../../node_modules/.bin and if I remember correctly $PATH is read left to right

@gigabo gigabo mentioned this pull request Aug 12, 2016
@gigabo

gigabo commented Aug 12, 2016

Copy link
Copy Markdown
Contributor

Hi @rygine - Looks like this has diverged from master a little. Please rebase if you get a chance.

@rygine

rygine commented Aug 12, 2016

Copy link
Copy Markdown
Contributor Author

@gigabo i'll work on this over the weekend. i have a few adjustments i'd like to make as well so that npm outdated works.

@rygine
rygine force-pushed the enhanced-bootstrap branch from 6513b96 to 003caed Compare August 15, 2016 21:15
@rygine

rygine commented Aug 15, 2016 •

Copy link
Copy Markdown
Contributor Author

@gigabo having issues with paths in windows. what am i missing? i thought path.join would handle things properly, but it seems like it's using \\ as the path separator and expecting /.

@gigabo

gigabo commented Aug 15, 2016

Copy link
Copy Markdown
Contributor

Weird. Looks like the test expectation is testDir, which comes from _initFixture. It's using path.resolve() to generate. Maybe that produces different output from path.join on windows?

@rygine
rygine force-pushed the enhanced-bootstrap branch from 003caed to 01236f8 Compare August 15, 2016 21:41
@rygine

rygine commented Aug 15, 2016

Copy link
Copy Markdown
Contributor Author

path.resolve should produce the same thing as path.join. i don't have access to a windows machine to test on. not sure how to proceed. 😕

@hzoo

hzoo commented Aug 15, 2016

Copy link
Copy Markdown
Contributor

Maybe you can look at #236 for reference, not sure either

@rygine

rygine commented Aug 15, 2016 •

Copy link
Copy Markdown
Contributor Author

i could potentially use normalize-path, but that seems hackish to me.

@laurentdesmet thoughts?

edit: normalize-path didn't seem to work. installing a windows VM for further testing. 😖
2nd edit: getting a windows environment up and running isn't a simple task. i don't have much time to explore this problem locally. i'll just rely on someone else to understand this issue.

@gigabo

gigabo commented Sep 6, 2016

Copy link
Copy Markdown
Contributor

Okay, looks like the issues raised by @seansfkelley and @bradleyayers have been addressed.

So... 👍 (again).

@hzoo, @thejameskyle This is a big one. Solves a lot of problems, but probably introduces new issues we haven't thought of yet. On balance seems worth moving forward. Ready?

@rygine rygine mentioned this pull request Sep 7, 2016
1 of 2 tasks
@hzoo
hzoo merged commit 143840e into lerna:master Sep 8, 2016
@hzoo

hzoo commented Sep 8, 2016

Copy link
Copy Markdown
Contributor

@rygine Thanks for your patience and work on this one! Hopefully we don't have to use the proxy method anymore

@laurentdesmet

Copy link
Copy Markdown
Contributor

First of all, nice work @rygine (and others that helped with this one), having support for deep-require within Lerna is awesome :-). At work we make heavily use of Lerna and lately we're experiencing performance problems (lerna bootstrap and webpack builds running slow) because we have a lot of packages with recurring/common packages (we're building a react/redux based application and this means every package depends on react, redux, redux-observable, etc.).

I saw that this PR mentioned at some point an optimization where common dependencies (or all dependencies) of each package were moved to the root of the repo. What is the exact reason of not including this optimization? Because it would definitely be helpful for big Lerna projects (actually now, we're applying this strategy by removing commonly used dependencies from all the package.jsons dependencies before lerna bootstrap and adding them back afterwards). I'm aware of the problem with commands in the .bin folder, and #245, but are there other issues beside these that I'm missing?

gigabo added a commit to gigabo/react-server that referenced this pull request Sep 8, 2016
This picks up:

- [Enhanced bootstrap](lerna/lerna#237)
- [Private module support](lerna/lerna#321)

You'll neeed to `npm update` after this lands (and `npm i -g
[email protected]` if you're using a global Lerna).

You'll also want to re-bootstrap to get your repo up-to-date.
@seansfkelley

Copy link
Copy Markdown
Contributor

@laurentdesmet I have a similar problem with Lerna + Webpack. I figured a neat way to solve it might be to have a Webpack resolver plugin that looks at the filesystem ancestors of a particular module when that module is required, checking if any ancestor has a package that fits the desired version range (preferring those closer to the root), and using that one instead. It wouldn't help install times, but it would reduce duplication/bloat in the final distributable. To my knowledge, such a plugin doesn't exist, but I've been mulling over how to make one.

@gigabo

gigabo commented Sep 8, 2016

Copy link
Copy Markdown
Contributor

@laurentdesmet - Thanks for calling that out!

Hoisting common deps to the repo root would definitely be a big improvement for Lerna.

Filed #334 to track.

@rsolomon

rsolomon commented Sep 9, 2016

Copy link
Copy Markdown
Contributor

Thank you SO much for this commit. It resolves a huge slew of issues I was having with Lerna, including:

  • Relative CSS imports between modules
  • Exporting and importing flow types
  • Reducing boilerplate exports for component library modules

A really great improvement to the usability of Lerna, and upgrading was almost completely seamless. Again, great work.

rygine added a commit to awakesecurity/lerna that referenced this pull request Sep 12, 2016
* Update bootstrap command (fixes lerna#11, lerna#176)

* Find common external dependencies and install to root node_modules folder
* Symlink all packages to root node_modules folder after installing external dependencies
* Symlink all binaries to packages that depend on them
* Minor refactor of bootstrap prepublish step
* Update `FileSystemUtilities.symlink` to allow type
* Move hasMatchingDependency and hasDependencyInstalled to Package
* Removed `linkedFiles` property of Repository
* Refactor bootstrap unit tests
* Add unit tests for `.hasMatchingDependency` and `.runScript` methods of Package

* Revert back to installing dependencies in each package's directory

* Move package symlinks to `packages/node_modules` to prevent NPM issues in the repo root
* Only symlink packages that are dependents

* Symlink packages to each package's node_modules directory

This reverts symlinking all packages to `packages/node_modules`.

* Only symlink packages defined in dependencies

* Update README

* Add better checks for already installed dependencies

* Fix logic around existing dependency checks

* Add better logic, more progress. Fix test.

* Unsync unlink

This change was out of scope of the PR.

* Update symlink check logic

* Use the same path variable for the installed dependency location and symlink destination
* Always use the built-in `nodeModulesLocation` on the package
* Check for `false` value instead of falsy value
* Add more clarity when `package.json` of a dependency is missing

* Support scoped package dependencies

* Get package location from Package object

* Use the package graph to get dependency packages

* Symlink packages with scope to proper directory

* Simplify package dependency destination path logic

Breathe deep... exhale
@AlastairTaft

AlastairTaft commented Sep 23, 2016 •

Copy link
Copy Markdown

UPDATE: My comment below is solved by rygine's comment.

Just ran into this, I loved lerna specifically because it didn't use symlinks. Symlinks make developing with webpack really hard, it'll start giving you annoying errors like Module not found: Error: Cannot resolve module 'babel' because the path of the package is now a level up from your project, this is caused by node's default behaviour of require using the physical path for symlinks which I think is wrong, see the issue here. Should keep the symlinked path.

I guess I'm using lerna as an edge case as I like to use it as my main way of developing peer dependent packages to avoid the side effects of the above node 'require' issue.

So my main gripe is node's require which I don't think they will change, so I wanted a way to develop without using npm link. Node have a cli option to --preserve-symlinks which helps alleviate the problem, I opened an issue to turn this on permanently. Would be nice having a dev setup that doesn't require loads of custom configuration to get just right though.

@bradleyayers

Copy link
Copy Markdown

Could you provide a bit more information about the structure of your project? It's surprising to me that you are having issues with babel.

@AlastairTaft

Copy link
Copy Markdown

So I have my website located in root/packages/luna. The webpack config inside this folder has a babel loader that looks like this

loaders: [
      {
        test: /\.jsx?$/, 
        loader: 'babel',
        exclude: /node_modules/,
      }
      // ...

Now I have another package as a dependency to luna in the folder root/packages/cms-controls. Webpack is now scanning this cms-controls folder because that exclude doesn't pick it up anymore because it's symlinked. I could avoid the problem by changing the exclude to exclude: /(node_modules|cms-controls)/ but that's not a nice solution.

@bradleyayers

Copy link
Copy Markdown

It's curious to me that you wouldn't want cms-controls to be loaded via babel. In my project all packages use roughly the same conventions and are expected to be loaded in a similar way.

I'm guessing cms-controls has a different webpack loader configuration, and luna's is not adequate to be able to load modules from cms-controls?

@AlastairTaft

AlastairTaft commented Sep 23, 2016 •

Copy link
Copy Markdown

All my third party packages are pre compiled with babel. So they have a src folder but then when I do npm publish everything gets babel'd into a lib folder (when developing npm start sets up a watch to do this). So anything consuming my package doesn't require babel it's just ES5.

@AlastairTaft

AlastairTaft commented Sep 23, 2016 •

Copy link
Copy Markdown

I think even if I wanted it to be consumed by babel it wouldn't work because the error says it can't find babel now, because it's using the physical path not the symlinked path. So I don't think the problem is with how I've setup my package. I think? Could be wrong

@bradleyayers

bradleyayers commented Sep 23, 2016 •

Copy link
Copy Markdown

The way our repo is setup, is for all packages in /packages to be loaded by each other using the same webpack config for development, and then published to a dist/ directory separately for consuming via ES5. We make use of different package.json mains for this. You can use webpack to point src/index.js (ES6), and main to point to lib/index.js.

@rygine

rygine commented Sep 23, 2016

Copy link
Copy Markdown
Contributor Author

@AlastairTaft you may need to add a resolver to your webpack config.

this is what i have in my webpack config:

resolve: {
  root: path.join(process.cwd(), 'node_modules'),
},
resolveLoader: {
  root: path.join(process.cwd(), 'node_modules'),
},

not sure if this is your issue, but this helped me.

@AlastairTaft

AlastairTaft commented Sep 25, 2016 •

Copy link
Copy Markdown

@bradleyayers Assuming the dist contents for each package are generated with webpack, how would you handle peer dependencies, say you used React, would you ignore it from the bundle? That also got me wondering say if componentA and componentB both depended on packageA, you'd end up with packageA in both component bundles? So you'd inflate your final bundle size?

I just use babel for third party components so it doesn't bundle the dependencies. I guess you could do that same with webpack by telling it to ignore bundling everything in the node_modules folder.

@rygine Thanks for that, that seemed to work.

@bradleyayers

Copy link
Copy Markdown

@AlastairTaft we have a few webpack outputs. In one of them we don't bundle any dependencies , instead we just declare dependencies in package.json, mark all of those as external in our webpack config, and let the consumer download those and bundle them however they wish.

We do produce other bundles that do include dependencies, and for those common dependencies are indeed duplicated.

@seansfkelley

seansfkelley commented Oct 5, 2016 •

Copy link
Copy Markdown
Contributor

@rygine when you were experiencing trouble in Webstorm, did you file a ticket on their tracker about it? I'm running into issues with Webstorm as well (https://youtrack.jetbrains.com/issue/WEB-23488#comment=27-1638599) and got myself into a bad state that's not clear if it's caused by circular symlinks or too many node_modules.

@rygine

rygine commented Oct 5, 2016 •

Copy link
Copy Markdown
Contributor Author

@seansfkelley i didn't file a ticket as there were already a few related ones. i've added node_modules to my ignored files and folders and everything seems to be fine.

@seansfkelley

Copy link
Copy Markdown
Contributor

I did that too, but I use Typescript and that causes it's own host of problems. Time to switch to VS Code!

@lock

lock Bot commented Dec 27, 2018

Copy link
Copy Markdown

This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock Bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.