diff --git a/CHANGELOG.md b/CHANGELOG.md index c29152e2..3f51a47b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### :sparkles: Release Note ### :rocket: Enhancements -- ... +- [#1845](https://github.com/reviewdog/reviewdog/pull/1845) Normalize file path in `related_locations` too. ### :bug: Fixes - ... diff --git a/filter/filter.go b/filter/filter.go index eef96a94..3e9d9297 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -41,7 +41,6 @@ func FilterCheck(results []*rdf.Diagnostic, diff []*diff.FileDiff, strip int, for _, result := range results { check := &FilteredDiagnostic{Diagnostic: result, SourceLines: make(map[int]string)} loc := result.GetLocation() - loc.Path = pathutil.NormalizePath(loc.GetPath(), cwd, "") startLine := int(loc.GetRange().GetStart().GetLine()) endLine := int(loc.GetRange().GetEnd().GetLine()) if endLine == 0 { diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go index a714660a..0e8dc929 100644 --- a/pathutil/pathutil.go +++ b/pathutil/pathutil.go @@ -3,6 +3,8 @@ package pathutil import ( "path/filepath" "strings" + + "github.com/reviewdog/reviewdog/proto/rdf" ) // NormalizePath return normalized path with workdir and relative path to @@ -42,6 +44,22 @@ func NormalizeDiffPath(diffpath string, strip int) string { return filepath.ToSlash(filepath.Clean(path)) } +// NormalizePathInResults normalize file path in RDFormat results. +func NormalizePathInResults(results []*rdf.Diagnostic, cwd string) { + for _, result := range results { + normalizeLocation(result.GetLocation(), cwd) + for _, rel := range result.GetRelatedLocations() { + normalizeLocation(rel.GetLocation(), cwd) + } + } +} + +func normalizeLocation(loc *rdf.Location, cwd string) { + if loc != nil { + loc.Path = NormalizePath(loc.GetPath(), cwd, "") + } +} + func contains(path, base string) bool { ps := splitPathList(path) bs := splitPathList(base) diff --git a/pathutil/pathutil_test.go b/pathutil/pathutil_test.go new file mode 100644 index 00000000..7ff63333 --- /dev/null +++ b/pathutil/pathutil_test.go @@ -0,0 +1,49 @@ +package pathutil + +import ( + "strings" + "testing" + + "github.com/reviewdog/reviewdog/proto/rdf" +) + +func TestNormalizePathInResults(t *testing.T) { + cwd := "/path/to/cwd" + results := []*rdf.Diagnostic{ + { + Location: &rdf.Location{ + Path: cwd + "/" + "sample_1_abs.txt", + }, + }, + { + Location: &rdf.Location{ + Path: "sample_2_rel.txt", + }, + }, + { + RelatedLocations: []*rdf.RelatedLocation{ + { + Location: &rdf.Location{ + Path: cwd + "/" + "sample_related_1_abs.txt", + }, + }, + { + Location: &rdf.Location{ + Path: "sample_related_2_rel.txt", + }, + }, + }, + }, + } + NormalizePathInResults(results, cwd) + for _, result := range results { + if strings.HasPrefix(result.GetLocation().GetPath(), cwd) { + t.Errorf("path unexpectedly contain prefix: %s", result.GetLocation().GetPath()) + } + for _, rel := range result.GetRelatedLocations() { + if strings.HasPrefix(rel.GetLocation().GetPath(), cwd) { + t.Errorf("related locations path unexpectedly contain prefix: %s", rel.GetLocation().GetPath()) + } + } + } +} diff --git a/reviewdog.go b/reviewdog.go index ed324275..08e8b5f8 100644 --- a/reviewdog.go +++ b/reviewdog.go @@ -10,6 +10,7 @@ import ( "github.com/reviewdog/reviewdog/diff" "github.com/reviewdog/reviewdog/filter" "github.com/reviewdog/reviewdog/parser" + "github.com/reviewdog/reviewdog/pathutil" "github.com/reviewdog/reviewdog/proto/rdf" ) @@ -73,6 +74,8 @@ func (w *Reviewdog) runFromResult(ctx context.Context, results []*rdf.Diagnostic return err } + pathutil.NormalizePathInResults(results, wd) + checks := filter.FilterCheck(results, filediffs, strip, wd, w.filterMode) hasViolations := false