rushstack icon indicating copy to clipboard operation
rushstack copied to clipboard

[rush] Don't autoretry 'rush update' (which nukes node_modules) when it won't help

Open KRyan opened this issue 8 years ago • 6 comments

Scenario: I update a dependency in package.json but make a typo. We quickly 404 requesting the npm package and fail. However, we try again, but this time:

Trying again (attempt #2)... Deleting the "node_modules" folder Deleting the "pnpm-store" folder ...

For a large monorepo, this is extremely slow, and of course it doesn't resolve the issue.

Can we prevent this auto-retry logic for known error codes? Alternatively, rather than the auto-retry, should we give the user with the option to retry with the purge step?

My team is using rush with pnpm.

KRyan avatar Nov 01 '18 17:11 KRyan

This is a good idea. The main work is figuring out how to interpret the package manager's errors, to determine whether they are retryable or not. Ideally we'd want to determine this based on the process exit code, versus trying to parse the console output. Some package managers also support JSON logging, which might also be an option, however we wouldn't want to give up the interactive console output during installation.

Note that Rush supports three different package managers, which will all have different behavior: PNPM, NPM, and Yarn.

octogonz avatar Nov 01 '18 17:11 octogonz

Why is an automatic retry necessary?

An alternative could be to just print an error message that suggests to run an alternative command, e.g. rush update --purge or something.

poelstra avatar Apr 05 '19 09:04 poelstra

Recap of discussion on Gitter:

@octogonz said:

This retry arose in part because npm install would randomly fail sometimes. Deleting node_modules and redoing the operation would sometimes fix it. We got tired of endless support tickets, so we made the retry automatic. PNPM makes things better. It's cache almost never gets corrupted, and overall it is very good about maintaining consistency. So for PNPM at least, we could definitely make it a lightweight retry that doesn't nuke everything. However I wouldn't eliminate the retry entirely, because there is another calss of issues where installs randomly fail due to network issues, and will work if you restart it. I believe recent PNPM versions now have an internal retry loop that addresses this silently. Yarn will actually print warnings to the console about network trouble. Our best theory is that the problem is actually an ancient unfixed bug in NodeJS itself, or perhaps some very low-level library that is shared by Yarn/PNPM/NPM, because they all seem to encounter this. (NodeJS network speed is also terrible compared to e.g. NuGet, which is further evidence that something's wrong with it.)

So the suggestion is to skip the automatic retry when pnpm is used, and print a message on how to manually do it instead.

poelstra avatar Apr 05 '19 09:04 poelstra

@poelstra I should mention another motivation was to help with CI jobs (e.g. a PR build) that would sometimes fail randomly, but then would work fine if you retried them. People found this frustrating: You'd set up your PR to automerge (an Azure DevOps feature), go to a meeting, and then come back a few hours later to find that your build had failed -- even though nothing was wrong. In a large monorepo a merge conflict could have arisen during this time, which means nontrivial effort was required to restart the job. Adding a retry made the process more robust.

Thinking about it from that angle, perhaps the ideal solution would be an option like rush install --with-retry that allows the retry logic to be applied during a CI build, while avoiding hassling developers during a local build.

@iclanton do you have any recent anecdotal experience around this?

octogonz avatar Apr 08 '19 23:04 octogonz

the ideal solution would be an option like rush install --with-retry

Yeah, that sound like a very good idea!

BTW, when I was playing around with it last week, I also noticed another occasion where it threw away lots of node_modules, which apparently happened after a non-successful run of rush update or something. Not completely sure anymore how I got into that state, but it would be good to try to minimize the amount of 'trash things just to be sure' in this case :) I traced that one back to this logic: https://github.com/Microsoft/web-build-tools/blob/1b494de0fba0272dbcb9f087ff875ae2cd741830/apps/rush-lib/src/logic/InstallManager.ts#L778

That code doesn't look very trivial, so I'm not yet sure I dare to try to change anything there without breaking some other edge cases that you apparently found out the hard way :)

poelstra avatar Apr 10 '19 09:04 poelstra

That code doesn't look very trivial, so I'm not yet sure I dare to try to change anything there without breaking some other edge cases that you apparently found out the hard way :)

@poelstra Consider this scenario:

  1. I create a PR
  2. The CI job starts building my PR branch
  3. While the CI job is in the middle of rush install, I push a change to my branch
  4. The CI job gets forcibly terminated
  5. Go to step 2

For internal build systems, it's common that the filesystem state will be preserved between runs. This allows caches such as the node_modules folder to be reused, which often provides major performance benefits. (In the best case, the second rush install takes only a few seconds.)

But what if forcibly terminating pnpm install somehow leaves the node_modules folder in a corrupted state? Subsequent runs will fail on that machine forever, and some person will have to come along and manually clear the folder.

NPM 4 was completely unreliable in this regard. PNPM is supposed to be extremely robust, but if there is just one little oversight in some obscure logic path somewhere, a busy lab environment will hit it frequently and nobody will trust PNPM.

By contrast, if you study Rush's marker file logic, it's fairly easy to convince yourself that corruption can never occur. But you are right that it is sometimes too paranoid, particularly on a local developer machine. If someone wants to propose an improvement to avoid needless nuking of node_modules, we'll definitely accept it.

octogonz avatar Apr 11 '19 08:04 octogonz