Skip to content

Commit f81bc02

Browse files
committed
Merge remote-tracking branch 'upstream/master'
* upstream/master: Fix FindCompileCommands to handle absolute paths in c_build_dir_names (dense-analysis#5096)
2 parents a155b6a + 90d8f8d commit f81bc02

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

autoload/ale/c.vim

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,31 @@ function! ale#c#FindCompileCommands(buffer) abort
246246
return [fnamemodify(l:json_file, ':h'), l:json_file]
247247
endif
248248

249+
" Something somewhere seems to delete this setting in tests, so ensure
250+
" we always have a default value.
251+
call ale#Set('c_build_dir_names', [
252+
\ 'build',
253+
\ 'build/Debug',
254+
\ 'build/Release',
255+
\ 'bin',
256+
\])
257+
249258
" Search in build directories if we can't find it in the project.
250259
for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
251260
for l:dirname in ale#Var(a:buffer, 'c_build_dir_names')
252-
let l:c_build_dir = l:path . s:sep . l:dirname
261+
let l:c_build_dir = ale#path#GetAbsPath(l:path, l:dirname)
253262
let l:json_file = l:c_build_dir . s:sep . 'compile_commands.json'
254263

255264
if filereadable(l:json_file)
256-
return [l:path, l:json_file]
265+
" For absolute build dir paths, use the parent
266+
" of the build dir as the project root. For
267+
" relative paths, use the directory found by
268+
" searching upwards from the file.
269+
let l:root = ale#path#IsAbsolute(l:dirname)
270+
\ ? fnamemodify(l:c_build_dir, ':h')
271+
\ : l:path
272+
273+
return [l:root, l:json_file]
257274
endif
258275
endfor
259276
endfor
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Before:
2+
Save g:ale_c_build_dir_names
3+
4+
call ale#test#SetDirectory('/testplugin/test')
5+
6+
After:
7+
Restore
8+
9+
call ale#test#RestoreDirectory()
10+
11+
Execute(FindCompileCommands should find compile_commands.json with relative build dir names):
12+
call ale#test#SetFilename('test-files/c/json_project/subdir/dummy')
13+
14+
let g:ale_c_build_dir_names = ['build']
15+
16+
AssertEqual
17+
\ [
18+
\ ale#path#Simplify(g:dir . '/test-files/c/json_project'),
19+
\ ale#path#Simplify(g:dir . '/test-files/c/json_project/build/compile_commands.json'),
20+
\ ],
21+
\ ale#c#FindCompileCommands(bufnr(''))
22+
23+
Execute(FindCompileCommands should find compile_commands.json with absolute build dir names):
24+
call ale#test#SetFilename('test-files/c/json_project/subdir/dummy')
25+
26+
let g:ale_c_build_dir_names = [ale#path#Simplify(g:dir . '/test-files/c/json_project/build')]
27+
28+
AssertEqual
29+
\ [
30+
\ ale#path#Simplify(g:dir . '/test-files/c/json_project'),
31+
\ ale#path#Simplify(g:dir . '/test-files/c/json_project/build/compile_commands.json'),
32+
\ ],
33+
\ ale#c#FindCompileCommands(bufnr(''))

0 commit comments

Comments
 (0)