aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorCopilot2026-03-28 21:52:26 +0900
committerGitHub2026-03-28 21:52:26 +0900
commit90d8f8d2d2f32718d19b71578f63a68c026f6984 (patch)
treefd80e3dbe5d8e5be19b798f2348f64d2724ae32d
parent3d3b75cdc523b27f8eb5b6c10251c4e242f129b9 (diff)
downloadale-90d8f8d2d2f32718d19b71578f63a68c026f6984.tar.gz

Fix FindCompileCommands to handle absolute paths in cbuilddir_names (#5096)

  • Initial plan

  • Fix FindCompileCommands to handle absolute paths in cbuilddir_names

Use ale#path#GetAbsPath to resolve build directory paths, which correctly handles both absolute and relative paths. When the dirname is absolute, derive the project root from the parent of the build directory.

Co-authored-by: w0rp <3518142+w0rp@users.noreply.github.com>

  • Improve comment clarity in FindCompileCommands

Co-authored-by: w0rp <3518142+w0rp@users.noreply.github.com>

  • Add defensive ale#Set for cbuilddir_names in FindCompileCommands

Mirror the existing s:CanParseMakefile pattern to ensure the g:alecbuilddirnames variable always exists when ale#Var is called, even if test Save/Restore cycles delete it.

Co-authored-by: w0rp <3518142+w0rp@users.noreply.github.com>


Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: w0rp <3518142+w0rp@users.noreply.github.com>

-rw-r--r--autoload/ale/c.vim21
-rw-r--r--test/test_c_find_compile_commands.vader33
2 files changed, 52 insertions, 2 deletions
diff --git a/autoload/ale/c.vim b/autoload/ale/c.vim
index bc955d0ea..329637b5c 100644
--- a/autoload/ale/c.vim
+++ b/autoload/ale/c.vim
@@ -246,14 +246,31 @@ function! ale#c#FindCompileCommands(buffer) abort
return [fnamemodify(l:json_file, ':h'), l:json_file]
endif
+ " Something somewhere seems to delete this setting in tests, so ensure
+ " we always have a default value.
+ call ale#Set('c_build_dir_names', [
+ \ 'build',
+ \ 'build/Debug',
+ \ 'build/Release',
+ \ 'bin',
+ \])
+
" 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]
+ " For absolute build dir paths, use the parent
+ " of the build dir as the project root. For
+ " relative paths, use the directory 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 000000000..c24f26c0a
--- /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(''))