Skip to content

Commit 8833210

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
dart migrate: Only opt out files with a comment if they appear in files-to-process
Change-Id: I3cb1bedaa4a03eca89e29528c554488955f2bb04 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180083 Reviewed-by: Paul Berry <paulberry@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
1 parent 05d46af commit 8833210

File tree

6 files changed

+90
-18
lines changed

6 files changed

+90
-18
lines changed

pkg/nnbd_migration/lib/migration_cli.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,10 @@ class MigrationCliRunner implements DartFixListenerClient {
591591
Set<String> computePathsToProcess(DriverBasedAnalysisContext context) =>
592592
context.contextRoot
593593
.analyzedFiles()
594-
.where((s) => s.endsWith('.dart'))
594+
.where((s) =>
595+
s.endsWith('.dart') &&
596+
// Any file may have been deleted since its initial analysis.
597+
resourceProvider.getFile(s).exists)
595598
.toSet();
596599

597600
NonNullableFix createNonNullableFix(
@@ -1159,6 +1162,8 @@ class _FixCodeProcessor extends Object {
11591162
_progressBar.complete();
11601163
_migrationCli.logger.stdout(_migrationCli.ansi
11611164
.emphasized('Compiling instrumentation information...'));
1165+
// Update the tasks paths-to-process, in case of new or deleted files.
1166+
_task.pathsToProcess = pathsToProcess;
11621167
var state = await _task.finish();
11631168
_task.processPackage(context.contextRoot.root, state.neededPackages);
11641169
if (_migrationCli.options.webPreview) {

pkg/nnbd_migration/lib/src/front_end/info_builder.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class InfoBuilder {
5959
/// migrated.
6060
final bool Function(String) shouldBeMigratedFunction;
6161

62+
/// The set of files which are being considered for migration.
63+
final Iterable<String> _pathsToProcess;
64+
6265
/// Initialize a newly created builder.
6366
InfoBuilder(
6467
this.provider,
@@ -68,7 +71,8 @@ class InfoBuilder {
6871
this.migration,
6972
this.nodeMapper,
7073
this._logger,
71-
this.shouldBeMigratedFunction);
74+
this.shouldBeMigratedFunction,
75+
this._pathsToProcess);
7276

7377
/// The provider used to get information about libraries.
7478
DriverProviderImpl get driverProvider => listener.server;
@@ -79,11 +83,18 @@ class InfoBuilder {
7983
var sourceInfoMap = info.sourceInformation;
8084
Set<UnitInfo> units =
8185
SplayTreeSet<UnitInfo>((u1, u2) => u1.path.compareTo(u2.path));
82-
var progressBar = ProgressBar(_logger, sourceInfoMap.length);
8386

84-
for (var source in sourceInfoMap.keys) {
87+
// Collect all of the sources for which we have [SourceInformation], as well
88+
// as all files which are being "processed" during this migration, which may
89+
// include already migrated files.
90+
var sources = {
91+
...sourceInfoMap.keys.map((source) => source.fullName),
92+
..._pathsToProcess,
93+
};
94+
var progressBar = ProgressBar(_logger, sources.length);
95+
96+
for (var filePath in sources) {
8597
progressBar.tick();
86-
var filePath = source.fullName;
8798
var session = driverProvider.getAnalysisSession(filePath);
8899
if (!session.getFile(filePath).isPart) {
89100
var result = await session.getResolvedLibrary(filePath);
@@ -102,7 +113,7 @@ class InfoBuilder {
102113
sourceInfo ??= SourceInformation();
103114
var edit = listener.sourceChange.getFileEdit(unitResult.path);
104115
var unit = _explainUnit(sourceInfo, unitResult, edit);
105-
if (provider.pathContext.isWithin(includedPath, unitResult.path)) {
116+
if (_pathsToProcess.contains(unitResult.path)) {
106117
units.add(unit);
107118
}
108119
}

pkg/nnbd_migration/lib/src/front_end/migration_state.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MigrationState {
7777
}
7878

7979
/// Refresh the state of the migration after the migration has been updated.
80-
Future<void> refresh(Logger logger) async {
80+
Future<void> refresh(Logger logger, Iterable<String> pathsToProcess) async {
8181
assert(!hasBeenApplied);
8282
var provider = listener.server.resourceProvider;
8383
var infoBuilder = InfoBuilder(
@@ -88,7 +88,8 @@ class MigrationState {
8888
migration,
8989
nodeMapper,
9090
logger,
91-
shouldBeMigratedFunction);
91+
shouldBeMigratedFunction,
92+
pathsToProcess);
9293
var unitInfos = await infoBuilder.explainMigration();
9394
var pathContext = provider.pathContext;
9495
migrationInfo = MigrationInfo(

pkg/nnbd_migration/lib/src/front_end/non_nullable_fix.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class NonNullableFix {
7979
/// migrated.
8080
final bool Function(String) shouldBeMigratedFunction;
8181

82+
/// The set of files which are being considered for migration.
83+
Iterable<String> pathsToProcess;
84+
8285
/// Completes when the server has been shutdown.
8386
Completer<void> serverIsShutdown;
8487

@@ -116,7 +119,7 @@ class NonNullableFix {
116119
var neededPackages = migration.finish();
117120
final state = MigrationState(migration, includedRoot, listener,
118121
instrumentationListener, neededPackages, shouldBeMigratedFunction);
119-
await state.refresh(_logger);
122+
await state.refresh(_logger, pathsToProcess);
120123
return state;
121124
}
122125

pkg/nnbd_migration/test/front_end/nnbd_migration_test_base.dart

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
159159
var includedRoot = resourceProvider.pathContext.dirname(testFile);
160160
await _buildMigrationInfo([testFile],
161161
includedRoot: includedRoot,
162+
shouldBeMigratedFunction: (String path) => true,
163+
pathsToProcess: [testFile],
162164
removeViaComments: removeViaComments,
163165
warnOnWeakCode: warnOnWeakCode);
164166
}
@@ -190,13 +192,20 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
190192
///
191193
/// Returns the singular [UnitInfo] which was built.
192194
Future<List<UnitInfo>> buildInfoForTestFiles(Map<String, String> files,
193-
{String includedRoot}) async {
195+
{@required String includedRoot,
196+
bool Function(String) shouldBeMigratedFunction,
197+
Iterable<String> pathsToProcess}) async {
198+
shouldBeMigratedFunction ??= (String path) => true;
194199
var testPaths = <String>[];
195200
files.forEach((String path, String content) {
196201
newFile(path, content: content);
197202
testPaths.add(path);
198203
});
199-
await _buildMigrationInfo(testPaths, includedRoot: includedRoot);
204+
pathsToProcess ??= testPaths;
205+
await _buildMigrationInfo(testPaths,
206+
includedRoot: includedRoot,
207+
shouldBeMigratedFunction: shouldBeMigratedFunction,
208+
pathsToProcess: pathsToProcess);
200209
// Ignore info for dart:core.
201210
var filteredInfos = [
202211
for (var info in infos)
@@ -212,8 +221,10 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
212221

213222
/// Uses the InfoBuilder to build information for files at [testPaths], which
214223
/// should all share a common parent directory, [includedRoot].
215-
Future<void> _buildMigrationInfo(List<String> testPaths,
216-
{String includedRoot,
224+
Future<void> _buildMigrationInfo(Iterable<String> testPaths,
225+
{@required String includedRoot,
226+
@required bool Function(String) shouldBeMigratedFunction,
227+
@required Iterable<String> pathsToProcess,
217228
bool removeViaComments = true,
218229
bool warnOnWeakCode = false}) async {
219230
// Compute the analysis results.
@@ -243,8 +254,16 @@ class NnbdMigrationTestBase extends AbstractAnalysisTest {
243254
// Build the migration info.
244255
var info = instrumentationListener.data;
245256
var logger = TestLogger(false);
246-
var builder = InfoBuilder(resourceProvider, includedRoot, info, listener,
247-
migration, nodeMapper, logger, (String path) => true);
257+
var builder = InfoBuilder(
258+
resourceProvider,
259+
includedRoot,
260+
info,
261+
listener,
262+
migration,
263+
nodeMapper,
264+
logger,
265+
shouldBeMigratedFunction,
266+
pathsToProcess);
248267
infos = await builder.explainMigration();
249268
}
250269
}

pkg/nnbd_migration/test/preview/preview_site_test.dart

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,20 @@ class PreviewSiteWithEngineTest extends NnbdMigrationTestBase
269269
with ResourceProviderMixin, PreviewSiteTestMixin {
270270
MigrationInfo migrationInfo;
271271

272-
Future<void> setUpMigrationInfo(Map<String, String> files) async {
273-
await buildInfoForTestFiles(files, includedRoot: projectPath);
272+
Future<void> setUpMigrationInfo(Map<String, String> files,
273+
{bool Function(String) shouldBeMigratedFunction,
274+
Iterable<String> pathsToProcess}) async {
275+
shouldBeMigratedFunction ??= (String path) => true;
276+
pathsToProcess ??= files.keys;
277+
await buildInfoForTestFiles(files,
278+
includedRoot: projectPath,
279+
shouldBeMigratedFunction: shouldBeMigratedFunction,
280+
pathsToProcess: pathsToProcess);
274281
dartfixListener = DartFixListener(null, ListenerClient());
275282
migrationInfo =
276283
MigrationInfo(infos, {}, resourceProvider.pathContext, projectPath);
277284
state = MigrationState(
278-
null, null, dartfixListener, null, {}, (String path) => true);
285+
null, null, dartfixListener, null, {}, shouldBeMigratedFunction);
279286
nodeMapper = state.nodeMapper;
280287
state.pathMapper = PathMapper(resourceProvider);
281288
state.migrationInfo = migrationInfo;
@@ -469,6 +476,32 @@ Opted 1 file out of null safety with a new Dart language version comment:
469476
'''));
470477
}
471478

479+
void test_applyMigration_doNotOptOutFileNotInPathsToProcess() async {
480+
final pathA = convertPath('$projectPath/lib/a.dart');
481+
final pathB = convertPath('$projectPath/lib/b.dart');
482+
final content = 'void main() {}';
483+
await setUpMigrationInfo({pathA: content, pathB: content},
484+
// Neither [pathA] nor [[pathB] should be migrated.
485+
shouldBeMigratedFunction: (String path) => false,
486+
pathsToProcess: [pathA]);
487+
site.unitInfoMap[pathA] = UnitInfo(pathA)
488+
..diskContent = content
489+
..wasExplicitlyOptedOut = false
490+
..migrationStatus = UnitMigrationStatus.optingOut;
491+
site.unitInfoMap[pathB] = UnitInfo(pathB)
492+
..diskContent = content
493+
..wasExplicitlyOptedOut = false
494+
..migrationStatus = UnitMigrationStatus.optingOut;
495+
var navigationTree =
496+
NavigationTreeRenderer(migrationInfo, state.pathMapper).render();
497+
site.performApply(navigationTree);
498+
expect(getFile(pathA).readAsStringSync(), '''
499+
// @dart=2.9
500+
501+
void main() {}''');
502+
expect(getFile(pathB).readAsStringSync(), 'void main() {}');
503+
}
504+
472505
void test_applyMigration_optOutOne_migrateAnother() async {
473506
final pathA = convertPath('$projectPath/lib/a.dart');
474507
final pathB = convertPath('$projectPath/lib/b.dart');

0 commit comments

Comments
 (0)