@@ -122,7 +122,7 @@ class NonNullableFix {
122
122
123
123
/// Update the pubspec.yaml file to specify a minimum Dart SDK version which
124
124
/// enables the Null Safety feature.
125
- Future < void > processPackage (Folder pkgFolder) async {
125
+ void processPackage (Folder pkgFolder) {
126
126
if (! _packageIsNNBD) {
127
127
return ;
128
128
}
@@ -137,14 +137,17 @@ class NonNullableFix {
137
137
138
138
try {
139
139
pubspecContent = pubspecFile.readAsStringSync ();
140
+ var pubspecYaml = loadYaml (pubspecContent);
141
+ if (pubspecYaml is YamlMap ) {
142
+ pubspecMap = pubspecYaml;
143
+ } else {
144
+ throw FormatException ('pubspec.yaml file is not a YAML map' );
145
+ }
140
146
} on FileSystemException catch (e) {
141
- processYamlException ('read' , pubspecFile.path, e);
147
+ _processPubspecException ('read' , pubspecFile.path, e);
142
148
return ;
143
- }
144
- try {
145
- pubspecMap = loadYaml (pubspecContent) as YamlNode ;
146
- } on YamlException catch (e) {
147
- processYamlException ('parse' , pubspecFile.path, e);
149
+ } on FormatException catch (e) {
150
+ _processPubspecException ('parse' , pubspecFile.path, e);
148
151
return ;
149
152
}
150
153
@@ -202,17 +205,25 @@ environment:
202
205
sdk: '$_intendedSdkVersionConstraint '""" ;
203
206
insertAfterParent (environmentOptions.span, content);
204
207
} else if (sdk is YamlScalar ) {
205
- var currentConstraint = VersionConstraint .parse (sdk.value as String );
206
- var minimumVersion = Version .parse (_intendedMinimumSdkVersion);
207
- if (currentConstraint is VersionRange &&
208
- currentConstraint.min >= minimumVersion) {
209
- // The current SDK version constraint already enables Null Safety.
210
- return ;
208
+ VersionConstraint currentConstraint;
209
+ if (sdk.value is String ) {
210
+ currentConstraint = VersionConstraint .parse (sdk.value as String );
211
+ var minimumVersion = Version .parse (_intendedMinimumSdkVersion);
212
+ if (currentConstraint is VersionRange &&
213
+ currentConstraint.min >= minimumVersion) {
214
+ // The current SDK version constraint already enables Null Safety.
215
+ // Do not edit pubspec.yaml.
216
+ return ;
217
+ } else {
218
+ // TODO(srawlins): This overwrites the current maximum version. In
219
+ // the uncommon situation that the maximum is not '<3.0.0', it
220
+ // should not.
221
+ replaceSpan (sdk.span, "'$_intendedSdkVersionConstraint '" );
222
+ }
211
223
} else {
212
- // TODO(srawlins): This overwrites the current maximum version. In
213
- // the uncommon situation that the maximum is not '<3.0.0', it should
214
- // not.
215
- replaceSpan (sdk.span, "'$_intendedSdkVersionConstraint '" );
224
+ // Something is odd with the SDK constraint we've found in
225
+ // pubspec.yaml; Best to leave it alone.
226
+ return ;
216
227
}
217
228
}
218
229
}
@@ -237,20 +248,6 @@ environment:
237
248
}
238
249
}
239
250
240
- void processYamlException (String action, String optionsFilePath, error) {
241
- listener.addRecommendation ('''Failed to $action options file
242
- $optionsFilePath
243
- $error
244
-
245
- Manually update this file to enable the Null Safety language feature by
246
- adding:
247
-
248
- environment:
249
- sdk: '$_intendedSdkVersionConstraint ';
250
- ''' );
251
- _packageIsNNBD = false ;
252
- }
253
-
254
251
Future <MigrationState > rerun () async {
255
252
reset ();
256
253
await rerunFunction ();
@@ -275,6 +272,20 @@ environment:
275
272
_server? .close ();
276
273
}
277
274
275
+ void _processPubspecException (String action, String pubspecPath, error) {
276
+ listener.addRecommendation ('''Failed to $action pubspec file
277
+ $pubspecPath
278
+ $error
279
+
280
+ Manually update this file to enable the Null Safety language feature by
281
+ adding:
282
+
283
+ environment:
284
+ sdk: '$_intendedSdkVersionConstraint ';
285
+ ''' );
286
+ _packageIsNNBD = false ;
287
+ }
288
+
278
289
/// Allows unit tests to shut down any rogue servers that have been started,
279
290
/// so that unit testing can complete.
280
291
@visibleForTesting
0 commit comments