Skip to content

Server format results can be incorrect if sent immediately after updateContent #57120

@DanTup

Description

@DanTup

This came up at dart-lang/dart-pad#3092 - the format tests for DartPad are flaky against main.

I can reproduce in an analysis server test at test\edit\format_test.dart:

@soloTest
Future<void> test_danny() async {
  var content = 'void main() {}';
  var updatedContent = "void main() {\n  print('hello world');\n}\n";

  await handleSuccessfulRequest(
    AnalysisUpdateContentParams({
      testFile.path: AddContentOverlay(content),
    }).toRequest('1', clientUriConverter: server.uriConverter),
  );
  // Not awaiting is important to trigger this.
  unawaited(
    handleSuccessfulRequest(
      AnalysisUpdateContentParams({
        testFile.path: AddContentOverlay(updatedContent),
      }).toRequest('3', clientUriConverter: server.uriConverter),
    ),
  );
  var formatResult = await _formatAt(0, 0);

  expect(formatResult.edits, isNotNull);
  expect(formatResult.edits, hasLength(0));
}

By not awaiting the updateContent, the format request can return incorrect results. There should be no format result, but we get:

00:00 +0 -1: FormatTest | test_danny [E]

  Expected: an object with length of <0>
    Actual: [SourceEdit:{"offset":0,"length":14,"replacement":"void main() {}\n"}]
     Which: has length of <1>

@bwilkerson can you confirm my assumption here is correct - there should be no need to await the overlay update, because the server should synchronously process the overlay update enough that no future requests use stale results?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions