WSL中使用NPM和yarn

从Windows 10 1709版本开始,WSL(Windows Subsystem for Linux)已经成为自带的功能,具体安装方式可以参考https://juejin.im/entry/59ed4...

对于习惯使用Linux运行程序,但是开发时候喜欢windows的程序员来讲,WSL是最好的选择了。奈何WSL有很多bug,经常会出现一些奇怪的事情,百思不得其解的时候发现问题还是在WSL中,例如,在1709版本中使用npm或者yarn安装包依赖时,会出现各种问题:
例如在yarn中,出现了一下的问题:

$ yarn
[1/4] Resolving packages...
error An unexpected error occurred: "EINVAL: invalid argument, lstat '/mnt/c/Users/duybui/code/web/old/node_modules/accord/node_modules/cliui/README.md'".
info If you think this is a bug, please open a bug report with the information provided in "/mnt/c/Users/duybui/code/web/old/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

从上面的错误日志中可以发现一个EINVAL: invalid argument, lstat 错误,这个错误是什么意思?
首先我们查看一下lstat命令是干嘛用的:

These functions return information about a file. No permissions are required on the file itself, but-in the case of stat() and lstat() - execute (search) permission is required on all of the directories in path that lead to the file.

from: https://linux.die.net/man/2/lstat

从上面的描述来看,这是一个文件系统的错误,获取文件信息的时候出错了。

为什么会出现文件系统级别的错误???????????

微软的程序员出来解释说:

Hi all. I've been pinged several times over the last 48 hours for an update on the fix for this issue.

Know that we appreciate and understand the enthusiasm for the fix for this issue, but we do ask for your patience as the fix makes its way up through our build and test systems, on its way to the Insider rings.

As @SvenGroot stated above, the fix has been checked in but it didn't make it up to the Insider release branch in time for 17025 which was released on 11/1. It's v. likely it'll arrive in the next Insider build.

Sven is also working to backport this fix to Fall Creators Update (FCU), but since this isn't a fix for a major security exploit, it'll make its way through our normal servicing channels, passing through numerous review and testing gates before it's formally released in an up-coming servicing release.

Please note...

讲了一大堆,其实就是说:“我们已经知道这个错误了,但是短期内不会马上修复,请关注我们的进度。”
真是令人失望,连问题的根本原因也没有告诉我们。

不过热心的网友还是提供了一个奇妙的方法:
~/.bashrc中增加如下代码:

# Workaround for issue: https://github.com/Microsoft/WSL/issues/2448
if ! mount | grep -q "C: on /mnt/c type drvfs (rw,noatime,fallback=1)"; then
        echo "== Remount of C: drive required =="
        pushd ~ > /dev/null
        sudo umount /mnt/c
        sudo mount -t drvfs -o noatime,fallback=1 C: /mnt/c
        popd > /dev/null
fi

从上面的代码来看,就是umount了C盘,然后重新Mount上去,只是增加了一些参数:
首先是drvfs微软的一篇博客对它进行了比较详细的讲解:https://blogs.msdn.microsoft....

-o noatime是提高文件系统性能的一个参数,禁止记录最近一次访问时间戳,至于fallback=1就不太理解了。

验证过上面的方法确实有效,不过最终解决问题还是要等微软更新版本。

相关推荐