#cargo-build #cargo #neovim #filter #wrapper #dedup #build-time

build cargo-limit

Cargo with less noise: warnings are skipped until errors are fixed, Neovim integration, etc

9 releases

0.0.10 Jan 14, 2023
0.0.9 Oct 14, 2021
0.0.8 Mar 22, 2021
0.0.7 Feb 16, 2021
0.0.2 Sep 14, 2020

#107 in Build Utils

Download history 34/week @ 2024-07-01 21/week @ 2024-07-08 21/week @ 2024-07-15 28/week @ 2024-07-22 28/week @ 2024-07-29 38/week @ 2024-08-05 19/week @ 2024-08-12 23/week @ 2024-08-19 25/week @ 2024-08-26 29/week @ 2024-09-02 37/week @ 2024-09-09 61/week @ 2024-09-16 45/week @ 2024-09-23 30/week @ 2024-09-30 42/week @ 2024-10-07 39/week @ 2024-10-14

170 downloads per month
Used in dawproject-rs

MIT/Apache

76KB
2K SLoC

Rust 1.5K SLoC // 0.0% comments Vim Script 204 SLoC

cargo-limit

Crates.io

🚀 Cargo with less noise:

  • errors have highest priority
    • they never appear in the middle of warnings
    • warnings are skipped by default until errors are fixed
    • external path dependencies' warnings are skipped by default
  • all messages come in reversed order by default
    • to avoid extra scrolling
  • messages are grouped by filenames
  • number of messages can be limited
  • after encountering first error the rest of build time is limited by default
  • files can be automatically opened in your text editor on affected lines

This tool is especially useful in combination with cargo-watch.

Initially this project was just a workaround for this issue.

asciicast

Installation

From crates.io

cargo install cargo-limit

From git

cargo install --force --git https://github.com/alopatindev/cargo-limit

Usage

Run any of these in your project directory:

cargo lbench
cargo lbuild
cargo lcheck
cargo lclippy
cargo ldoc
cargo lfix
cargo lrun
cargo lrustc
cargo lrustdoc
cargo ltest

Also llcheck, llrun, etc.

💡 Environment Variables 👁️

CARGO_MSG_LIMIT

  • limit compiler messages number
  • 0 means no limit, which is default

CARGO_TIME_LIMIT

  • cargo execution time limit in seconds after encountering first compiling error
  • 1 is default
  • 0 means no limit

CARGO_ASC

  • show compiler messages in ascending order
  • false is default

CARGO_FORCE_WARN

  • show warnings even if errors still exist
  • false is default

CARGO_DEPS_WARN

  • show external path dependencies' warnings
  • false is default

CARGO_EDITOR

  • opens affected files in external app
  • empty ("") means don't run external app
  • "_cargo-limit-open-in-nvim" is default

Text Editor/IDE integrations

💡 Neovim Plugin 👁️

Enable the plugin in your init.vim. For instance for vim-plug:

Plug 'alopatindev/cargo-limit', { 'do': 'cargo install cargo-limit nvim-send' }

and install it with

nvim +PlugInstall +UpdateRemotePlugins +qa

Optionally: F2 to save, F2 again to jump to next affected line

function! SaveAllFilesOrOpenNextLocation()
  let l:all_files_are_saved = 1
  for i in getbufinfo({'bufmodified': 1})
    if i.name != ''
      let l:all_files_are_saved = 0
      break
    endif
  endfor

  if l:all_files_are_saved
    call g:CargoLimitOpenNextLocation()
  else
    execute 'wa'
  endif
endfunction

nmap <F2> :call SaveAllFilesOrOpenNextLocation()<cr>
vmap <F2> <esc>:call SaveAllFilesOrOpenNextLocation()<cr>v
imap <F2> <esc>:call SaveAllFilesOrOpenNextLocation()<cr>i
💡 Test the plugin! 👁️

  1. Open two terminals (or tmux windows, etc.)
  2. cd your/project/directory in both of them
  3. Run nvim in one of them
  4. Run cargo lrun in the other
  5. In case of compiling errors nvim opens new or existing tabs with the files on affected lines and columns
  6. Fix the error, save the file and nvim will jump to the next error location
  7. cargo llrun (cargo llcheck, etc.) will open them in case of warnings as well.

