Skip to content

Commit 4fc9181

Browse files
authored
Add option to not use module name in MetadataProvider (#2529)
DDC library bundle format does not provide module names as part of the ModuleMetadata. - To support this discrepancy, a flag is added to MetadataProvider to either use the module name or the module path to identify the module. - createProvider is added so LoadStrategys can create MetadataProviders based on whether the module format uses the module name. - Cleans up some getters and methods in LoadStrategy implementations so that getters are grouped together.
1 parent 1281e60 commit 4fc9181

File tree

8 files changed

+148
-64
lines changed

8 files changed

+148
-64
lines changed

dwds/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
- Replace deprecated JS code `this.__proto__` with `Object.getPrototypeOf(this)`. - [#2500](https://github.com/dart-lang/webdev/pull/2500)
66
- Migrate injected client code to `package:web`. - [#2491](https://github.com/dart-lang/webdev/pull/2491)
77
- Deprecated MetadataProvider's, CompilerOptions', SdkConfiguration's & SdkLayout's soundNullSafety. - [#2427](https://github.com/dart-lang/webdev/issues/2427)
8-
- Add load strategy and an unimplemented hot restart strategy for DDC library bundle format.
8+
- Add load strategy and an unimplemented hot restart strategy for DDC library
9+
bundle format.
10+
- Added `useModuleName` option to `MetadataProvider` to determine whether or not
11+
to use the provided `name` in a `ModuleMetadata`. Metadata provided by DDC
12+
when using the library bundle format does not provide a useful bundle name.
913

1014
## 24.1.0
1115

dwds/lib/src/debugging/metadata/module_metadata.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class ModuleMetadata {
101101
///
102102
/// Used as a name of the js module created by the compiler and
103103
/// as key to store and load modules in the debugger and the browser
104+
// TODO(srujzs): Remove once https://github.com/dart-lang/sdk/issues/59618 is
105+
// resolved.
104106
final String name;
105107

106108
/// Name of the function enclosing the module

dwds/lib/src/debugging/metadata/provider.dart

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class MetadataProvider {
2222
final Map<String, String> _moduleToModulePath = {};
2323
final Map<String, List<String>> _scripts = {};
2424
final _metadataMemoizer = AsyncMemoizer();
25+
// Whether to use the `name` provided in the module metadata.
26+
final bool _useModuleName;
2527

2628
/// Implicitly imported libraries in any DDC component.
2729
///
@@ -64,7 +66,11 @@ class MetadataProvider {
6466
'dart:ui',
6567
];
6668

67-
MetadataProvider(this.entrypoint, this._assetReader);
69+
MetadataProvider(
70+
this.entrypoint,
71+
this._assetReader, {
72+
required bool useModuleName,
73+
}) : _useModuleName = useModuleName;
6874

6975
/// A sound null safety mode for the whole app.
7076
///
@@ -113,6 +119,8 @@ class MetadataProvider {
113119
/// web/main
114120
/// }
115121
///
122+
/// If [_useModuleName] is false, the values will be the module paths instead
123+
/// of the name except for 'dart_sdk'.
116124
Future<Map<String, String>> get scriptToModule async {
117125
await _initialize();
118126
return _scriptToModule;
@@ -127,13 +135,14 @@ class MetadataProvider {
127135
/// web/main.ddc.js.map
128136
/// }
129137
///
130-
///
138+
/// If [_useModuleName] is false, the keys will be the module paths instead of
139+
/// the name.
131140
Future<Map<String, String>> get moduleToSourceMap async {
132141
await _initialize();
133142
return _moduleToSourceMap;
134143
}
135144

136-
/// A map of module path to module name
145+
/// A map of module path to module name.
137146
///
138147
/// Example:
139148
///
@@ -142,12 +151,14 @@ class MetadataProvider {
142151
/// web/main
143152
/// }
144153
///
154+
/// If [_useModuleName] is false, the values will be the module paths instead
155+
/// of the name, making this an identity map.
145156
Future<Map<String, String>> get modulePathToModule async {
146157
await _initialize();
147158
return _modulePathToModule;
148159
}
149160

150-
/// A map of module to module path
161+
/// A map of module to module path.
151162
///
152163
/// Example:
153164
///
@@ -156,12 +167,14 @@ class MetadataProvider {
156167
/// web/main.ddc.js :
157168
/// }
158169
///
170+
/// If [_useModuleName] is false, the keys will be the module paths instead of
171+
/// the name, making this an identity map.
159172
Future<Map<String, String>> get moduleToModulePath async {
160173
await _initialize();
161174
return _moduleToModulePath;
162175
}
163176

164-
/// A list of module ids
177+
/// A list of module ids.
165178
///
166179
/// Example:
167180
///
@@ -170,6 +183,8 @@ class MetadataProvider {
170183
/// web/foo/bar
171184
/// ]
172185
///
186+
/// If [_useModuleName] is false, this will be the set of module paths
187+
/// instead.
173188
Future<List<String>> get modules async {
174189
await _initialize();
175190
return _moduleToModulePath.keys.toList();
@@ -196,8 +211,9 @@ class MetadataProvider {
196211
final metadata =
197212
ModuleMetadata.fromJson(moduleJson as Map<String, dynamic>);
198213
_addMetadata(metadata);
199-
_logger
200-
.fine('Loaded debug metadata for module: ${metadata.name}');
214+
final moduleName =
215+
_useModuleName ? metadata.name : metadata.moduleUri;
216+
_logger.fine('Loaded debug metadata for module: $moduleName');
201217
} catch (e) {
202218
_logger.warning('Failed to read metadata: $e');
203219
rethrow;
@@ -211,10 +227,14 @@ class MetadataProvider {
211227
void _addMetadata(ModuleMetadata metadata) {
212228
final modulePath = stripLeadingSlashes(metadata.moduleUri);
213229
final sourceMapPath = stripLeadingSlashes(metadata.sourceMapUri);
230+
// DDC library bundle module format does not provide names for library
231+
// bundles, and therefore we use the URI instead to represent a library
232+
// bundle.
233+
final moduleName = _useModuleName ? metadata.name : modulePath;
214234

215-
_moduleToSourceMap[metadata.name] = sourceMapPath;
216-
_modulePathToModule[modulePath] = metadata.name;
217-
_moduleToModulePath[metadata.name] = modulePath;
235+
_moduleToSourceMap[moduleName] = sourceMapPath;
236+
_modulePathToModule[modulePath] = moduleName;
237+
_moduleToModulePath[moduleName] = modulePath;
218238

219239
for (final library in metadata.libraries.values) {
220240
if (library.importUri.startsWith('file:/')) {
@@ -223,12 +243,12 @@ class MetadataProvider {
223243
_libraries.add(library.importUri);
224244
_scripts[library.importUri] = [];
225245

226-
_scriptToModule[library.importUri] = metadata.name;
246+
_scriptToModule[library.importUri] = moduleName;
227247
for (final path in library.partUris) {
228248
// Parts in metadata are relative to the library Uri directory.
229249
final partPath = p.url.join(p.dirname(library.importUri), path);
230250
_scripts[library.importUri]!.add(partPath);
231-
_scriptToModule[partPath] = metadata.name;
251+
_scriptToModule[partPath] = moduleName;
232252
}
233253
}
234254
}
@@ -239,6 +259,9 @@ class MetadataProvider {
239259
for (final lib in sdkLibraries) {
240260
_libraries.add(lib);
241261
_scripts[lib] = [];
262+
// TODO(srujzs): It feels weird that we add this mapping to only this map
263+
// and not any of the other module maps. We should maybe handle this
264+
// differently.
242265
_scriptToModule[lib] = moduleName;
243266
}
244267
}

dwds/lib/src/loaders/ddc.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class DdcStrategy extends LoadStrategy {
163163
@override
164164
String get loadModuleSnippet => 'dart_library.import';
165165

166+
@override
167+
BuildSettings get buildSettings => _buildSettings;
168+
166169
@override
167170
Future<String> bootstrapFor(String entrypoint) async =>
168171
await _ddcLoaderSetup(entrypoint);
@@ -208,9 +211,6 @@ window.\$dartLoader.loader.nextAttempt();
208211
@override
209212
String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri);
210213

211-
@override
212-
BuildSettings get buildSettings => _buildSettings;
213-
214214
@override
215215
String? g3RelativePath(String absolutePath) => _g3RelativePath(absolutePath);
216216
}

dwds/lib/src/loaders/ddc_library_bundle.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ class DdcLibraryBundleStrategy extends LoadStrategy {
140140
"function() { throw new Error('LoadStrategy.loadModuleSnippet is used. "
141141
"This is currently unsupported in the DDC library bundle format.'); }";
142142

143+
@override
144+
BuildSettings get buildSettings => _buildSettings;
145+
143146
@override
144147
Future<String> bootstrapFor(String entrypoint) async =>
145148
await _ddcLoaderSetup(entrypoint);
@@ -186,8 +189,11 @@ window.\$dartLoader.loader.nextAttempt();
186189
String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri);
187190

188191
@override
189-
BuildSettings get buildSettings => _buildSettings;
192+
String? g3RelativePath(String absolutePath) => _g3RelativePath(absolutePath);
190193

191194
@override
192-
String? g3RelativePath(String absolutePath) => _g3RelativePath(absolutePath);
195+
MetadataProvider createProvider(String entrypoint, AssetReader reader) =>
196+
// DDC library bundle format does not provide module names in the module
197+
// metadata.
198+
MetadataProvider(entrypoint, reader, useModuleName: false);
193199
}

dwds/lib/src/loaders/strategy.dart

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,37 @@ abstract class LoadStrategy {
5353
/// App build settings, such as entry point, build flags, app kind etc.
5454
BuildSettings get buildSettings;
5555

56-
/// Returns the bootstrap required for this [LoadStrategy].
57-
///
58-
/// The bootstrap is appended to the end of the entry point module.
59-
Future<String> bootstrapFor(String entrypoint);
60-
6156
/// A handler for strategy specific requests.
6257
///
6358
/// Used as a part of the injected_handler middleware.
6459
Handler get handler;
6560

61+
/// Returns a loader to read the content of the package configuration.
62+
///
63+
/// The package configuration URIs will be resolved relative to
64+
/// [packageConfigPath], but the loader can read the config from a different
65+
/// location.
66+
///
67+
/// If null, the default loader will read from [packageConfigPath].
68+
Future<Uint8List?> Function(Uri uri)? get packageConfigLoader => null;
69+
70+
/// The absolute path to the app's package configuration.
71+
String get packageConfigPath {
72+
return _packageConfigPath ?? _defaultPackageConfigPath;
73+
}
74+
75+
/// The default package config path if none is provided.
76+
String get _defaultPackageConfigPath => p.join(
77+
DartUri.currentDirectory,
78+
'.dart_tool',
79+
'package_config.json',
80+
);
81+
82+
/// Returns the bootstrap required for this [LoadStrategy].
83+
///
84+
/// The bootstrap is appended to the end of the entry point module.
85+
Future<String> bootstrapFor(String entrypoint);
86+
6687
/// JS code snippet for loading the injected client script.
6788
String loadClientSnippet(String clientScript);
6889

@@ -113,27 +134,6 @@ abstract class LoadStrategy {
113134
/// Returns `null` if not a google3 app.
114135
String? g3RelativePath(String absolutePath);
115136

116-
/// Returns a loader to read the content of the package configuration.
117-
///
118-
/// The package configuration URIs will be resolved relative to
119-
/// [packageConfigPath], but the loader can read the config from a different
120-
/// location.
121-
///
122-
/// If null, the default loader will read from [packageConfigPath].
123-
Future<Uint8List?> Function(Uri uri)? get packageConfigLoader => null;
124-
125-
/// The absolute path to the app's package configuration.
126-
String get packageConfigPath {
127-
return _packageConfigPath ?? _defaultPackageConfigPath;
128-
}
129-
130-
/// The default package config path if none is provided.
131-
String get _defaultPackageConfigPath => p.join(
132-
DartUri.currentDirectory,
133-
'.dart_tool',
134-
'package_config.json',
135-
);
136-
137137
/// Returns the [MetadataProvider] for the application located at the provided
138138
/// [entrypoint].
139139
MetadataProvider metadataProviderFor(String entrypoint) {
@@ -144,10 +144,15 @@ abstract class LoadStrategy {
144144
}
145145
}
146146

147+
/// Creates and returns a [MetadataProvider] with the given [entrypoint] and
148+
/// [reader].
149+
MetadataProvider createProvider(String entrypoint, AssetReader reader) =>
150+
MetadataProvider(entrypoint, reader, useModuleName: true);
151+
147152
/// Initializes a [MetadataProvider] for the application located at the
148153
/// provided [entrypoint].
149154
Future<void> trackEntrypoint(String entrypoint) {
150-
final metadataProvider = MetadataProvider(entrypoint, _assetReader);
155+
final metadataProvider = createProvider(entrypoint, _assetReader);
151156
_providers[metadataProvider.entrypoint] = metadataProvider;
152157
// Returns a Future so that the asynchronous g3-implementation can override
153158
// this method:

dwds/test/fixtures/fakes.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ class FakeStrategy extends LoadStrategy {
363363
String get loadModuleSnippet => '';
364364

365365
@override
366-
String? g3RelativePath(String absolutePath) => null;
366+
ReloadConfiguration get reloadConfiguration => ReloadConfiguration.none;
367367

368368
@override
369-
ReloadConfiguration get reloadConfiguration => ReloadConfiguration.none;
369+
String? g3RelativePath(String absolutePath) => null;
370370

371371
@override
372372
String loadClientSnippet(String clientScript) => 'dummy-load-client-snippet';
@@ -394,7 +394,7 @@ class FakeStrategy extends LoadStrategy {
394394

395395
@override
396396
MetadataProvider metadataProviderFor(String entrypoint) =>
397-
MetadataProvider(entrypoint, FakeAssetReader());
397+
createProvider(entrypoint, FakeAssetReader());
398398

399399
@override
400400
Future<Map<String, ModuleInfo>> moduleInfoForEntrypoint(String entrypoint) =>

0 commit comments

Comments
 (0)