Skip to content

Commit e4b43bf

Browse files
committed
One identifier rule for both halves of a label
The separate target pattern was wrong: package components need the dot too. `flutter/third_party/swiftshader/third_party/llvm-16.0` comes in through DEPS and defines `swiftshader_llvm`, so a label pointing at it was rejected before this change and after it as well. The dot goes into `_identifier`, `_targetName` is gone, and both error messages share one sentence describing the rule. The leading letter or underscore still keeps `.`, `..` and `...` out of package names, which matters because `TargetPattern` strips only a trailing `/...` before handing the rest to `Label`.
1 parent 5b867c7 commit e4b43bf

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

‎engine/src/flutter/tools/engine_tool/lib/src/label.dart‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import 'package:meta/meta.dart';
1515
///
1616
/// Unlike counterparts in Bazel and GN:
1717
/// - The package name is always a source-absolute path (i.e. starts with `//`).
18-
/// - Valid package identifier characters are `a-zA-Z0-9_-`, not starting with
19-
/// a digit; target names additionally allow `.`.
18+
/// - Valid identifier characters are `a-zA-Z0-9_-.`, not starting with a digit
19+
/// or a dot.
2020
/// - The target name is never empty, even when it is a default target.
2121
@immutable
2222
final class Label {
@@ -79,7 +79,7 @@ final class Label {
7979

8080
/// A target name within the package.
8181
///
82-
/// The target name must be a valid identifier, which may also contain dots.
82+
/// The target name must be a valid identifier.
8383
final String target;
8484

8585
@override
@@ -119,7 +119,7 @@ final class Label {
119119
final int j = package.indexOf('/', i);
120120
final String component = j == -1 ? package.substring(i) : package.substring(i, j);
121121
if (!_identifier.hasMatch(component)) {
122-
return FormatException('Package name component must be a valid identifier.', package, i);
122+
return FormatException('Package name component $_identifierRule', package, i);
123123
}
124124
if (j == -1) {
125125
return null;
@@ -129,22 +129,26 @@ final class Label {
129129
}
130130

131131
static FormatException? _checkTarget(String target) {
132-
if (!_targetName.hasMatch(target)) {
133-
return FormatException(
134-
'Target name must start with a letter or underscore and contain only '
135-
'letters, digits, "_", "-", or ".".',
136-
target,
137-
);
132+
if (!_identifier.hasMatch(target)) {
133+
return FormatException('Target name $_identifierRule', target);
138134
}
139135
return null;
140136
}
141137

142-
static final RegExp _identifier = RegExp(r'^[a-zA-Z_][a-zA-Z0-9_-]*$');
138+
static const String _identifierRule =
139+
'must start with a letter or underscore and contain only letters, '
140+
'digits, "_", "-", or ".".';
143141

144-
/// Target names additionally allow a dot, which GN permits and which the
145-
/// engine's own build files rely on: `testing/dart/BUILD.gn` derives a
146-
/// target per test file, giving names like `compile_gpu_test.dart`.
147-
static final RegExp _targetName = RegExp(r'^[a-zA-Z_][a-zA-Z0-9_.-]*$');
142+
/// Identifiers allow a dot, which both halves of a label need.
143+
///
144+
/// `testing/dart/BUILD.gn` derives a compile target from each test file name,
145+
/// giving target names like `compile_gpu_test.dart`, and directories brought
146+
/// in by `DEPS` carry a dot as well, as in
147+
/// `//flutter/third_party/swiftshader/third_party/llvm-16.0:swiftshader_llvm`.
148+
///
149+
/// The leading letter or underscore keeps `.`, `..` and `...` from passing as
150+
/// a package name component.
151+
static final RegExp _identifier = RegExp(r'^[a-zA-Z_][a-zA-Z0-9_.-]*$');
148152
}
149153

150154
/// A generic target pattern that can be used to match multiple targets.

‎engine/src/flutter/tools/engine_tool/test/label_test.dart‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ void main() {
3030
);
3131
});
3232

33-
test('rejects a package component containing a dot', () {
34-
expect(() => Label.parse('//foo/bar.baz'), throwsFormatException);
33+
test('parses a package component containing a dot', () {
34+
expect(
35+
Label.parse('//flutter/third_party/swiftshader/third_party/llvm-16.0:swiftshader_llvm'),
36+
Label('//flutter/third_party/swiftshader/third_party/llvm-16.0', 'swiftshader_llvm'),
37+
);
38+
});
39+
40+
test('rejects a component that is only dots', () {
41+
expect(() => Label.parse('//foo/../bar'), throwsFormatException);
42+
expect(() => Label.parse('//foo/./bar'), throwsFormatException);
43+
expect(() => Label.parse('//foo/bar:.'), throwsFormatException);
3544
});
3645

3746
test('rejects ending with a slash', () {

0 commit comments

Comments
 (0)