简体   繁体   English

如何在运行测试之前自动清理 Visual Code 中的 go 缓存?

[英]How do I automatically clean the go cache in Visual Code before running tests?

There is a small nuisance with Visual Code and Go, but is mainly related to the most recent release of go 1.11.x and the newly introduced module system. Visual Code 和 Go 有一个小麻烦,但主要与最近发布的 go 1.11.x 和新引入的模块系统有关。 The go builder creates a folder called 'auto' for cached builds, which can be cleaned / deleted via 'go clean -cache'. go 构建器为缓存构建创建一个名为“auto”的文件夹,可以通过“go clean -cache”清理/删除。

Visual Code seems to be confused when executing tests, if such a directory is present, and complains about missing dependencies:执行测试时,如果存在这样的目录,Visual Code 似乎会感到困惑,并抱怨缺少依赖项:

/home/user/.golang/src/github.com/stretchr/testify/assert/assertions.go:20:2: could not import github.com/pmezard/go-difflib/difflib (open auto/4c/4cb1f0f2c9f328e3cade99998704759897fd4e530af38db25c85cdc253c1f1a0-d: no such file or directory)

is there any possibility to fix this?有没有可能解决这个问题? Or is it just another setting I forgot to make?或者这只是我忘记做的另一个设置?

Warning from issue 26809 ("GOCACHE=off causes modules to exit the tests") : 来自问题26809的警告(“ GOCACHE = off导致模块退出测试”)

The GOCACHE env variable will be phased out out in 1.12, and cannot be used with go modules enabled in 1.11 GOCACHE env变量将在1.12中逐步淘汰,并且不能与在1.11中启用的go modules一起使用

Configuring VSCode to use using go test -count=1 ... is safer. 使用go test -count=1 ...配置VSCode更加安全。

The Go 1.12 release notes (for February 2019) makes that official: Go 1.12发行说明 (2019年2月发布 )使该官员成为:

Build cache requirement 构建缓存要求

The build cache is now required as a step toward eliminating $GOPATH/pkg . 现在需要构建缓存,以消除$GOPATH/pkg
Setting the environment variable GOCACHE=off will cause go commands that write to the cache to fail. 设置环境变量GOCACHE=off将导致写入高速缓存的go命令失败。

尝试设置环境变量GOCACHE=off

In 2022... 2022年...

  1. Open settings.json in Visual Studio Code with either:在 Visual Studio Code 中打开settings.json

    • For Global setting, open Preferences > Settings from the menu bar.对于全局设置,从菜单栏中打开Preferences > Settings

      • Search for go.testFlags .搜索go.testFlags
      • Under Go: Test Flags , click Edit in settings.json .Go: Test Flags下,单击Edit in settings.json
    • For Project-only setting, open/create file at [project_root]/.vscode/settings.json对于仅项目设置,在[project_root]/.vscode/settings.json打开/创建文件

  2. In settings.json, add the following value to the go.testFlags key:在 settings.json 中,将以下值添加到go.testFlags键:

     { "go.testFlags": ["-count=1"] }
  3. Save and enjoy.保存并享受。

Note: these steps ensure test cache will be skipped every time like OP desires.注意:这些步骤确保每次都像 OP 一样跳过测试缓存。 If you instead want a one-time fix, then run go clean -testcache in the terminal.如果您想要一次性修复,请在终端中运行go clean -testcache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM