Skip to content

Commit

Permalink
rust/cargo: add support for a custom target directory
Browse files Browse the repository at this point in the history
This can avoid having to wait for ALE or ALE being blocked on other
cargo actions within the same crate.
  • Loading branch information
mathstuf committed Aug 13, 2020
1 parent c2b01f0 commit a0bfd58
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ale_linters/rust/cargo.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ call ale#Set('rust_cargo_default_feature_behavior', 'default')
call ale#Set('rust_cargo_include_features', '')
call ale#Set('rust_cargo_use_clippy', 0)
call ale#Set('rust_cargo_clippy_options', '')
call ale#Set('rust_cargo_target_dir', '')

function! ale_linters#rust#cargo#GetCargoExecutable(bufnr) abort
if ale#path#FindNearestFile(a:bufnr, 'Cargo.toml') isnot# ''
Expand All @@ -31,6 +32,9 @@ function! ale_linters#rust#cargo#GetCommand(buffer, version) abort
\ && ale#semver#GTE(a:version, [0, 22, 0])
let l:use_tests = ale#Var(a:buffer, 'rust_cargo_check_tests')
\ && ale#semver#GTE(a:version, [0, 22, 0])
let l:target_dir = ale#Var(a:buffer, 'rust_cargo_target_dir')
let l:use_target_dir = !empty(l:target_dir)
\ && ale#semver#GTE(a:version, [0, 17, 0])

let l:include_features = ale#Var(a:buffer, 'rust_cargo_include_features')

Expand Down Expand Up @@ -82,6 +86,7 @@ function! ale_linters#rust#cargo#GetCommand(buffer, version) abort
\ . (l:use_all_targets ? ' --all-targets' : '')
\ . (l:use_examples ? ' --examples' : '')
\ . (l:use_tests ? ' --tests' : '')
\ . (l:use_target_dir ? (' --target-dir ' . ale#Escape(l:target_dir)) : '')
\ . ' --frozen --message-format=json -q'
\ . l:default_feature
\ . l:include_features
Expand Down
12 changes: 12 additions & 0 deletions doc/ale-rust.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ g:ale_rust_cargo_clippy_options
only `cargo clippy` supports (e.g. `--deny`).


g:ale_rust_cargo_target_dir
*g:ale_rust_cargo_target_dir*
*b:ale_rust_cargo_target_dir*

Type: |String|
Default: `''`

Use a custom target directory when running the commands for ALE. This can
help to avoid "waiting for file lock on build directory" messages when
running `cargo` commands manually while ALE is performing its checks.


===============================================================================
rls *ale-rust-rls*

Expand Down
24 changes: 22 additions & 2 deletions test/command_callback/test_cargo_command_callbacks.vader
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ Execute(Build supports all cargo flags):
let g:ale_rust_cargo_check_tests = 1
let g:ale_rust_cargo_check_examples = 1
let b:ale_rust_cargo_default_feature_behavior = 'all'
let b:ale_rust_cargo_target_dir = 'target/ale'

AssertLinter 'cargo', [
\ ale#Escape('cargo') . ' --version',
\ 'cargo build --all-targets --examples --tests --frozen --message-format=json -q --all-features',
\ 'cargo build --all-targets --examples --tests --target-dir ' . ale#Escape('target/ale') . ' --frozen --message-format=json -q --all-features',
\]

Execute(Clippy supports all cargo flags):
Expand All @@ -182,10 +183,11 @@ Execute(Clippy supports all cargo flags):
let g:ale_rust_cargo_check_examples = 1
let b:ale_rust_cargo_default_feature_behavior = 'all'
let b:ale_rust_cargo_clippy_options = '-D warnings'
let b:ale_rust_cargo_target_dir = 'target/ale'

AssertLinter 'cargo', [
\ ale#Escape('cargo') . ' --version',
\ 'cargo clippy --all-targets --examples --tests --frozen --message-format=json -q --all-features -- -D warnings',
\ 'cargo clippy --all-targets --examples --tests --target-dir ' . ale#Escape('target/ale') . ' --frozen --message-format=json -q --all-features -- -D warnings',
\]

Execute(cargo-check does not refer ale_rust_cargo_clippy_options):
Expand All @@ -197,3 +199,21 @@ Execute(cargo-check does not refer ale_rust_cargo_clippy_options):
\ ale#Escape('cargo') . ' --version',
\ 'cargo check --frozen --message-format=json -q',
\]

Execute(`cargo --target-dir` should be used when the version is new enough and it is set):
let b:ale_rust_cargo_target_dir = 'target/ale'

GivenCommandOutput ['cargo 0.17.0 (3423351a5 2017-10-06)']
AssertLinter 'cargo', [
\ ale#Escape('cargo') . ' --version',
\ 'cargo check --target-dir ' . ale#Escape('target/ale') . g:suffix,
\]

Execute(`cargo --target-dir` should not be used when the version is not new enough and it is set):
let b:ale_rust_cargo_target_dir = 'target/ale'

GivenCommandOutput ['cargo 0.16.0 (3423351a5 2017-10-06)']
AssertLinter 'cargo', [
\ ale#Escape('cargo') . ' --version',
\ 'cargo build' . g:suffix,
\]

0 comments on commit a0bfd58

Please sign in to comment.