Home > Article > Web Front-end > Record and analyze a yarn bug that has existed for 6 years
I recently encountered a yarn bug. After searching, I found that it existed for 6 years. What kind of magical problem is this? After some analysis and investigation, I gave 6 solutions. . .
The package manager of several projects I have taken over recently is yarn@v1.22.19
. After installing the dependencies, no matter what Whether it is successful or not, there are always network connection problems and it will be stuck for a long time, and then there will be a few lines of abnormal logs like this: info There appears to be trouble with your network connection. Retrying...
.
Sometimes some magical packages (such as node-sass
) have exceptions and cause the installation to fail. As a result, the failure is discovered after being stuck for a long time, which is really frustrating. In addition, there are dozens of related issue
in the github
warehouse of yarn
, with a time span of 6 years from 2016 to 2022. There are different opinions on the reasons and solutions. I was very curious about what kind of magical problem this was that had not been solved for 6 years, so I decided to find out. [Recommended related tutorials: nodejs video tutorial, Programming video]
When you encounter problems and errors that don’t have any ideas, The first tip is to search for it. Search the github
warehouse of yarn
for error informationThere appears to be trouble with your network connection
, you can see that there are 1 related code and 91 in the results Related issue
. I searched for a while in issue
but couldn't find a suitable solution, so I went to the next step: search the code.
Due to network reasons, go directly to the local yarn
installation directory to search. Use vscode
to open the installation directory of yarn
(my local directory is ~/.volta/tools/image/yarn/1.22.19
), and search globally for the key WordThere appears to be trouble with your network connection
. You can see that there is also only one result, and the entire error message is assigned to a variable offlineRetrying
.
Global search keywordsofflineRetrying
There are 2 results in total, except for the results in the previous step, there is only 1 reference. The code here mainly throws exceptions and retries, and there are no more keywords to dig out. Next, enter the debugging process, put a breakpoint in front of the offlineRetrying
line of code, and debug to see the specific error message and context.
You need to run the command to install dependenciesyarn
, So how to debug it? yarn
is a npm
package. When executed, it actually calls node xxx.js
, and this xxx.js
is generally configured in ## In the bin
field of #package.json. As you can see from the picture below, the corresponding file for yarn is
./bin/yarn.js, so you can use this line of debugging command:
node --inspect-brk ~/ .volta/tools/image/yarn/1.22.19/bin/yarn.js. (For debugging of
Node.js, please refer to
Official Documentation)
statement before the code line cli.js:66099
where the variable offlineRetrying
is located.
Then go back to the root directory of the business project and run the debugging command node --inspect-brk ~/.volta/tools/image/yarn/1.22.19/bin/yarn.js
. At this time, the program hangs waiting for the debugging tool to connect, and prints out the following log:
Then open the chrome
built-in debugging pagechrome://inspect/#devices
, find Target
with the same file path, and click the inspect
button to start debugging.
Then chrome
will open an independent DevTools
window, because node --inspect-brk is used
command, at this time DevTools
the automatic breakpoint is at the beginning of the file being debugged, you need to press F8
to skip the breakpoint and continue execution.
After waiting for a short period of time, DevTools
stops at the previously added breakpoint. You can see that this is a timeout exception, and the request that caused the exception is GET: https://yarnpkg.com/latest-version
. Using curl
to request this link results in a 210s timeout. Accessing this link using a proxy can succeed, but the request is redirected to classic.yarnpkg.com/latest-vers…, and the return result is 1.22.19
.
The problem is now basically clear. The main reason is that the request timed out and multiple retries caused the problem at the beginning of the article. You can use a proxy to avoid this problem. It would be boring if the investigation ended here.
In order to further understand yarn
why we need to requestyarnpkg.com/latest-vers…, searched the code using this link as a keyword, and found this keyword chain: https://yarnpkg.com/latest-version
-> SELF_UPDATE_VERSION_URL
-> _checkUpdate
-> checkUpdate
, the actual calling relationship is just the opposite, as shown below:
It has been inferred that the calling relationship of the timeout link is: checkUpdate
-> _checkUpdate
-> SELF_UPDATE_VERSION_URL
-> https://yarnpkg.com/latest-version
, combined with the comments and checkUpdate
function Judging from the code, every time the yarn
installation command is executed, yarnpkg.com/latest-vers… will be requested to check whether there is a new version that needs to be updated. However, this link access times out and will be retried after failure. The default timeout is 30s and the number of retries is 4, so after the installation is completed, it will still be stuck for 120s before the program actually ends.
There are three key factors that cause problems: checking for updates, timeout, and retrying. Therefore, you can optimize the network, adjust the timeout, and jump There are 3 directions to solve the problem by checking and updating. There are 6 solutions below for reference.
It is easy to think of this idea. Since the access times out, then increase the request speed.
$ yarn install --proxy "http://{domain}:{port}" --https-proxy "http://{domain}:{port}"
Taking my development environment as an example, the command looks like this:
yarn install --proxy "http://10.180.55.191:7890" --https-proxy "http://10.180.55.191:7890"
This idea is relatively straightforward and applicable to more scenarios. If other methods don’t work, you can try it.
The default timeout is 30s and can be changed to 2s. After modification, the exception still exists, but it can be Checking for updates fails quickly without waiting for several minutes.
yarn install --network-timeout 2000
Some developers said that the exception occurred because some large
npm
packages took too long to install and exceeded the default timeout of 30s, so the network-timeout can also be changed. Avoid exceptions.
The solution to this idea mainly comes from several termination conditions in the checkUpdate
function.
$ yarn config set disable-self-update-check true$ yarn install
$ yarn config set lastUpdateCheck 1e13 $ yarn install
$ yarn install --non-interactive
yarn
的安装目录注释 checkUpdate
的调用,具体代码行为 cli.js:7261
,修改后长这样:// this.checkUpdate();
checkUpdate
函数的代码...以上主要是分享一些问题分析排查的经验,另外也提供了一些 yarn install
超时异常的解决方案,希望能对前端同学们有所帮助。
在快写完这篇文章的时候,yarnpkg.com/latest-vers… 已经可以正常访问,不知道还会不会有人再遇到这个问题。
另外我在
yarn
的github issue
中回复了以上的解决方案,希望前端同学们少受点折磨,也希望官方早点修复这个6年陈的老Bug。?
更多编程相关知识,请访问:编程教学!!
The above is the detailed content of Record and analyze a yarn bug that has existed for 6 years. For more information, please follow other related articles on the PHP Chinese website!