From d9e139ae2384b04af05ce9343aa0d0a5bae5449b Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Thu, 28 Jun 2018 00:36:02 +0300 Subject: [PATCH] Rust Cargo linter: Improve workspace support (#1679) * Rust Cargo linter: Improve workspace support When using Cargo workspaces [1], there is a 'Cargo.toml' directory in a top level directory, listing all the crates in the project. If we are currently editing one of the crates, 'cargo build' should execute in that directory for that crate's separate `Cargo.toml`, otherwise Cargo may spend more time possibly rebuilding the entire workspace, and maybe failing on one of the other crates, instead of succeeding on the current. [1] https://doc.rust-lang.org/book/second-edition/ch14-03-cargo-workspaces.html --- ale_linters/rust/cargo.vim | 15 +++++++++++- doc/ale-rust.txt | 9 ++++++++ .../cargo_workspace_paths/Cargo.toml | 0 .../cargo_workspace_paths/subpath/Cargo.toml | 0 .../test_cargo_command_callbacks.vader | 23 +++++++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/command_callback/cargo_workspace_paths/Cargo.toml create mode 100644 test/command_callback/cargo_workspace_paths/subpath/Cargo.toml diff --git a/ale_linters/rust/cargo.vim b/ale_linters/rust/cargo.vim index ef0c3bd1..e6c3870a 100644 --- a/ale_linters/rust/cargo.vim +++ b/ale_linters/rust/cargo.vim @@ -6,6 +6,7 @@ call ale#Set('rust_cargo_use_check', 1) call ale#Set('rust_cargo_check_all_targets', 0) call ale#Set('rust_cargo_check_examples', 0) call ale#Set('rust_cargo_check_tests', 0) +call ale#Set('rust_cargo_avoid_whole_workspace', 1) call ale#Set('rust_cargo_default_feature_behavior', 'default') call ale#Set('rust_cargo_include_features', '') @@ -45,6 +46,18 @@ function! ale_linters#rust#cargo#GetCommand(buffer, version_output) abort let l:include_features = ' --features ' . ale#Escape(l:include_features) endif + let l:avoid_whole_workspace = ale#Var(a:buffer, 'rust_cargo_avoid_whole_workspace') + let l:nearest_cargo_prefix = '' + + if l:avoid_whole_workspace + let l:nearest_cargo = ale#path#FindNearestFile(a:buffer, 'Cargo.toml') + let l:nearest_cargo_dir = fnamemodify(l:nearest_cargo, ':h') + + if l:nearest_cargo_dir isnot# '.' + let l:nearest_cargo_prefix = 'cd '. ale#Escape(l:nearest_cargo_dir) .' && ' + endif + endif + let l:default_feature_behavior = ale#Var(a:buffer, 'rust_cargo_default_feature_behavior') if l:default_feature_behavior is# 'all' let l:include_features = '' @@ -55,7 +68,7 @@ function! ale_linters#rust#cargo#GetCommand(buffer, version_output) abort let l:default_feature = '' endif - return 'cargo ' + return l:nearest_cargo_prefix . 'cargo ' \ . (l:use_check ? 'check' : 'build') \ . (l:use_all_targets ? ' --all-targets' : '') \ . (l:use_examples ? ' --examples' : '') diff --git a/doc/ale-rust.txt b/doc/ale-rust.txt index f9afe17d..d61e5b55 100644 --- a/doc/ale-rust.txt +++ b/doc/ale-rust.txt @@ -108,6 +108,15 @@ g:ale_rust_cargo_include_features *g:ale_rust_cargo_include_features* When defined, ALE will set the `--features` option when invoking `cargo` to perform the lint check. See |g:ale_rust_cargo_default_feature_behavior|. +g:ale_rust_cargo_avoid_whole_workspace *g:ale_rust_cargo_avoid_whole_workspace* + *b:ale_rust_cargo_avoid_whole_workspace* + Type: |Number| + Default: `1` + + When set to 1, and ALE is used to edit a crate that is part of a Cargo + workspace, avoid building the entire entire workspace by invoking + `cargo` directly in the crate's directory. Otherwise, behave as usual. + =============================================================================== rls *ale-rust-rls* diff --git a/test/command_callback/cargo_workspace_paths/Cargo.toml b/test/command_callback/cargo_workspace_paths/Cargo.toml new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/cargo_workspace_paths/subpath/Cargo.toml b/test/command_callback/cargo_workspace_paths/subpath/Cargo.toml new file mode 100644 index 00000000..e69de29b diff --git a/test/command_callback/test_cargo_command_callbacks.vader b/test/command_callback/test_cargo_command_callbacks.vader index f674645f..6bdc10b6 100644 --- a/test/command_callback/test_cargo_command_callbacks.vader +++ b/test/command_callback/test_cargo_command_callbacks.vader @@ -5,6 +5,7 @@ Before: Save g:ale_rust_cargo_check_examples Save g:ale_rust_cargo_default_feature_behavior Save g:ale_rust_cargo_include_features + Save g:ale_rust_cargo_avoid_whole_workspace unlet! g:ale_rust_cargo_use_check unlet! g:ale_rust_cargo_check_all_targets @@ -12,6 +13,7 @@ Before: unlet! g:ale_rust_cargo_check_examples unlet! g:ale_rust_cargo_default_feature_behavior unlet! g:ale_rust_cargo_include_features + unlet! g:ale_rust_cargo_avoid_whole_workspace runtime ale_linters/rust/cargo.vim call ale#test#SetDirectory('/testplugin/test/command_callback') @@ -198,3 +200,24 @@ Execute(--all-features should be used when g:ale_rust_cargo_default_feature_beha \ ale_linters#rust#cargo#GetCommand(bufnr(''), [ \ 'cargo 0.22.0 (3423351a5 2017-10-06)', \ ]) + +Execute(When a crate belongs to a workspace we chdir into the crate.): + call ale#test#SetFilename('cargo_workspace_paths/subpath/test.rs') + + if ale#Has('win32') + let test_cdprefix = "C:\\testplugin\\test\\command_callback\\cargo_workspace_paths\\subpath" + else + let test_cdprefix = "'/testplugin/test/command_callback/cargo_workspace_paths/subpath'" + endif + + AssertEqual + \ "cd ".test_cdprefix." && cargo build --frozen --message-format=json -q", + \ ale_linters#rust#cargo#GetCommand(bufnr(''), []) + +Execute(When a crate belongs to a workspace we chdir into the crate, unless we disabled it): + let g:ale_rust_cargo_avoid_whole_workspace = 0 + call ale#test#SetFilename('cargo_workspace_paths/subpath/test.rs') + + AssertEqual + \ "cargo build --frozen --message-format=json -q", + \ ale_linters#rust#cargo#GetCommand(bufnr(''), [])