Skip to content

Commit

Permalink
How to view code coverage
Browse files Browse the repository at this point in the history
Commits:
* Code coverage

* flake lock update

* update nix rev

* flake update

* llvm-cov does not work on macos

* back to old rev
  • Loading branch information
Anton-4 authored Sep 28, 2024
1 parent f572457 commit 587d0af
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
20 changes: 20 additions & 0 deletions devtools/debug_tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,23 @@ Note that the addresses shown in objdump may use a different offset compared to

gdb scripting is very useful, [for example](https://roc.zulipchat.com/#narrow/stream/395097-compiler-development/topic/gdb.20script/near/424422545).
ChatGPT and Claude are good at writing those scripts as well.

## Code Coverage

When investigating a bug, it can be nice to instantly see if a line of rust code was executed during for example `roc build yourFile.roc`. We can use [`cargo-llvm-cov`](https://github.com/taiki-e/cargo-llvm-cov) for this, on linux, it comes pre-installed with our flake.nix. On macos you'll need to install it with `cargo +stable install cargo-llvm-cov --locked`.

To generate the code coverage file:

```shell
$ cd roc
$ source <(cargo llvm-cov show-env --export-prefix)
$ cargo llvm-cov clean --workspace
$ cargo build --bin roc
# Replace with the command you want to generate coverage for:
$ ./target/debug/roc build ./examples/platform-switching/rocLovesRust.roc
# To view in editor
$ cargo llvm-cov report --lcov --output-path lcov.info
# To view in browser
$ cargo llvm-cov report --html
```
Viewing lcov.info will depend on your editor. For vscode, you can use the [coverage gutters](https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters) extension. After installing, click `Watch` in the bottom bar and go to a file for which you want to see the coverage, for example `crates/compiler/build/src/link.rs`. `Watch` in the bottom bar will now be replaced with `x% Coverage`.
12 changes: 6 additions & 6 deletions devtools/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion devtools/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
vscodeWithExtensions = pkgs.vscode-with-extensions.override {
vscodeExtensions = with pkgs.vscode-extensions;
[
matklad.rust-analyzer
rust-lang.rust-analyzer
# eamodio.gitlens
bbenoist.nix
tamasfe.even-better-toml
Expand Down
21 changes: 9 additions & 12 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
xorg.libXrandr
xorg.libXi
xorg.libxcb
cargo-llvm-cov # to visualize code coverage
];

# DevInputs are not necessary to build roc as a user
Expand Down
4 changes: 3 additions & 1 deletion nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ let
inherit rustPlatform;
compile-deps = callPackage ./compile-deps.nix { };
rust-shell =
(rustVersion.override { extensions = [ "rust-src" "rust-analyzer" ]; });
# llvm-tools-preview for code coverage with cargo-llvm-cov
(rustVersion.override { extensions = [ "rust-src" "rust-analyzer" "llvm-tools-preview"]; });


# contains all rust crates in workspace.members of Cargo.toml
roc-full = (callPackage ./builder.nix { }).roc-release;
Expand Down

0 comments on commit 587d0af

Please sign in to comment.