Home  >  Article  >  Web Front-end  >  Record and analyze a yarn bug that has existed for 6 years

Record and analyze a yarn bug that has existed for 6 years

青灯夜游
青灯夜游forward
2022-11-04 19:29:421514browse

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. . .

1. Problem description

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.... Record and analyze a yarn bug that has existed for 6 years

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]

Record and analyze a yarn bug that has existed for 6 years

2. Troubleshooting

2.1. Keyword search

2.1.1. Search github

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. Record and analyze a yarn bug that has existed for 6 years

2.1.2. Search 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. Record and analyze a yarn bug that has existed for 6 years

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. Record and analyze a yarn bug that has existed for 6 years

2.2. Program debugging

2.2.1. Determine the debugging command

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) Record and analyze a yarn bug that has existed for 6 years

##2.2.2. Start debugging

First add the

debugger

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: Record and analyze a yarn bug that has existed for 6 years

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.

Record and analyze a yarn bug that has existed for 6 years

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. Record and analyze a yarn bug that has existed for 6 years

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. Record and analyze a yarn bug that has existed for 6 years

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.

2.2.3. In-depth investigation

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:

Record and analyze a yarn bug that has existed for 6 years

Record and analyze a yarn bug that has existed for 6 years

Record and analyze a yarn bug that has existed for 6 years

3. Determine the reason

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.

4. Solution

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.

4.1. Optimize the network

It is easy to think of this idea. Since the access times out, then increase the request speed.

  • [Option 1] Use a proxy to optimize the network (recommended)
$ 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"

4.2. Adjust the timeout period

This idea is relatively straightforward and applicable to more scenarios. If other methods don’t work, you can try it.

  • [Option 2] Modify network-timeout (depending on the situation)

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.

4.3. Skip check update

The solution to this idea mainly comes from several termination conditions in the checkUpdate function.

  • 【方案3】修改配置禁止检查更新(推荐)
$ yarn config set disable-self-update-check true$ yarn install
  • 【方案4】修改配置把上次更新时间调到百年后(推荐)
$ yarn config set lastUpdateCheck 1e13
$ yarn install
  • 【方案5】执行命令时禁用交互式提示(推荐)
$ yarn install --non-interactive
  • 【方案6】修改代码跳过检查更新(不推荐)
    • 找到 yarn 的安装目录注释 checkUpdate 的调用,具体代码行为 cli.js:7261,修改后长这样:// this.checkUpdate();
    • 也可以修改其他可以阻断 checkUpdate 函数的代码...

5、最后

以上主要是分享一些问题分析排查的经验,另外也提供了一些 yarn install 超时异常的解决方案,希望能对前端同学们有所帮助。

在快写完这篇文章的时候,yarnpkg.com/latest-vers… 已经可以正常访问,不知道还会不会有人再遇到这个问题。

另外我在 yarngithub 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!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete