Unverified Commit 35bd77bb authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] separate concept of null safe language version from current...

[flutter_tools] separate concept of null safe language version from current language version (#79052)
parent 971881c8
...@@ -14,6 +14,7 @@ import '../../artifacts.dart'; ...@@ -14,6 +14,7 @@ import '../../artifacts.dart';
import '../../base/file_system.dart'; import '../../base/file_system.dart';
import '../../base/io.dart'; import '../../base/io.dart';
import '../../build_info.dart'; import '../../build_info.dart';
import '../../cache.dart';
import '../../dart/language_version.dart'; import '../../dart/language_version.dart';
import '../../dart/package_map.dart'; import '../../dart/package_map.dart';
import '../../globals.dart' as globals; import '../../globals.dart' as globals;
...@@ -103,6 +104,7 @@ class WebEntrypointTarget extends Target { ...@@ -103,6 +104,7 @@ class WebEntrypointTarget extends Target {
final LanguageVersion languageVersion = determineLanguageVersion( final LanguageVersion languageVersion = determineLanguageVersion(
environment.fileSystem.file(targetFile), environment.fileSystem.file(targetFile),
packageConfig[flutterProject.manifest.appName], packageConfig[flutterProject.manifest.appName],
Cache.flutterRoot,
); );
// Use the PackageConfig to find the correct package-scheme import path // Use the PackageConfig to find the correct package-scheme import path
......
...@@ -2,9 +2,13 @@ ...@@ -2,9 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart'; import 'package:file/file.dart';
import 'package:package_config/package_config.dart'; import 'package:package_config/package_config.dart';
import '../base/version.dart';
final RegExp _languageVersion = RegExp(r'\/\/\s*@dart\s*=\s*([0-9])\.([0-9]+)'); final RegExp _languageVersion = RegExp(r'\/\/\s*@dart\s*=\s*([0-9])\.([0-9]+)');
final RegExp _declarationEnd = RegExp('(import)|(library)|(part)'); final RegExp _declarationEnd = RegExp('(import)|(library)|(part)');
const String _blockCommentStart = '/*'; const String _blockCommentStart = '/*';
...@@ -13,6 +17,28 @@ const String _blockCommentEnd = '*/'; ...@@ -13,6 +17,28 @@ const String _blockCommentEnd = '*/';
/// The first language version where null safety was available by default. /// The first language version where null safety was available by default.
final LanguageVersion nullSafeVersion = LanguageVersion(2, 12); final LanguageVersion nullSafeVersion = LanguageVersion(2, 12);
LanguageVersion? _currentLanguageVersion;
/// Lookup the current Dart language version.
LanguageVersion currentLanguageVersion(FileSystem fileSystem, String flutterRoot) {
if (_currentLanguageVersion != null) {
return _currentLanguageVersion!;
}
// Either reading the file or parsing the version could fail on a corrupt Dart SDK.
// let it crash so it shows up in crash logging.
final File versionFile = fileSystem.file(fileSystem.path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'version'));
if (!versionFile.existsSync() && _inUnitTest()) {
return LanguageVersion(2, 12);
}
final Version version = Version.parse(versionFile.readAsStringSync())!;
return _currentLanguageVersion = LanguageVersion(version.major, version.minor);
}
// Whether the tool is executing in a unit test.
bool _inUnitTest() {
return Zone.current[#test.declarer] != null;
}
/// Attempts to read the language version of a dart [file]. /// Attempts to read the language version of a dart [file].
/// ///
/// If this is not present, falls back to the language version defined in /// If this is not present, falls back to the language version defined in
...@@ -22,7 +48,7 @@ final LanguageVersion nullSafeVersion = LanguageVersion(2, 12); ...@@ -22,7 +48,7 @@ final LanguageVersion nullSafeVersion = LanguageVersion(2, 12);
/// ///
/// The specification for the language version tag is defined at: /// The specification for the language version tag is defined at:
/// https://github.com/dart-lang/language/blob/master/accepted/future-releases/language-versioning/feature-specification.md#individual-library-language-version-override /// https://github.com/dart-lang/language/blob/master/accepted/future-releases/language-versioning/feature-specification.md#individual-library-language-version-override
LanguageVersion determineLanguageVersion(File file, Package package) { LanguageVersion determineLanguageVersion(File file, Package package, String flutterRoot) {
int blockCommentDepth = 0; int blockCommentDepth = 0;
// If reading the file fails, default to a null-safe version. The // If reading the file fails, default to a null-safe version. The
// command will likely fail later in the process with a better error // command will likely fail later in the process with a better error
...@@ -31,7 +57,7 @@ LanguageVersion determineLanguageVersion(File file, Package package) { ...@@ -31,7 +57,7 @@ LanguageVersion determineLanguageVersion(File file, Package package) {
try { try {
lines = file.readAsLinesSync(); lines = file.readAsLinesSync();
} on FileSystemException { } on FileSystemException {
return nullSafeVersion; return currentLanguageVersion(file.fileSystem, flutterRoot);
} }
for (final String line in lines) { for (final String line in lines) {
...@@ -84,8 +110,8 @@ LanguageVersion determineLanguageVersion(File file, Package package) { ...@@ -84,8 +110,8 @@ LanguageVersion determineLanguageVersion(File file, Package package) {
// If the language version cannot be found, use the package version. // If the language version cannot be found, use the package version.
if (package != null) { if (package != null) {
return package.languageVersion ?? nullSafeVersion; return package.languageVersion ?? currentLanguageVersion(file.fileSystem, flutterRoot);
} }
// Default to 2.12 // Default to current version.
return nullSafeVersion; return currentLanguageVersion(file.fileSystem, flutterRoot);
} }
...@@ -25,6 +25,7 @@ import '../base/time.dart'; ...@@ -25,6 +25,7 @@ import '../base/time.dart';
import '../base/utils.dart'; import '../base/utils.dart';
import '../build_info.dart'; import '../build_info.dart';
import '../build_system/targets/web.dart'; import '../build_system/targets/web.dart';
import '../cache.dart';
import '../dart/language_version.dart'; import '../dart/language_version.dart';
import '../devfs.dart'; import '../devfs.dart';
import '../device.dart'; import '../device.dart';
...@@ -678,6 +679,7 @@ class ResidentWebRunner extends ResidentRunner { ...@@ -678,6 +679,7 @@ class ResidentWebRunner extends ResidentRunner {
final LanguageVersion languageVersion = determineLanguageVersion( final LanguageVersion languageVersion = determineLanguageVersion(
_fileSystem.file(mainUri), _fileSystem.file(mainUri),
packageConfig[flutterProject.manifest.appName], packageConfig[flutterProject.manifest.appName],
Cache.flutterRoot,
); );
final String entrypoint = <String>[ final String entrypoint = <String>[
......
...@@ -892,6 +892,7 @@ abstract class FlutterCommand extends Command<void> { ...@@ -892,6 +892,7 @@ abstract class FlutterCommand extends Command<void> {
final LanguageVersion languageVersion = determineLanguageVersion( final LanguageVersion languageVersion = determineLanguageVersion(
entrypointFile, entrypointFile,
packageConfig.packageOf(entrypointFile.absolute.uri), packageConfig.packageOf(entrypointFile.absolute.uri),
Cache.flutterRoot,
); );
// Extra frontend options are only provided if explicitly // Extra frontend options are only provided if explicitly
// requested. // requested.
......
...@@ -14,6 +14,7 @@ import 'package:test_core/src/platform.dart'; // ignore: implementation_imports ...@@ -14,6 +14,7 @@ import 'package:test_core/src/platform.dart'; // ignore: implementation_imports
import '../base/common.dart'; import '../base/common.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/io.dart'; import '../base/io.dart';
import '../cache.dart';
import '../compile.dart'; import '../compile.dart';
import '../convert.dart'; import '../convert.dart';
import '../dart/language_version.dart'; import '../dart/language_version.dart';
...@@ -512,6 +513,7 @@ class FlutterPlatform extends PlatformPlugin { ...@@ -512,6 +513,7 @@ class FlutterPlatform extends PlatformPlugin {
final LanguageVersion languageVersion = determineLanguageVersion( final LanguageVersion languageVersion = determineLanguageVersion(
file, file,
packageConfig[flutterProject?.manifest?.appName], packageConfig[flutterProject?.manifest?.appName],
Cache.flutterRoot,
); );
return generateTestBootstrap( return generateTestBootstrap(
testUrl: testUrl, testUrl: testUrl,
......
...@@ -16,6 +16,7 @@ import '../base/logger.dart'; ...@@ -16,6 +16,7 @@ import '../base/logger.dart';
import '../base/platform.dart'; import '../base/platform.dart';
import '../build_info.dart'; import '../build_info.dart';
import '../bundle.dart'; import '../bundle.dart';
import '../cache.dart';
import '../compile.dart'; import '../compile.dart';
import '../dart/language_version.dart'; import '../dart/language_version.dart';
import '../web/bootstrap.dart'; import '../web/bootstrap.dart';
...@@ -63,7 +64,7 @@ class WebTestCompiler { ...@@ -63,7 +64,7 @@ class WebTestCompiler {
} }
} else if (buildInfo.nullSafetyMode == NullSafetyMode.sound) { } else if (buildInfo.nullSafetyMode == NullSafetyMode.sound) {
platformDillArtifact = Artifact.webPlatformSoundKernelDill; platformDillArtifact = Artifact.webPlatformSoundKernelDill;
languageVersion = nullSafeVersion; languageVersion = currentLanguageVersion(_fileSystem, Cache.flutterRoot);
if (!extraFrontEndOptions.contains('--sound-null-safety')) { if (!extraFrontEndOptions.contains('--sound-null-safety')) {
extraFrontEndOptions.add('--sound-null-safety'); extraFrontEndOptions.add('--sound-null-safety');
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment