Skip to content

Commit

Permalink
output: convert backslashes to forward slashes for GitHub Action anno…
Browse files Browse the repository at this point in the history
…tations printer (#4149)

Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
surik and ldez committed Oct 24, 2023
1 parent 3d58209 commit 9b20d49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/printers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package printers
import (
"fmt"
"io"
"path/filepath"

"github.com/golangci/golangci-lint/pkg/result"
)
Expand All @@ -26,7 +27,12 @@ func formatIssueAsGithub(issue *result.Issue) string {
severity = issue.Severity
}

ret := fmt.Sprintf("::%s file=%s,line=%d", severity, issue.FilePath(), issue.Line())
// Convert backslashes to forward slashes.
// This is needed when running on windows.
// Otherwise, GitHub won't be able to show the annotations pointing to the file path with backslashes.
file := filepath.ToSlash(issue.FilePath())

ret := fmt.Sprintf("::%s file=%s,line=%d", severity, file, issue.Line())
if issue.Pos.Column != 0 {
ret += fmt.Sprintf(",col=%d", issue.Pos.Column)
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/printers/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package printers
import (
"bytes"
"go/token"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -72,3 +73,24 @@ func TestFormatGithubIssue(t *testing.T) {
sampleIssue.Pos.Column = 0
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
}

func TestFormatGithubIssueWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Skipping test on non Windows")
}

sampleIssue := result.Issue{
FromLinter: "sample-linter",
Text: "some issue",
Pos: token.Position{
Filename: "path\\to\\file.go",
Offset: 2,
Line: 10,
Column: 4,
},
}
require.Equal(t, "::error file=path/to/file.go,line=10,col=4::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))

sampleIssue.Pos.Column = 0
require.Equal(t, "::error file=path/to/file.go,line=10::some issue (sample-linter)", formatIssueAsGithub(&sampleIssue))
}

0 comments on commit 9b20d49

Please sign in to comment.