Skip to content

Commit b8fe6e0

Browse files
committed
[flutter_tools] Enforce file scheme in CleanEnvironment and log extension name on clean failure
Enforce that `CleanEnvironment` directory URIs use the `file:` scheme in `_parseDirectoryUri` and include `ExtensionConnection.extensionName` in `ExtensionCleanManager.cleanProject` error messages. Part of flutter#190755
1 parent a6598e1 commit b8fe6e0

8 files changed

Lines changed: 76 additions & 21 deletions

File tree

‎packages/flutter_tools/lib/src/experimental/extension_clean_manager.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class ExtensionCleanManager {
6666
timeout: null,
6767
);
6868
} on Object catch (e) {
69-
_logger.printError('Failed to clean extension artifacts: $e');
69+
_logger.printError(
70+
'Failed to clean extension artifacts for "${connection.extensionName}": $e',
71+
);
7072
}
7173
}),
7274
);

‎packages/flutter_tools/lib/src/experimental/extension_discovery.dart‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ typedef ExtensionEntryPoint = void Function(SendPort sendPort);
1717
/// Represents an active host-side connection to a running tool extension isolate.
1818
class ExtensionConnection {
1919
ExtensionConnection._({
20-
required Isolate this._isolate,
21-
required this._peer,
2220
required this.capabilities,
21+
required this.extensionName,
22+
required Isolate this._isolate,
2323
required this._logger,
24+
required this._peer,
2425
});
2526

2627
Isolate? _isolate;
@@ -30,6 +31,9 @@ class ExtensionConnection {
3031
/// The capabilities and supported service namespaces of the extension.
3132
final ToolExtensionCapabilities capabilities;
3233

34+
/// The resolved package or extension name of the connected extension.
35+
final String extensionName;
36+
3337
bool _isDisposed = false;
3438

3539
/// Sends an RPC request to the extension isolate.
@@ -57,6 +61,7 @@ class ExtensionConnection {
5761
static Future<ExtensionConnection> spawn(
5862
ExtensionEntryPoint entryPoint, {
5963
required Logger logger,
64+
String? packageName,
6065
Duration timeout = const Duration(seconds: 5),
6166
}) async {
6267
logger.printTrace('ExtensionConnection spawning extension isolate...');
@@ -114,10 +119,11 @@ class ExtensionConnection {
114119
);
115120

116121
return ExtensionConnection._(
117-
isolate: isolate,
118-
peer: peer,
119122
capabilities: capabilities,
123+
extensionName: capabilities.extensionName ?? packageName ?? 'unknown_extension',
124+
isolate: isolate,
120125
logger: logger,
126+
peer: peer,
121127
);
122128
} on TimeoutException {
123129
logger.printTrace('ExtensionConnection handshake timed out.');

‎packages/flutter_tools/packages/flutter_tools_core/lib/src/clean.dart‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,29 @@ class CleanEnvironment {
2020
/// Deserializes a [CleanEnvironment] from a JSON-serializable map.
2121
///
2222
/// Throws a [FormatException] if required fields are missing or are not
23-
/// valid trailing-slash-terminated absolute directory URIs.
23+
/// valid trailing-slash-terminated absolute `file:` directory URIs.
2424
factory CleanEnvironment.fromJson(Map<String, Object?> json) {
2525
if (json case {
2626
projectRootKey: final String projectRootStr,
2727
buildDirKey: final String buildDirStr,
2828
}) {
29-
final Uri? projectRootUri = Uri.tryParse(projectRootStr);
30-
final Uri? buildDirUri = Uri.tryParse(buildDirStr);
31-
if (projectRootUri != null &&
32-
projectRootUri.hasAbsolutePath &&
33-
projectRootUri.path.endsWith('/') &&
34-
buildDirUri != null &&
35-
buildDirUri.hasAbsolutePath &&
36-
buildDirUri.path.endsWith('/')) {
29+
final Uri? projectRootUri = _parseDirectoryUri(projectRootStr);
30+
final Uri? buildDirUri = _parseDirectoryUri(buildDirStr);
31+
if (projectRootUri != null && buildDirUri != null) {
3732
return CleanEnvironment(buildDir: buildDirUri, projectRoot: projectRootUri);
3833
}
3934
}
4035
throw FormatException('Invalid CleanEnvironment JSON: $json');
4136
}
4237

38+
static Uri? _parseDirectoryUri(String rawUri) {
39+
final Uri? uri = Uri.tryParse(rawUri);
40+
if (uri != null && uri.isScheme('file') && uri.hasAbsolutePath && uri.path.endsWith('/')) {
41+
return uri;
42+
}
43+
return null;
44+
}
45+
4346
/// Map key for [buildDir].
4447
static const String buildDirKey = 'buildDir';
4548

‎packages/flutter_tools/packages/flutter_tools_core/test/clean_test.dart‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,20 @@ void main() {
8787
}),
8888
throwsFormatException,
8989
);
90+
expect(
91+
() => CleanEnvironment.fromJson(const <String, Object?>{
92+
'projectRoot': 'http://example.com/dir/',
93+
'buildDir': 'file:///path/to/project/build/',
94+
}),
95+
throwsFormatException,
96+
);
97+
expect(
98+
() => CleanEnvironment.fromJson(const <String, Object?>{
99+
'projectRoot': 'file:///path/to/project/',
100+
'buildDir': 'http://example.com/dir/',
101+
}),
102+
throwsFormatException,
103+
);
90104
});
91105
});
92106
}

