Skip to content

Commit be7a40d

Browse files
DanTupCommit Queue
authored andcommitted
[analyzer] Ensure parseFileSync() flushes pending file changes
Fixes #57120 Fixes dart-lang/dart-pad#3092 Change-Id: I803cdc19702bcd357cdafde3176ecad246495182 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396003 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 2a4ce14 commit be7a40d

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

pkg/analysis_server/test/edit/format_test.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analysis_server/protocol/protocol_generated.dart';
5+
import 'dart:async';
6+
7+
import 'package:analysis_server/src/protocol_server.dart';
68
import 'package:test/test.dart';
79
import 'package:test_reflective_loader/test_reflective_loader.dart';
810

@@ -23,6 +25,37 @@ class FormatTest extends PubPackageAnalysisServerTest {
2325
await setRoots(included: [workspaceRootPath], excluded: []);
2426
}
2527

28+
/// Verify that an overlay change is reflected in a format request that
29+
/// is sent immediately without waiting.
30+
///
31+
/// https://github.com/dart-lang/sdk/issues/57120
32+
Future<void> test_format_immediatelyAfterOverlayChange() async {
33+
var initialContentNeedsFormatting = 'void main() { }';
34+
var updatedContentNeedsNoFormatting =
35+
"void main() {\n print('hello world');\n}\n";
36+
37+
// Set the initial content to something that will produce format edits.
38+
await handleSuccessfulRequest(
39+
AnalysisUpdateContentParams({
40+
testFile.path: AddContentOverlay(initialContentNeedsFormatting),
41+
}).toRequest('1', clientUriConverter: server.uriConverter),
42+
);
43+
// Update the content to something that will not produce edits, but do not
44+
// await, because we are testing that the server is consistent even if it
45+
// doesn't have time to handle the change.
46+
unawaited(
47+
handleSuccessfulRequest(
48+
AnalysisUpdateContentParams({
49+
testFile.path: AddContentOverlay(updatedContentNeedsNoFormatting),
50+
}).toRequest('2', clientUriConverter: server.uriConverter),
51+
),
52+
);
53+
var formatResult = await _formatAt(0, 0);
54+
55+
// Expect no edits, because the last overlay was already formatted.
56+
expect(formatResult.edits, hasLength(0));
57+
}
58+
2659
Future<void> test_format_longLine_analysisOptions() async {
2760
writeTestPackageAnalysisOptionsFile(r'''
2861
formatter:

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,8 @@ class AnalysisDriver {
11611161
return InvalidPathResult();
11621162
}
11631163

1164+
_applyPendingFileChanges();
1165+
11641166
var file = _fsState.getFileForPath(path);
11651167
RecordingErrorListener listener = RecordingErrorListener();
11661168
CompilationUnit unit = file.parse(

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3921,6 +3921,25 @@ import 'b.dart';
39213921
]);
39223922
}
39233923

3924+
test_parseFileSync_appliesPendingFileChanges() async {
3925+
var initialContent = 'initial content';
3926+
var updatedContent = 'updated content';
3927+
var a = newFile('$testPackageLibPath/a.dart', initialContent);
3928+
3929+
// Check initial content.
3930+
var driver = driverFor(testFile);
3931+
var parsed = driver.parseFileSync(a.path) as ParsedUnitResult;
3932+
expect(parsed.content, initialContent);
3933+
3934+
// Update the file.
3935+
newFile(a.path, updatedContent);
3936+
driver.changeFile(a.path);
3937+
3938+
// Expect parseFileSync to return the updated content.
3939+
parsed = driver.parseFileSync(a.path) as ParsedUnitResult;
3940+
expect(parsed.content, updatedContent);
3941+
}
3942+
39243943
test_parseFileSync_changedFile() async {
39253944
var a = newFile('$testPackageLibPath/a.dart', r'''
39263945
''');
@@ -3990,14 +4009,6 @@ class A {}
39904009
// Notify the driver that `a` was changed.
39914010
driver.changeFile2(a);
39924011

3993-
// Pending changes are no applied yes, so `a` is empty.
3994-
{
3995-
var result = driver.parseFileSync2(a) as ParsedUnitResult;
3996-
assertParsedNodeText(result.unit, r'''
3997-
CompilationUnit
3998-
''');
3999-
}
4000-
40014012
// The pending change to `a` declares `A`.
40024013
// So, `b` does not have errors anymore.
40034014
collector.getResolvedUnit('B2', b);

0 commit comments

Comments
 (0)