Skip to content

Commit b31e9b0

Browse files
Copilotpascalberger
andcommitted
Fix "label column cannot start at the end of the line" error
Co-authored-by: pascalberger <[email protected]>
1 parent 0940bea commit b31e9b0

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

src/Cake.Issues.Reporting.Console.Tests/ConsoleIssueReportGeneratorTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,67 @@ public void Should_Generate_Report_With_No_Issues(
102102

103103
// Then
104104
}
105+
106+
[Fact]
107+
public void Should_Handle_Issue_At_End_Of_Line()
108+
{
109+
// Given
110+
var fixture = new ConsoleIssueReportFixture
111+
{
112+
ConsoleIssueReportFormatSettings =
113+
{
114+
ShowDiagnostics = true
115+
}
116+
};
117+
118+
// Create an issue at the end of a line (column equals line length)
119+
var issues = new[]
120+
{
121+
IssueBuilder
122+
.NewIssue("SA1629", "StyleCop", "StyleCopAnalyzers")
123+
.InFile("src/MyProject/MyFile.cs", 20, 119) // Column 119 which matches the error scenario
124+
.OfRule("SA1629")
125+
.WithPriority(IssuePriority.Warning)
126+
.Create()
127+
};
128+
129+
// When - This should not throw an exception
130+
var exception = Record.Exception(() =>
131+
fixture.CreateReport(issues, @"c:\Source\Cake.Issues.Reporting.Console"));
132+
133+
// Then
134+
exception.ShouldBeNull();
135+
}
136+
137+
[Fact]
138+
public void Should_Handle_Issue_With_End_Column_At_End_Of_Line()
139+
{
140+
// Given
141+
var fixture = new ConsoleIssueReportFixture
142+
{
143+
ConsoleIssueReportFormatSettings =
144+
{
145+
ShowDiagnostics = true
146+
}
147+
};
148+
149+
// Create an issue with EndColumn at the end of a line
150+
var issues = new[]
151+
{
152+
IssueBuilder
153+
.NewIssue("Test issue with end column at line end", "TestProvider", "TestProvider")
154+
.InFile("TestFile.cs", 10, null, 110, 119) // Line 10, Column 110-119 with EndColumn at end of line
155+
.OfRule("TestRule")
156+
.WithPriority(IssuePriority.Error)
157+
.Create()
158+
};
159+
160+
// When - This should not throw an exception
161+
var exception = Record.Exception(() =>
162+
fixture.CreateReport(issues, @"c:\Source\Cake.Issues.Reporting.Console"));
163+
164+
// Then
165+
exception.ShouldBeNull();
166+
}
105167
}
106168
}

src/Cake.Issues.Reporting.Console.Tests/IssueDiagnosticTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,25 @@ public void Should_Not_Include_Project_When_Not_Available()
9191
Assert.Fail("Note should not be null when rule URL is provided");
9292
}
9393
}
94+
95+
[Fact]
96+
public void Should_Handle_Issue_At_End_Of_Line()
97+
{
98+
// Given
99+
// Create an issue where the column is at the end of the line
100+
// This scenario previously caused "label column cannot start at the end of the line" error
101+
var issue = IssueBuilder
102+
.NewIssue("Test message at end of line", "TestProvider", "TestProviderName")
103+
.InFile("test.cs", 10, 119) // Column 119 which could be at end of line
104+
.OfRule("TestRule")
105+
.WithPriority(IssuePriority.Warning)
106+
.Create();
107+
108+
// When
109+
var exception = Record.Exception(() => new IssueDiagnostic(issue));
110+
111+
// Then
112+
exception.ShouldBeNull();
113+
}
94114
}
95115
}

src/Cake.Issues.Reporting.Console/IssueDiagnostic.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ private static (Location Location, int Lenght) GetLocation(IIssue issue)
9393
if (issue.EndColumn.HasValue)
9494
{
9595
length = issue.EndColumn.Value - issue.Column.Value;
96+
97+
// Ensure length is not negative
98+
if (length < 0)
99+
{
100+
length = 0;
101+
}
96102
}
97103

98104
return (location, length);
@@ -114,19 +120,36 @@ private void CreateLabels()
114120
foreach (var issue in this.issues)
115121
{
116122
var (location, length) = GetLocation(issue);
117-
var label =
118-
new Label(
119-
issue.AffectedFileRelativePath.FullPath,
120-
location,
121-
issue.Message(IssueCommentFormat.PlainText))
122-
.WithColor(color);
123123

124-
if (length > 0)
124+
try
125125
{
126-
label = label.WithLength(length);
126+
var label =
127+
new Label(
128+
issue.AffectedFileRelativePath.FullPath,
129+
location,
130+
issue.Message(IssueCommentFormat.PlainText))
131+
.WithColor(color);
132+
133+
if (length > 0)
134+
{
135+
label = label.WithLength(length);
136+
}
137+
138+
this.Labels.Add(label);
127139
}
140+
catch (ArgumentException ex) when (ex.Message.Contains("label column cannot start at the end of the line"))
141+
{
142+
// If Errata throws an error because the column is at the end of the line,
143+
// create a label at the beginning of the line instead so the issue is still reported
144+
var fallbackLocation = new Location(issue.Line.Value, 1);
145+
var labelWithFallbackLocation = new Label(
146+
issue.AffectedFileRelativePath.FullPath,
147+
fallbackLocation,
148+
issue.Message(IssueCommentFormat.PlainText))
149+
.WithColor(color);
128150

129-
this.Labels.Add(label);
151+
this.Labels.Add(labelWithFallbackLocation);
152+
}
130153
}
131154
}
132155
}

0 commit comments

Comments
 (0)