diff --git a/autoload/ale/c.vim b/autoload/ale/c.vim index bc955d0e..9ff7e697 100644 --- a/autoload/ale/c.vim +++ b/autoload/ale/c.vim @@ -249,11 +249,18 @@ function! ale#c#FindCompileCommands(buffer) abort " Search in build directories if we can't find it in the project. for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h')) for l:dirname in ale#Var(a:buffer, 'c_build_dir_names') - let l:c_build_dir = l:path . s:sep . l:dirname + let l:c_build_dir = ale#path#GetAbsPath(l:path, l:dirname) let l:json_file = l:c_build_dir . s:sep . 'compile_commands.json' if filereadable(l:json_file) - return [l:path, l:json_file] + " Use the parent of the build dir for absolute + " paths, otherwise use the path found by searching + " upwards from the file. + let l:root = ale#path#IsAbsolute(l:dirname) + \ ? fnamemodify(l:c_build_dir, ':h') + \ : l:path + + return [l:root, l:json_file] endif endfor endfor diff --git a/test/test_c_find_compile_commands.vader b/test/test_c_find_compile_commands.vader new file mode 100644 index 00000000..c24f26c0 --- /dev/null +++ b/test/test_c_find_compile_commands.vader @@ -0,0 +1,33 @@ +Before: + Save g:ale_c_build_dir_names + + call ale#test#SetDirectory('/testplugin/test') + +After: + Restore + + call ale#test#RestoreDirectory() + +Execute(FindCompileCommands should find compile_commands.json with relative build dir names): + call ale#test#SetFilename('test-files/c/json_project/subdir/dummy') + + let g:ale_c_build_dir_names = ['build'] + + AssertEqual + \ [ + \ ale#path#Simplify(g:dir . '/test-files/c/json_project'), + \ ale#path#Simplify(g:dir . '/test-files/c/json_project/build/compile_commands.json'), + \ ], + \ ale#c#FindCompileCommands(bufnr('')) + +Execute(FindCompileCommands should find compile_commands.json with absolute build dir names): + call ale#test#SetFilename('test-files/c/json_project/subdir/dummy') + + let g:ale_c_build_dir_names = [ale#path#Simplify(g:dir . '/test-files/c/json_project/build')] + + AssertEqual + \ [ + \ ale#path#Simplify(g:dir . '/test-files/c/json_project'), + \ ale#path#Simplify(g:dir . '/test-files/c/json_project/build/compile_commands.json'), + \ ], + \ ale#c#FindCompileCommands(bufnr(''))