⚠️ Known Limitations! 👁️

1. Auto-jumps work only if

  • current mode is normal
  • current buffer is either empty or contains some existing and unmodified (saved) file

This is by design, in order to not disrupt from active text editing or file navigation process.

2. Auto-jump on each file save is currently imprecise

  • it may jump to a wrong line if it moved
  • it may not jump at all, if the next affected line is supposed to be modified already

For precise jump please rerun cargo ll{check,run,etc.}.

3. Before running nvim: Current Directory should be Project (sub)directory

  • that's required so cargo-limit could figure out which exact nvim instance should be controlled
  • only first nvim instance with current project (sub)directory will be controlled by cargo-limit.

Customizations

Add a custom open handler to your init.vim if you want other Neovim behavior.

💡 See examples! 👁️

Open Files in Buffers Instead of Tabs

function! g:CargoLimitOpen(editor_data)
  let l:current_file = resolve(expand('%:p'))
  if l:current_file != '' && !filereadable(l:current_file)
    return
  endif
  for location in reverse(a:editor_data.files)
    let l:path = fnameescape(location.path)
    if mode() == 'n' && &l:modified == 0
      execute 'edit ' . l:path
      call cursor((location.line), (location.column))
    else
      break
    endif
  endfor
endfunction

Populate a QuickFix List

set errorformat =%f:%l:%c:%m

function! g:CargoLimitOpen(editor_data)
  let l:winnr = winnr()

  cgetexpr []
  for file in a:editor_data['files']
    caddexpr file['path'] . ':' . file['line'] . ':' . file['column'] . ':' . file['message']
  endfor

  if empty(a:editor_data['files'])
    cclose
  else
    copen
  endif

  if l:winnr !=# winnr()
    wincmd p
  endif
endfunction

💡 Other Text Editors/IDEs 👁️

cargo-limit can run external app/script and provide affected locations to stdin in the following JSON format:

{
  "workspace_root": "/full/path/to/project",
  "files": [
    {
      "path": "/full/path/to/project/file.rs",
      "line": 4,
      "column": 1,
      "message": "unused import: `diagnostic::DiagnosticSpan`",
      "level": "warning"
    }
  ]
}

Theoretically this can be used for any text editor or IDE, especially if it supports client/server communication. To do that you need a wrapper app/script that parses the files and gives them to the text editor or IDE client.

💡 Example: Gedit! 👁️

  1. Install jq
  2. Create open-in-gedit.sh:
#!/bin/bash

jq --raw-output '.files |= unique_by(.path) | .files[] | [
    "gedit",
    .path,
    "+" + (.line | tostring) + ":" + (.column | tostring),
    "&"
] | join(" ")' | bash
  1. chmod +x open-in-gedit.sh
  2. Set CARGO_EDITOR=/path/to/open-in-gedit.sh environment variable
  3. Run cargo lrun in your project directory
  4. In case of compiling errors open-in-gedit.sh will open files in gedit on affected lines and columns
  5. cargo llrun (cargo llcheck, etc.) will open them in case of warnings as well.

Similar Projects

bacon is a background rust code checker.

Thanks to all Contributors ❤️

Thanks everyone for code contributions and bug reporting. Special thanks to Casey Rodarmor for providing VimL code for quickfix populator and Otavio Salvador for NixOS package.

Wanna Contribute?

Please check out issues and kanban board. You can also make a package for your favorite OS distribution.

Support

Please support the project with crypto:

🪙 Bitcoin 👁️

1Afgvdz1oPaugFcLgDaAzCYYdHexV6tTvH

🪙 Tron (TRX, USDT-TRC20, etc.) 👁️

TVxE2HyryNyNReMvw9HRQ3BkYePCszXSrc

🪙 Ethereum (ETH, DAI, etc.) 👁️

0xa879cdb1d7d859e6e425f8e50c4ee49f4b3a7b06

License

MIT/Apache-2.0

Dependencies

~4–11MB
~134K SLoC