From 0d2485fd704c30bac232dd3a29aa8d700467d342 Mon Sep 17 00:00:00 2001 From: Yury Gargay Date: Sun, 22 Oct 2023 17:35:22 +0200 Subject: [PATCH 1/2] Convert backslashes to forward slashes in GitHub Action annotations This commit addresses an issue in the GitHub Actions printer where file paths were using backslashes on Windows systems. As result we didn't see correct annotations in the PRs. To ensure proper formatting we always convert to forward slash separator in filepathes. --- pkg/printers/github.go | 9 ++++++++- pkg/printers/github_test.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/printers/github.go b/pkg/printers/github.go index 7f148097ab8c..6a9512515911 100644 --- a/pkg/printers/github.go +++ b/pkg/printers/github.go @@ -3,6 +3,8 @@ package printers import ( "fmt" "io" + "path/filepath" + "strings" "github.com/golangci/golangci-lint/pkg/result" ) @@ -26,7 +28,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 + list := filepath.SplitList(issue.FilePath()) + file := strings.Join(list, "/") + + 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) } diff --git a/pkg/printers/github_test.go b/pkg/printers/github_test.go index 4652b0c760f7..0513e2c4b0b7 100644 --- a/pkg/printers/github_test.go +++ b/pkg/printers/github_test.go @@ -4,6 +4,7 @@ package printers import ( "bytes" "go/token" + "runtime" "testing" "github.com/stretchr/testify/assert" @@ -72,3 +73,23 @@ 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)) +} From d7b2c05f06fe0d4bde94979f33269271456e9f4b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sun, 22 Oct 2023 21:37:39 +0200 Subject: [PATCH 2/2] review: simplify --- pkg/printers/github.go | 9 ++++----- pkg/printers/github_test.go | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/printers/github.go b/pkg/printers/github.go index 6a9512515911..c1da9df9c602 100644 --- a/pkg/printers/github.go +++ b/pkg/printers/github.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "path/filepath" - "strings" "github.com/golangci/golangci-lint/pkg/result" ) @@ -28,10 +27,10 @@ func formatIssueAsGithub(issue *result.Issue) string { severity = issue.Severity } - // 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 - list := filepath.SplitList(issue.FilePath()) - file := strings.Join(list, "/") + // 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 { diff --git a/pkg/printers/github_test.go b/pkg/printers/github_test.go index 0513e2c4b0b7..214a16e8f33f 100644 --- a/pkg/printers/github_test.go +++ b/pkg/printers/github_test.go @@ -78,6 +78,7 @@ 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",