Get the language version of BazelWorkspace from the dart/build_defs/bzl/language.bzl

Presubmit looks green.
https://fusion2.corp.google.com/presubmit/tap/398141496/OCL:398141496:BASE:398160873:1632289516597:2c6df414/targets

Change-Id: Ibe12b4d0709cd38be0307a6c211962dca7a69de6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214075
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov 2021-09-22 15:17:47 +00:00 committed by commit-bot@chromium.org
parent 34f1bddb88
commit fdf5014697
2 changed files with 91 additions and 2 deletions

View file

@ -206,6 +206,9 @@ class BazelWorkspace extends Workspace
/// to avoid this in cases when `BUILD` files are always available.
final bool _lookForBuildFileSubstitutes;
/// The language version for this workspace, `null` if cannot be read.
final Version? _languageVersion;
/// The cache of packages. The key is the directory path, the value is
/// the corresponding package.
final Map<String, BazelWorkspacePackage> _directoryToPackage = {};
@ -220,7 +223,8 @@ class BazelWorkspace extends Workspace
this.binPaths,
this.genfiles, {
required bool lookForBuildFileSubstitutes,
}) : _lookForBuildFileSubstitutes = lookForBuildFileSubstitutes;
}) : _lookForBuildFileSubstitutes = lookForBuildFileSubstitutes,
_languageVersion = _readLanguageVersion(provider, root);
/// Stream of files that we tried to find along with their potential or actual
/// paths.
@ -564,6 +568,33 @@ class BazelWorkspace extends Workspace
static Folder? _firstExistingFolder(Folder root, List<String> names) => names
.map((name) => root.getChildAssumingFolder(name))
.firstWhereOrNull((folder) => folder.exists);
/// Return the default language version of the workspace.
///
/// Return `null` if cannot be read, for example because the file does not
/// exist, or is not available in this build configuration (batch analysis).
static Version? _readLanguageVersion(
ResourceProvider resourceProvider,
String rootPath,
) {
var file = resourceProvider.getFile(
resourceProvider.pathContext.joinAll(
[rootPath, 'dart', 'build_defs', 'bzl', 'language.bzl'],
),
);
String content;
try {
content = file.readAsStringSync();
} on FileSystemException {
return null;
}
final pattern = RegExp(r'_version_null_safety\s*=\s*"(\d+\.\d+)"');
for (var match in pattern.allMatches(content)) {
return Version.parse('${match.group(1)}.0');
}
}
}
/// Information about a package defined in a BazelWorkspace.
@ -597,7 +628,7 @@ class BazelWorkspacePackage extends WorkspacePackage {
@override
Version? get languageVersion {
_readBuildFile();
return _languageVersion;
return _languageVersion ?? workspace._languageVersion;
}
@override

View file

@ -2,6 +2,9 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/dart/analysis/experiments.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'context_collection_resolution.dart';
@ -63,6 +66,52 @@ dart_package(
assertType(findNode.namedType('int v'), 'int');
}
test_buildFile_nonNullable_languageVersion_current() async {
newFile('$myPackageRootPath/BUILD', content: r'''
dart_package(
null_safety = True,
)
''');
await resolveFileCode(
'$myPackageRootPath/lib/a.dart',
'int v = 0;',
);
_assertLanguageVersion(
package: ExperimentStatus.currentVersion,
override: null,
);
}
test_buildFile_nonNullable_languageVersion_fromWorkspace() async {
newFile('$workspaceRootPath/dart/build_defs/bzl/language.bzl', content: r'''
_version = "2.9"
_version_null_safety = "2.14"
_version_for_analyzer = _version_null_safety
language = struct(
version = _version,
version_null_safety = _version_null_safety,
version_for_analyzer = _version_for_analyzer,
)
''');
newFile('$myPackageRootPath/BUILD', content: r'''
dart_package(
null_safety = True,
)
''');
await resolveFileCode(
'$myPackageRootPath/lib/a.dart',
'int v = 0;',
);
_assertLanguageVersion(
package: Version.parse('2.14.0'),
override: null,
);
}
test_buildFile_nonNullable_oneLine_noComma() async {
newFile('$myPackageRootPath/BUILD', content: r'''
dart_package(null_safety = True)
@ -99,4 +148,13 @@ int v = 0;
assertType(findNode.namedType('int v'), 'int*');
}
void _assertLanguageVersion({
required Version package,
required Version? override,
}) async {
var element = result.libraryElement;
expect(element.languageVersion.package, package);
expect(element.languageVersion.override, override);
}
}