Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Support lazy ddr debugging (prorotype)
  • Loading branch information
Anna Gringauze committed Dec 8, 2023
commit 6a6139133f3603781d849a1796a1294a192cffbe
8 changes: 5 additions & 3 deletions dwds/lib/src/debugging/metadata/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:path/path.dart' as p;
class MetadataProvider {
final AssetReader _assetReader;
final _logger = Logger('MetadataProvider');
final String appName;
bool _soundNullSafety;
final List<String> _libraries = [];
final Map<String, String> _scriptToModule = {};
Expand Down Expand Up @@ -68,7 +67,9 @@ class MetadataProvider {
'dart:ui',
];

MetadataProvider(this.appName, this._assetReader) : _soundNullSafety = false;
MetadataProvider(this._assetReader) : _soundNullSafety = false {
_logger.info('Created metadata provider');
}

/// A sound null safety mode for the whole app.
///
Expand Down Expand Up @@ -179,9 +180,10 @@ class MetadataProvider {
}

Future<void> _initialize() async {
// TODO: run in a sequence? not sure if there are races.
_logger.info('Initializing... ');
await mainEntrypoint;
await Future.wait(_entryPoints.values);
_logger.info('Initialized.');
}

void update(String entrypoint) {
Expand Down
10 changes: 4 additions & 6 deletions dwds/lib/src/handlers/dev_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
/// Note: this should not be checked in enabled.
const _enableLogging = false;

final _logger = Logger('DevHandler');

/// SSE handler to enable development features like hot reload and
/// opening DevTools.
class DevHandler {
Logger _logger = Logger('DevHandler');
final _logger = Logger('DevHandler');
final _subs = <StreamSubscription>[];
final _sseHandlers = <String, SocketHandler>{};
final _injectedConnections = <SocketConnection>{};
Expand Down Expand Up @@ -274,7 +272,7 @@ class DevHandler {
await _handleConnectRequest(message, injectedConnection);
for (var entrypoint in connection.request.entrypoints) {
globalToolConfiguration.loadStrategy
.trackAppEntrypoint(connection.request.appName, entrypoint);
.trackEntrypoint(connection.request.appName, entrypoint);
}
appConnection = connection;
} else {
Expand All @@ -283,11 +281,11 @@ class DevHandler {
throw StateError('Not connected to an application.');
}
if (message is RegisterEntrypointRequest) {
globalToolConfiguration.loadStrategy.trackAppEntrypoint(
globalToolConfiguration.loadStrategy.trackEntrypoint(
connection.request.appName,
message.entrypointPath,
);
_servicesByAppId[connection.request.appId]
await _servicesByAppId[connection.request.appId]
?.chromeProxyService
.parseRegisterEntrypointRequest(message);
}
Expand Down
148 changes: 116 additions & 32 deletions dwds/lib/src/handlers/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:io';
import 'dart:isolate';

import 'package:crypto/crypto.dart';
import 'package:dwds/dwds.dart';
import 'package:dwds/src/config/tool_configuration.dart';
import 'package:dwds/src/version.dart';
import 'package:logging/logging.dart';
Expand Down Expand Up @@ -97,16 +98,19 @@ class DwdsInjector {
_devHandlerPaths.add(devHandlerPath);
final entrypoint = request.url.path;

// TODO: define a separate API for reading build metadata and app's metadata?
// Track the main entrypoint for the app read the build metadata.
await globalToolConfiguration.loadStrategy
.trackEntrypoint(entrypoint);
.trackAppEntrypoint(entrypoint);

body = _injectClientAndHoistMain(
body,
appId,
devHandlerPath,
entrypoint,
await _extensionUri,
);
_logger.info('Injecting debugging metadata for '
'entrypoint at $requestedUri');
body += await globalToolConfiguration.loadStrategy
.bootstrapFor(entrypoint);
_logger.info('Injected debugging metadata for '
Expand Down Expand Up @@ -200,38 +204,118 @@ String _injectedClientSnippet(
final appMetadata = globalToolConfiguration.appMetadata;
final debugSettings = globalToolConfiguration.debugSettings;

var injectedBody = '\n'
' console.log("INJECTOR: registering app: \${appName}.");\n'
' window.\$dwdsVersion = "$packageVersion";\n' // used by DDC
'\n'
' let appRecord = {};\n'
' appRecord.moduleStrategy = "${loadStrategy.id}";\n'
' appRecord.reloadConfiguration = "${loadStrategy.reloadConfiguration}";\n'
' appRecord.loadModuleConfig = ${loadStrategy.loadModuleSnippet};\n'
' appRecord.dwdsVersion = "$packageVersion";\n'
' appRecord.enableDevtoolsLaunch = ${debugSettings.enableDevToolsLaunch};\n'
' appRecord.emitDebugEvents = ${debugSettings.emitDebugEvents};\n'
' appRecord.isInternalBuild = ${appMetadata.isInternalBuild};\n'
' appRecord.appName = appName;\n'
' appRecord.appId = "$appId";\n'
' appRecord.isFlutterApp = ${buildSettings.isFlutterApp};\n'
' appRecord.devHandlerPath = "$devHandlerPath";\n'
' appRecord.entrypoints = new Array();\n'
' appRecord.entrypoints.push("$entrypointPath");\n';

if (extensionUri != null) {
injectedBody += ' appRecord.extensionUrl = "$extensionUri";\n';
}

final workspaceName = appMetadata.workspaceName;
if (workspaceName != null) {
injectedBody += ' appRecord.workspaceName = "$workspaceName";\n';
}
final appInfo = JSAppInfo(
appId: appId,
devHandlerPath: devHandlerPath,
dwdsVersion: packageVersion,
emitDebugEvents: debugSettings.emitDebugEvents,
enableDevToolsLaunch: debugSettings.enableDevToolsLaunch,
entrypointPath: entrypointPath,
extensionUrl: extensionUri,
isFlutterApp: buildSettings.isFlutterApp,
isInternalBuild: appMetadata.isInternalBuild,
loadModuleConfig: loadStrategy.loadModuleSnippet,
moduleStrategy: loadStrategy.id,
reloadConfiguration: loadStrategy.reloadConfiguration,
workspaceName: appMetadata.workspaceName,
);

injectedBody += '\n'
' window.\$dartAppInfo = appRecord;\n'
' console.log("INJECTOR: Loading injected client...");\n'
final injectedBody = '\n'
//' console.log("INJECTOR: registering app: " + appName);\n'
// Used by DDC runtime to detect if a debugger is attached.
' window.\$dwdsVersion = "$packageVersion";\n'
// Used by the injected client to communicate with the debugger.
' window.\$dartAppInfo = ${appInfo.toJs()};\n'
// Load the injected client.
' ${loadStrategy.loadClientSnippet(_clientScript)};\n';

Logger.root.warning(injectedBody);

// injectedBody += '\n'
// ' let appRecord = {};\n'
// ' appRecord.moduleStrategy = "${loadStrategy.id}";\n'
// ' appRecord.reloadConfiguration = "${loadStrategy.reloadConfiguration}";\n'
// ' appRecord.loadModuleConfig = ${loadStrategy.loadModuleSnippet};\n'
// ' appRecord.dwdsVersion = "$packageVersion";\n'
// ' appRecord.enableDevToolsLaunch = ${debugSettings.enableDevToolsLaunch};\n'
// ' appRecord.emitDebugEvents = ${debugSettings.emitDebugEvents};\n'
// ' appRecord.isInternalBuild = ${appMetadata.isInternalBuild};\n'
// ' appRecord.appName = appName;\n'
// ' appRecord.appId = "$appId";\n'
// ' appRecord.isFlutterApp = ${buildSettings.isFlutterApp};\n'
// ' appRecord.devHandlerPath = "$devHandlerPath";\n'
// ' appRecord.entrypoints = new Array();\n'
// ' appRecord.entrypoints.push("$entrypointPath");\n';

// if (extensionUri != null) {
// injectedBody += ' appRecord.extensionUrl = "$extensionUri";\n';
// }

// final workspaceName = appMetadata.workspaceName;
// if (workspaceName != null) {
// injectedBody += ' appRecord.workspaceName = "$workspaceName";\n';
// }

// injectedBody += '\n'
// ' window.\$dartAppInfo = $appInfo;\n'
// ' console.log("INJECTOR: Loading injected client...");\n'
// ' ${loadStrategy.loadClientSnippet(_clientScript)};\n';

return injectedBody;
}

/// Generate JS app info object for the injected client.
/// TODO(annagrin): ensure client's AppInfo can read this object.
class JSAppInfo {
String moduleStrategy;
ReloadConfiguration reloadConfiguration;
String loadModuleConfig;
String dwdsVersion;
bool enableDevToolsLaunch;
bool emitDebugEvents;
bool isInternalBuild;
String appId;
bool isFlutterApp;
String? extensionUrl;
String devHandlerPath;
String entrypointPath;
String? workspaceName;

JSAppInfo({
required this.appId,
required this.devHandlerPath,
required this.dwdsVersion,
required this.emitDebugEvents,
required this.enableDevToolsLaunch,
required this.entrypointPath,
required this.extensionUrl,
required this.isFlutterApp,
required this.isInternalBuild,
required this.loadModuleConfig,
required this.moduleStrategy,
required this.reloadConfiguration,
required this.workspaceName,
});

String toJs() {
final fields = {
'moduleStrategy': '"$moduleStrategy"',
'reloadConfiguration': '"$reloadConfiguration"',
'loadModuleConfig': loadModuleConfig,
'dwdsVersion': '"$dwdsVersion"',
'enableDevToolsLaunch': enableDevToolsLaunch,
'emitDebugEvents': emitDebugEvents,
'isInternalBuild': isInternalBuild,
'appName': 'appName',
'appId': '"$appId"',
'isFlutterApp': isFlutterApp,
'devHandlerPath': '"$devHandlerPath"',
'entrypoints': '["$entrypointPath"]',
if (extensionUrl != null) 'extensionUrl': '"$extensionUrl"',
if (workspaceName != null) 'workspaceName': '"$workspaceName"',
};

final lines = fields.entries.map((e) => ' ${e.key}: ${e.value},');
return ['{', ...lines, '}'].join('\n ');
}
}
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.