Building Go with coverage instrumentation

I am wondering if its possible to build Go from source with coverage instrumentation such that everytime we execute go run or go build we can check the coverage on the go compiler source code.

Hi @PanickyPeguin,

Would using a Makefile work?

(Edited to add: I mean, instead of recompiling the Go toolchain. You could set up make targets with the -cover flag to build the code with coverage enabled.)

Edit 2: Or do you want to measure coverage of the compiler itself?

This sounds suspiciously like DevOps to me. Take a look at this:

This would push the onus of worrying about coverage to your pull requests and such. Other than that, assuming you’re using bash, maybe you could alias the command in your ~/.bashrc? For example, I aliased my npm command like so:

alias npm="winpty npm.cmd"

So it uses winpty (I have a complicated setup wherein I’m using git bash on Windows and winpty makes colors work with npm).

Hi @christophberger I mean measuring the coverage of the compiler itself

This might be possible with some tweaking. I found this email in the golang-dev mailing list:

Keith Randall, Jun 7, 2016, 2:07:56 AM, to golang-dev

Rob and I got the coverage tool working on the compiler itself (running all.bash on amd64 as the test suite). Enjoy!

There’s a tool to generate these: GitHub - randall77/compilercover: Tool to generate coverage reports on the Go compiler

Be sure to read the top of main.go before you run the tool.

And you’ll need the patch: https://go-review.googlesource.com/c/23831/

This email is from 2016, so YMMV. (It might be worth digging deeper into the golang-dev archives. I only skimmed through a few hits from searching for “coverage compiler”.)

Yes, it is possible to build the Go compiler from source with coverage instrumentation so that you can check the code coverage of the Go compiler source code itself.

To achieve this, you can follow these steps:

1.Clone the Go repository: Start by cloning the Go repository from GitHub using the following command:

git clone GitHub - golang/go: The Go programming language

2.Navigate to the Go source directory: Change your working directory to the cloned Go source code directory:

cd go

3.Enable coverage instrumentation: Enable code coverage instrumentation by setting the GOFLAGS environment variable. Run the following command:

export GOFLAGS=“-cover”

4.Build the Go compiler: Build the Go compiler using the make command. This will compile the Go source code with coverage instrumentation:

make
This process may take some time as it builds the entire Go toolchain.

5.Check coverage: Once the build process is complete, you can check the coverage of the Go compiler source code by running go tool cover command followed by the path to the generated coverage profile file. For example:

go tool cover -html=coverage.out
This will generate an HTML report showing the coverage details of the Go compiler source code.

By following these steps, you can build the Go compiler from source with coverage instrumentation and analyze the code coverage of the Go compiler source code using the generated coverage profile.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.