How to enable race detector for Golang in Visual Studio Code?
Asked Answered
C

2

5

I searched in many web pages to find what phrase exact should I place in settings.json in VS Code Golang extension (released by Microsoft) to add a build flag (in my case, race detector)?

I added:

"go.buildFlags": ["-race"],

in extension's settings.json but still when debugging, in a function that definitely generates a race condition, the debugging console does not denote something like:

Found 1 data race(s)

which the go compiler usually indicates when executing the same file via

go run -race file.go
Cent answered 19/6, 2019 at 13:0 Comment(4)
This has been asked before, but not answered. https://mcmap.net/q/2029416/-how-to-add-race-argument-in-launch-json-file/13860 If we can get an answer here, we can close the other as a duplicate.Binominal
@Flimzy I do not know about launch.json which is being mentioned in your denoted question. Although two questions appear to be similar, my question is focused on VS code microsoft extension for Go, which is configured via settings.json nd not via launch.json. If they are the same, then this question may be considered duplicate (while my version is more detailed and elaborated).Cent
You may be right. I don't use VSCode, so don't know about the differences you're talking about. I mainly left the comment as a note to future readers (possibly myself) to close the old one as a dupe of this if/when you get an answer.Binominal
@Flimzy thanks. I will also search to find whether there has been a relationship between these two files in VS Code historically.Cent
C
10

adding this line in settings.json

"go.testFlags": ["-race"]

works for me

Clementia answered 5/12, 2019 at 1:21 Comment(0)
S
1

Maybe it's a little late. But adding "go.testFlags": ["-race"] in the user of settings.json doesn't work for me. And adding "buildFlags": "-race" in configurations of launch.json with "mode" being "test" works well for me.

Seventh answered 15/2, 2022 at 10:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.