‎packages/flutter_tools/packages/flutter_tools_extension/lib/src/protocol_base/provider.dart‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class ToolExtensionEntryPoint {
2020
static Future<void> run(
2121
SendPort sendPort,
2222
List<ToolExtensionService> services, {
23-
Set<String>? supportedPlatforms,
23+
String? extensionName,
2424
void Function(String message)? logger,
25+
Set<String>? supportedPlatforms,
2526
}) async {
2627
logger?.call('[ToolExtensionIsolate] Initializing isolate channel...');
2728
final channel = IsolateChannel<Object?>.connectSend(sendPort);
@@ -45,6 +46,7 @@ class ToolExtensionEntryPoint {
4546
}
4647

4748
final capabilities = ToolExtensionCapabilities(
49+
extensionName: extensionName,
4850
supportedPlatforms: supportedPlatforms ?? const <String>{'linux', 'macos', 'windows'},
4951
services: services.map((ToolExtensionService s) => s.namespace).toList(),
5052
);

‎packages/flutter_tools/packages/flutter_tools_extension/lib/src/protocol_base/service.dart‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,41 @@ class ToolExtensionCapabilities {
3232
/// Creates [ToolExtensionCapabilities] listing supported [services] and [supportedPlatforms].
3333
const ToolExtensionCapabilities({
3434
required this.services,
35+
this.extensionName,
3536
this.supportedPlatforms = const <String>{'linux', 'macos', 'windows'},
3637
});
3738

3839
/// Deserializes [ToolExtensionCapabilities] from a JSON map payload.
3940
factory ToolExtensionCapabilities.fromJson(Map<String, Object?> json) {
40-
final Object? servicesList = json['services'];
41+
final Object? servicesList = json[servicesKey];
4142
final services = servicesList is Iterable ? List<String>.from(servicesList) : <String>[];
42-
final Object? platformsList = json['supportedPlatforms'];
43+
final Object? platformsList = json[supportedPlatformsKey];
4344
final Set<String> supportedPlatforms = platformsList is Iterable
4445
? platformsList.map((Object? p) => p.toString().toLowerCase()).toSet()
4546
: const <String>{'linux', 'macos', 'windows'};
46-
return ToolExtensionCapabilities(services: services, supportedPlatforms: supportedPlatforms);
47+
final String? extensionName = switch (json[extensionNameKey]) {
48+
final String name => name,
49+
_ => null,
50+
};
51+
return ToolExtensionCapabilities(
52+
services: services,
53+
extensionName: extensionName,
54+
supportedPlatforms: supportedPlatforms,
55+
);
4756
}
4857

58+
/// Map key for [extensionName].
59+
static const String extensionNameKey = 'extensionName';
60+
61+
/// Map key for [services].
62+
static const String servicesKey = 'services';
63+
64+
/// Map key for [supportedPlatforms].
65+
static const String supportedPlatformsKey = 'supportedPlatforms';
66+
67+
/// Optional identifier name of the extension package.
68+
final String? extensionName;
69+
4970
/// The list of service namespace identifiers supported by the extension.
5071
final List<String> services;
5172

@@ -59,7 +80,8 @@ class ToolExtensionCapabilities {
5980

6081
/// Serializes capabilities to a map payload.
6182
Map<String, Object?> toMap() => <String, Object?>{
62-
'services': services,
63-
'supportedPlatforms': supportedPlatforms.toList(),
83+
extensionNameKey: ?extensionName,
84+
servicesKey: services,
85+
supportedPlatformsKey: supportedPlatforms.toList(),
6486
};
6587
}

‎packages/flutter_tools/packages/flutter_tools_extension_linux_prototype/lib/flutter_tools_extension_linux_prototype.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void linuxExtensionEntryPoint(SendPort sendPort) {
2828
LinuxBuildService(),
2929
LinuxCleanService(),
3030
],
31+
extensionName: 'flutter_tools_extension_linux_prototype',
3132
supportedPlatforms: const <String>{'linux'},
3233
logger: (String message) {
3334
// ignore: avoid_print

‎packages/flutter_tools/test/general.shard/extension_protocol/extension_clean_manager_test.dart‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ void main() {
159159
throw StateError('Simulated extension clean failure');
160160
}),
161161
],
162+
extensionName: 'failing_clean_extension',
162163
supportedPlatforms: const <String>{'linux'},
163164
);
164165
}
@@ -171,6 +172,7 @@ void main() {
171172
verifySendPort.send(env.toMap());
172173
}),
173174
],
175+
extensionName: 'succeeding_clean_extension',
174176
supportedPlatforms: const <String>{'linux'},
175177
);
176178
}
@@ -195,7 +197,10 @@ void main() {
195197
final Directory buildDir = projectDir.childDirectory('build');
196198

197199
await cleanManager.cleanProject(project, buildDirectory: buildDir);
198-
expect(logger.errorText, contains('Failed to clean extension artifacts'));
200+
expect(
201+
logger.errorText,
202+
contains('Failed to clean extension artifacts for "failing_clean_extension"'),
203+
);
199204

200205
final Object? rawReceived = await verifyPort.first;
201206
final receivedEnv = CleanEnvironment.fromJson(rawReceived! as Map<String, Object?>);

0 commit comments

Comments
 (0)