Skip to content

feat(workspaces): Add --ignore-workspace-root-check flag and improve usage#4630

Merged
arcanis merged 1 commit intoyarnpkg:masterfrom
arcanis:yarn-workspace-improvs
Oct 5, 2017
Merged

feat(workspaces): Add --ignore-workspace-root-check flag and improve usage#4630
arcanis merged 1 commit intoyarnpkg:masterfrom
arcanis:yarn-workspace-improvs

Conversation

@arcanis
Copy link
Copy Markdown
Member

@arcanis arcanis commented Oct 4, 2017

Summary

Supercedes #4583 - cc @kaylieEB and @jgoz

  • Implements a --ignore-workspace-root-check option that replaces the --dev requirement when adding a dependency at the root. We received a lot of feedback about this warning, which prompted this change. You can now add both production and development dependencies to your packages, as long as you also use the new flag. Hopefully it will make things easier to understand!

  • Fixes the bug of Fix commands when used in a workspace #4583 where the extraneous files were not removed from the workspaces.

  • Partially fixes outdated, which will now print the whole tree (including the workspace root).

  • Running yarn add in the root won't remove workspaces dependencies from the lockfile anymore.

Details

At first this PR was only about adding the --ignore-workspace-root-check flag, but then I needed to change some parts of the code that #4583 was already altering, so I had to spend some time thinking about it as well, and eventually integrated the relevant changes here.

First, a distinction has to be made:

  • First we have the package.json file format. We have a package.json for each workspace.
  • Then we have the yarn.lock file format. There's only a single lockfile that aggregate all workspaces.

Which now brings me to my point: the following bugs, fixed in #4278 and #4583:

  • "When called at the project root level with workspaces, upgrade and upgrade-interactive should only change the root dev dependencies"
  • "upgrade to workspace root preserves child dependencies"
  • "upgrade to workspace child preserves root dependencies"

They are not bugs.

Now that might sounds weird, but it all makes sense if you think about how we store the resolved dependencies! Our lockfile format looks like this:

"foo@~1.0.0"
    version: 1.0.0

Think about a workspace root containing two workspaces, A and B. Both depends on foo@~1.0.0. Now what will happen when we run yarn upgrade foo in A? Should we only update A? But then, what will our lockfile look like? We can't have two entries with the same key, like this:

"foo@~1.0.0"
    version: 1.0.0

"foo@~1.0.0"
    version: 1.0.5

And here lies the problem: the lockfile format doesn't (yet) allow us to operate on part of the workspace tree. We always must deal with the whole workspace tree, because if we don't we'll end up corrupting the lockfile by "forgetting" entries (like what happened when we were just not reading the workspaces package.json files), or overriding them accidentally (like what would happen if we were to just override foo@~1.0.0: B would be surprisingly affected as well by this change because it happens to share the same pattern range).

Test plan

Added some tests from #4583, fixed a few others, everything should be green.

@arcanis arcanis requested review from BYK and kaylie-alexa October 4, 2017 13:20
@arcanis arcanis assigned arcanis and unassigned arcanis Oct 4, 2017
@arcanis arcanis force-pushed the yarn-workspace-improvs branch from 7647e38 to 8ba4966 Compare October 4, 2017 13:44
@buildsize
Copy link
Copy Markdown

buildsize bot commented Oct 4, 2017

This change will decrease the build size from 9.91 MB to 9.9 MB, a decrease of 2.96 KB (0%)

File name Previous Size New Size Change
yarn-[version].noarch.rpm 856.9 KB 856.41 KB -497 bytes (0%)
yarn-[version].js 3.77 MB 3.77 MB -905 bytes (0%)
yarn-legacy-[version].js 3.82 MB 3.82 MB -879 bytes (0%)
yarn-v[version].tar.gz 862.77 KB 862.33 KB -455 bytes (0%)
yarn_[version]all.deb 652.13 KB 651.85 KB -292 bytes (0%)

1 similar comment
@buildsize
Copy link
Copy Markdown

buildsize bot commented Oct 4, 2017

This change will decrease the build size from 9.91 MB to 9.9 MB, a decrease of 2.96 KB (0%)

File name Previous Size New Size Change
yarn-[version].noarch.rpm 856.9 KB 856.41 KB -497 bytes (0%)
yarn-[version].js 3.77 MB 3.77 MB -905 bytes (0%)
yarn-legacy-[version].js 3.82 MB 3.82 MB -879 bytes (0%)
yarn-v[version].tar.gz 862.77 KB 862.33 KB -455 bytes (0%)
yarn_[version]all.deb 652.13 KB 651.85 KB -292 bytes (0%)

Copy link
Copy Markdown
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

LGTM. Great job with the PR explanation.


export function setFlags(commander: Object) {
commander.usage('add [packages ...] [flags]');
commander.option('-W, --ignore-workspace-root-check', 'required to run yarn add inside a workspace root');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Interesting short-hand flag name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Didn't think long about this one, but you make me remember it conflicts with -W(arning) of compilers 😞 oh well, should be fine for this iteration.

if (file[0] === '@') {
const scopedPaths: Set<string> = new Set();

const findExtraneousFiles = async basePath => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It may be better in the future to extract this method outside of the closure.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Since it needs to changes scopedPaths and extraneousFiles and isn't meant to be reused anywhere else in the code I thought it was cleaner to keep it together with the code segment that uses it.

@BYK BYK changed the title Improves the workspaces feat(workspaces): Add --ignore-workspace-root-check flag and improve the DX Oct 4, 2017
@BYK BYK changed the title feat(workspaces): Add --ignore-workspace-root-check flag and improve the DX feat(workspaces): Add --ignore-workspace-root-check flag and improve usage Oct 4, 2017
Copy link
Copy Markdown
Contributor

@jgoz jgoz left a comment

Choose a reason for hiding this comment

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

@arcanis Your explanation makes sense and I agree that outdated and upgrade should operate across all workspaces, including the root.

However, I just tested your branch and ran outdated and upgrade-interactive at the workspace root for my test repository, and both commands only showed outdated root packages only, even though there are outdated packages in the workspaces. Did you intend to change that behaviour in this PR too?

Copy link
Copy Markdown
Contributor

@jgoz jgoz left a comment

Choose a reason for hiding this comment

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

Also, this doesn't fix the "No lockfile in this directory..." issue when running remove inside of a workspace package (which is a legitimate case for running an individual command in a workspace).

I think you need to include the changes I made in cli.js too.

@arcanis
Copy link
Copy Markdown
Member Author

arcanis commented Oct 5, 2017

@jgoz Yep I missed those fixes :/ Right now I'll have to merge this PR as is (the new yarn add option is important for common use cases), would you mind opening a new PR with the things I missed? I'm sorry, I know it must be time-consuming 😞

@arcanis arcanis merged commit e286034 into yarnpkg:master Oct 5, 2017
@jgoz
Copy link
Copy Markdown
Contributor

jgoz commented Oct 5, 2017

@arcanis No problem! I can open a new PR that will look very similar to #4583 😉

@edmorley
Copy link
Copy Markdown
Contributor

edmorley commented Oct 5, 2017

Thank you both for all your work on this - I can't wait to start using yarn workspaces once the remaining papercuts are fixed 😃

Looking at #4583 which this superseded, I see a few test-cases that weren't migrated to this PR - was this intentional? eg:

@jgoz
Copy link
Copy Markdown
Contributor

jgoz commented Oct 5, 2017

@edmorley I plan to migrate those fixes to a new PR shortly, though outdated will work as @arcanis suggested in the description of this PR (i.e., it will operate across the whole workspace, both root and individual packages).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants