Skip to content

Commit 0dd4a95

Browse files
bwilkersonCommit Queue
authored andcommitted
Display correct line and column offsets for context messages
The line and column offsets for the context messages were being computed based on line information for the file in which the diagnostic was being reported, which can be different than the file pointed to by the context message. This ensures that we use the right line info. Fixes #60750 Change-Id: I6dbc95bde5aeb1abc70d90767dc59082982a5a96 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429380 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 652a419 commit 0dd4a95

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

pkg/analysis_server/lib/src/protocol_server.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,22 @@ DiagnosticMessage newDiagnosticMessage(
215215
var offset = message.offset;
216216
var length = message.length;
217217

218-
var startLocation = result.lineInfo.getLocation(offset);
218+
var lineInfo = result.lineInfo;
219+
if (result.path != message.filePath) {
220+
var messageResult = result.session.getFile(message.filePath);
221+
// If we can't get a result for the file then we will return bogus start and
222+
// end positions, but that's probably better than not returning the
223+
// diagnostic.
224+
if (messageResult is engine.FileResult) {
225+
lineInfo = messageResult.lineInfo;
226+
}
227+
}
228+
229+
var startLocation = lineInfo.getLocation(offset);
219230
var startLine = startLocation.lineNumber;
220231
var startColumn = startLocation.columnNumber;
221232

222-
var endLocation = result.lineInfo.getLocation(offset + length);
233+
var endLocation = lineInfo.getLocation(offset + length);
223234
var endLine = endLocation.lineNumber;
224235
var endColumn = endLocation.columnNumber;
225236

pkg/analysis_server/test/protocol_server_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AnalysisErrorTest {
5353
result = _ResolvedUnitResultImplMock(
5454
lineInfo: lineInfo,
5555
errors: [engineError],
56+
path: 'foo.dart',
5657
);
5758
}
5859

@@ -70,6 +71,7 @@ class AnalysisErrorTest {
7071
_ResolvedUnitResultImplMock(
7172
lineInfo: engine.LineInfo([0, 5, 9, 20]),
7273
errors: [engineError],
74+
path: 'bar.dart',
7375
),
7476
engineError,
7577
);
@@ -441,7 +443,14 @@ class _ResolvedUnitResultImplMock implements engine.ResolvedUnitResultImpl {
441443
@override
442444
final List<engine.Diagnostic> errors;
443445

444-
_ResolvedUnitResultImplMock({required this.lineInfo, required this.errors});
446+
@override
447+
final String path;
448+
449+
_ResolvedUnitResultImplMock({
450+
required this.lineInfo,
451+
required this.errors,
452+
required this.path,
453+
});
445454

446455
@override
447456
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);

pkg/dartdev/lib/src/commands/analyze.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ class AnalyzeCommand extends DartdevCommand {
380380

381381
// Add any context messages as bullet list items.
382382
for (var message in error.contextMessages) {
383-
var contextPath = _relativePath(error.file, relativeToDir);
383+
var contextPath = _relativePath(message.filePath, relativeToDir);
384384
var messageSentenceFragment = trimEnd(message.message, '.');
385385

386386
log.stdout('$_bodyIndent'

pkg/dartdev/test/commands/analyze_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,31 @@ void defineAnalyze() {
319319
expect(result.stdout, contains('return_of_invalid_type'));
320320
expect(result.stdout, contains('1 issue found.'));
321321
});
322+
323+
test('error with context message', () async {
324+
p = project(mainSrc: '''
325+
part 'a.dart';
326+
327+
class B extends A {
328+
@override
329+
void m(String p) {}
330+
}
331+
''');
332+
p.file('lib${path.separator}a.dart', '''
333+
part of 'main.dart';
334+
335+
class A {
336+
void m(int p) {}
337+
}
338+
''');
339+
var result = await p.runAnalyze([p.mainPath]);
340+
341+
expect(result.exitCode, 3);
342+
expect(result.stderr, isEmpty);
343+
expect(result.stdout, contains('a.dart:4:8.'));
344+
expect(result.stdout, contains('invalid_override'));
345+
expect(result.stdout, contains('1 issue found.'));
346+
});
322347
});
323348

324349
test('warning --fatal-warnings', () async {

0 commit comments

Comments
 (0)