diff --git a/changelog/fix_github_actions_in_non_default_directory.md b/changelog/fix_github_actions_in_non_default_directory.md new file mode 100644 index 00000000000..4fe559c2437 --- /dev/null +++ b/changelog/fix_github_actions_in_non_default_directory.md @@ -0,0 +1 @@ +* [#9933](https://github.com/rubocop/rubocop/pull/9933): Fix GitHub Actions formatter when running in non-default directory. ([@ojab][]) diff --git a/lib/rubocop/formatter/git_hub_actions_formatter.rb b/lib/rubocop/formatter/git_hub_actions_formatter.rb index e39fff05bc4..0628778463b 100644 --- a/lib/rubocop/formatter/git_hub_actions_formatter.rb +++ b/lib/rubocop/formatter/git_hub_actions_formatter.rb @@ -33,7 +33,7 @@ def report_offense(file, offense) output.printf( "\n::%s file=%s,line=%d,col=%d::%s\n", severity: github_severity(offense), - file: file, + file: PathUtil.smart_path(file), line: offense.line, column: offense.real_column, message: github_escape(offense.message) diff --git a/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb b/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb index 21ce0cf9634..fe7562164ad 100644 --- a/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb +++ b/spec/rubocop/formatter/git_hub_actions_formatter_spec.rb @@ -30,6 +30,15 @@ end end + context 'when file is relative to the current directory' do + let(:file) { "#{Dir.pwd}/path/to/file" } + + it 'reports offenses as error with the relative path' do + expect(output.string) + .to include('::error file=path/to/file,line=1,col=1::This is a message.') + end + end + context 'when no offenses are detected' do let(:offenses) { [] }