Skip to content

Commit 0369442

Browse files
Firehedclaude
andauthored
fix(phpcs): run from project root instead of file directory (dense-analysis#5105)
When phpcs.xml sets installed_paths to a relative path (e.g. vendor/slevomat/coding-standard), running phpcs from the file's directory causes it to fail because the path is resolved relative to cwd rather than the config file location. Change cwd to find the nearest composer.json and use that directory, matching how most PHP ecosystem tools expect to operate. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 69c945d commit 0369442

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

ale_linters/php/phpcs.vim

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ call ale#Set('php_phpcs_options', '')
77
call ale#Set('php_phpcs_executable', 'phpcs')
88
call ale#Set('php_phpcs_use_global', get(g:, 'ale_use_global_executables', 0))
99

10+
function! ale_linters#php#phpcs#GetCwd(buffer) abort
11+
let l:result = ale#path#Dirname(ale_linters#php#phpcs#FindProjectRoot(a:buffer))
12+
13+
return empty(l:result) ? v:null : l:result
14+
endfunction
15+
16+
function! ale_linters#php#phpcs#FindProjectRoot(buffer) abort
17+
let l:result = ale#path#FindNearestFile(a:buffer, 'phpcs.xml')
18+
19+
if empty(l:result)
20+
let l:result = ale#path#FindNearestFile(a:buffer, 'phpcs.xml.dist')
21+
endif
22+
23+
if empty(l:result)
24+
let l:result = ale#path#FindNearestFile(a:buffer, '.phpcs.xml')
25+
endif
26+
27+
if empty(l:result)
28+
let l:result = ale#path#FindNearestFile(a:buffer, '.phpcs.xml.dist')
29+
endif
30+
31+
if empty(l:result)
32+
let l:result = ale#path#FindNearestFile(a:buffer, 'composer.json')
33+
endif
34+
35+
return l:result
36+
endfunction
37+
1038
function! ale_linters#php#phpcs#GetCommand(buffer) abort
1139
let l:standard = ale#Var(a:buffer, 'php_phpcs_standard')
1240
let l:standard_option = !empty(l:standard)
@@ -48,7 +76,7 @@ call ale#linter#Define('php', {
4876
\ 'vendor/bin/phpcs',
4977
\ 'phpcs'
5078
\ ])},
51-
\ 'cwd': '%s:h',
79+
\ 'cwd': function('ale_linters#php#phpcs#GetCwd'),
5280
\ 'command': function('ale_linters#php#phpcs#GetCommand'),
5381
\ 'callback': 'ale_linters#php#phpcs#Handle',
5482
\})

test/linter/test_phpcs.vader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Execute(The local phpcs executable should be used):
1111

1212
let g:executable = ale#path#Simplify(g:dir . '/../test-files/phpcs/project-with-phpcs/vendor/bin/phpcs')
1313

14-
AssertLinterCwd '%s:h'
14+
AssertLinterCwd ale#path#Simplify(g:dir . '/../test-files/phpcs/project-with-phpcs')
1515
AssertLinter g:executable, ale#Escape(g:executable)
1616
\ . ' -s --report=emacs --stdin-path=%s'
1717

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)