diff --git a/devtools/debug_tips.md b/devtools/debug_tips.md index 324ddd00df3..272486b4761 100644 --- a/devtools/debug_tips.md +++ b/devtools/debug_tips.md @@ -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`. diff --git a/devtools/flake.lock b/devtools/flake.lock index 11b30cd5d98..eee0bba6c29 100644 --- a/devtools/flake.lock +++ b/devtools/flake.lock @@ -21,11 +21,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", "type": "github" }, "original": { @@ -101,8 +101,8 @@ "rust-overlay": "rust-overlay" }, "locked": { - "lastModified": 1722362288, - "narHash": "sha256-tjwYO8AH2Skwhynm/fwOTodN/EqLgAbvKzhE/xxQ2r8=", + "lastModified": 1727455099, + "narHash": "sha256-/tqXI2VIsx+OmXxYAUqOnzfQ2JqWBJJ32IjwzGMxg0A=", "path": "/home/username/gitrepos/roc", "type": "path" }, @@ -175,4 +175,4 @@ }, "root": "root", "version": 7 -} +} \ No newline at end of file diff --git a/devtools/flake.nix b/devtools/flake.nix index 228feadd6fa..992d07592e4 100755 --- a/devtools/flake.nix +++ b/devtools/flake.nix @@ -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 diff --git a/flake.lock b/flake.lock index 511c9adf186..dbaac0648a6 100644 --- a/flake.lock +++ b/flake.lock @@ -21,11 +21,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1710146030, - "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "lastModified": 1726560853, + "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", "owner": "numtide", "repo": "flake-utils", - "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", "type": "github" }, "original": { @@ -44,11 +44,11 @@ ] }, "locked": { - "lastModified": 1710868679, - "narHash": "sha256-V1o2bCZdeYKP/0zgVp4EN0KUjMItAMk6J7SvCXUI5IU=", + "lastModified": 1713543440, + "narHash": "sha256-lnzZQYG0+EXl/6NkGpyIz+FEOc/DSEG57AP1VsdeNrM=", "owner": "guibou", "repo": "nixGL", - "rev": "d709a8abcde5b01db76ca794280745a43c8662be", + "rev": "310f8e49a149e4c9ea52f1adf70cdc768ec53f8a", "type": "github" }, "original": { @@ -84,19 +84,16 @@ }, "rust-overlay": { "inputs": { - "flake-utils": [ - "flake-utils" - ], "nixpkgs": [ "nixpkgs" ] }, "locked": { - "lastModified": 1713150335, - "narHash": "sha256-Ic7zCPfiSYc9nFFp+E44WFk3TBJ99J/uPZ4QXX+uPPw=", + "lastModified": 1727490462, + "narHash": "sha256-OrrPiNBiikv9BR464XTT75FzOq7tKAvMbMi7YOKVIeg=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "b186d85e747e2b7bee220ec95839fb66c868dc47", + "rev": "11a13e50debafae4ae802f1d6b8585101516dd93", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 8487f2bd822..2858d641617 100644 --- a/flake.nix +++ b/flake.nix @@ -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 diff --git a/nix/default.nix b/nix/default.nix index e54aa8a2acf..4ecc76165d3 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -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;