|
4 | 4 |
|
5 | 5 | import 'package:args/args.dart';
|
6 | 6 | import 'package:dds/devtools_server.dart';
|
| 7 | +import 'package:dds_service_extensions/dds_service_extensions.dart'; |
| 8 | +import 'package:http/http.dart' as http; |
| 9 | +import 'package:meta/meta.dart'; |
7 | 10 | import 'package:path/path.dart' as path;
|
| 11 | +import 'package:vm_service/vm_service.dart'; |
| 12 | +import 'package:vm_service/vm_service_io.dart'; |
8 | 13 |
|
9 | 14 | import '../core.dart';
|
| 15 | +import '../dds_runner.dart'; |
10 | 16 | import '../sdk.dart';
|
11 | 17 | import '../utils.dart';
|
12 | 18 |
|
@@ -48,10 +54,183 @@ class DevToolsCommand extends DartdevCommand {
|
48 | 54 | final devToolsBinaries =
|
49 | 55 | fullSdk ? sdk.devToolsBinaries : path.absolute(sdkDir, 'devtools');
|
50 | 56 |
|
| 57 | + final argList = await _performDDSCheck(args); |
51 | 58 | final server = await DevToolsServer().serveDevToolsWithArgs(
|
52 |
| - args.arguments, |
| 59 | + argList, |
53 | 60 | customDevToolsPath: devToolsBinaries,
|
54 | 61 | );
|
55 | 62 | return server == null ? -1 : 0;
|
56 | 63 | }
|
| 64 | + |
| 65 | + /// Attempts to start a DDS instance if there isn't one running for the |
| 66 | + /// target application. |
| 67 | + /// |
| 68 | + /// Returns the argument list from [args], which is modified to include the |
| 69 | + /// DDS URI instead of the VM service URI if DDS is started by this method |
| 70 | + /// or a redirect to DDS would be followed. |
| 71 | + Future<List<String>> _performDDSCheck(ArgResults args) async { |
| 72 | + String? serviceProtocolUri; |
| 73 | + bool positionalServiceUri = false; |
| 74 | + if (args.rest.isNotEmpty) { |
| 75 | + serviceProtocolUri = args.rest.first; |
| 76 | + positionalServiceUri = true; |
| 77 | + } else if (args.wasParsed(DevToolsServer.argVmUri)) { |
| 78 | + serviceProtocolUri = args[DevToolsServer.argVmUri]; |
| 79 | + } |
| 80 | + |
| 81 | + final argList = args.arguments.toList(); |
| 82 | + |
| 83 | + // No VM service URI was provided, so the user is going to manually connect |
| 84 | + // to their application from DevTools or only use offline tooling. |
| 85 | + // |
| 86 | + // TODO(bkonyi): we should consider having devtools_server try and spawn |
| 87 | + // DDS if users try to connect to an application without a DDS instance. |
| 88 | + if (serviceProtocolUri == null) { |
| 89 | + return argList; |
| 90 | + } |
| 91 | + |
| 92 | + final originalUri = Uri.parse(serviceProtocolUri); |
| 93 | + var uri = originalUri; |
| 94 | + // The VM service doesn't like it when there's no trailing forward slash at |
| 95 | + // the end of the path. Add one if it's missing. |
| 96 | + uri = _ensureUriHasTrailingForwardSlash(uri); |
| 97 | + |
| 98 | + // Check to see if the URI is a VM service URI for a VM service instance |
| 99 | + // that already has a DDS instance connected. |
| 100 | + uri = await checkForRedirectToExistingDDSInstance(uri); |
| 101 | + |
| 102 | + // If this isn't a URI for a VM service with an active DDS instance, |
| 103 | + // check to see if this URI points to a running DDS instance. If not, try |
| 104 | + // and start DDS. |
| 105 | + if (uri == originalUri) { |
| 106 | + uri = await maybeStartDDS( |
| 107 | + uri: uri, |
| 108 | + ddsHost: args[DevToolsServer.argDdsHost], |
| 109 | + ddsPort: args[DevToolsServer.argDdsPort], |
| 110 | + machineMode: args.wasParsed(DevToolsServer.argMachine), |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + // If we ended up starting DDS or redirecting to an existing instance, |
| 115 | + // update the argument list to include the actual URI for DevTools to |
| 116 | + // connect to. |
| 117 | + if (uri != originalUri) { |
| 118 | + if (positionalServiceUri) { |
| 119 | + argList.removeLast(); |
| 120 | + } |
| 121 | + argList.add(uri.toString()); |
| 122 | + } |
| 123 | + |
| 124 | + return argList; |
| 125 | + } |
| 126 | + |
| 127 | + Uri _ensureUriHasTrailingForwardSlash(Uri uri) { |
| 128 | + if (uri.pathSegments.isNotEmpty) { |
| 129 | + final pathSegments = uri.pathSegments.toList(); |
| 130 | + if (pathSegments.last.isNotEmpty) { |
| 131 | + pathSegments.add(''); |
| 132 | + } |
| 133 | + uri = uri.replace(pathSegments: pathSegments); |
| 134 | + } |
| 135 | + return uri; |
| 136 | + } |
| 137 | + |
| 138 | + @visibleForTesting |
| 139 | + static Future<Uri> checkForRedirectToExistingDDSInstance(Uri uri) async { |
| 140 | + final request = http.Request('GET', uri)..followRedirects = false; |
| 141 | + final response = await request.send(); |
| 142 | + await response.stream.drain(); |
| 143 | + |
| 144 | + // If we're redirected that means that we attempted to speak directly to |
| 145 | + // the VM service even though there's an active DDS instance already |
| 146 | + // connected to it. In this case, DevTools will fail to connect to the VM |
| 147 | + // service directly so we need to modify the target URI in the args list to |
| 148 | + // instead point to the DDS instance. |
| 149 | + if (response.isRedirect) { |
| 150 | + final redirectUri = Uri.parse(response.headers['location']!); |
| 151 | + final ddsWsUri = Uri.parse(redirectUri.queryParameters['uri']!); |
| 152 | + |
| 153 | + // Remove '/ws' from the path, add a trailing '/' |
| 154 | + var pathSegments = ddsWsUri.pathSegments.toList() |
| 155 | + ..removeLast() |
| 156 | + ..add(''); |
| 157 | + if (pathSegments.length == 1) { |
| 158 | + pathSegments.add(''); |
| 159 | + } |
| 160 | + uri = ddsWsUri.replace( |
| 161 | + scheme: 'http', |
| 162 | + pathSegments: pathSegments, |
| 163 | + ); |
| 164 | + } |
| 165 | + return uri; |
| 166 | + } |
| 167 | + |
| 168 | + @visibleForTesting |
| 169 | + static Future<Uri> maybeStartDDS({ |
| 170 | + required Uri uri, |
| 171 | + required String ddsHost, |
| 172 | + required String ddsPort, |
| 173 | + bool machineMode = false, |
| 174 | + }) async { |
| 175 | + final pathSegments = uri.pathSegments.toList(); |
| 176 | + if (pathSegments.isNotEmpty && pathSegments.last.isEmpty) { |
| 177 | + // There's a trailing '/' at the end of the parsed URI, so there's an |
| 178 | + // empty string at the end of the path segments list that needs to be |
| 179 | + // removed. |
| 180 | + pathSegments.removeLast(); |
| 181 | + } |
| 182 | + |
| 183 | + final authCodesEnabled = pathSegments.isNotEmpty; |
| 184 | + final wsUri = uri.replace( |
| 185 | + scheme: 'ws', |
| 186 | + pathSegments: [ |
| 187 | + ...pathSegments, |
| 188 | + 'ws', |
| 189 | + ], |
| 190 | + ); |
| 191 | + |
| 192 | + final vmService = await vmServiceConnectUri(wsUri.toString()); |
| 193 | + |
| 194 | + try { |
| 195 | + // If this request throws a RPC error, DDS isn't running and we should |
| 196 | + // try and start it. |
| 197 | + await vmService.getDartDevelopmentServiceVersion(); |
| 198 | + } on RPCError { |
| 199 | + // If the user wants to start a debugging session we need to do some extra |
| 200 | + // work and spawn a Dart Development Service (DDS) instance. DDS is a VM |
| 201 | + // service intermediary which implements the VM service protocol and |
| 202 | + // provides non-VM specific extensions (e.g., log caching, client |
| 203 | + // synchronization). |
| 204 | + final debugSession = DDSRunner(); |
| 205 | + if (await debugSession.start( |
| 206 | + vmServiceUri: uri, |
| 207 | + ddsHost: ddsHost, |
| 208 | + ddsPort: ddsPort, |
| 209 | + debugDds: false, |
| 210 | + disableServiceAuthCodes: !authCodesEnabled, |
| 211 | + // TODO(bkonyi): should we just have DDS serve its own duplicate |
| 212 | + // DevTools instance? It shouldn't add much, if any, overhead but will |
| 213 | + // allow for developers to access DevTools directly through the VM |
| 214 | + // service URI at a later point. This would probably be a fairly niche |
| 215 | + // workflow. |
| 216 | + enableDevTools: false, |
| 217 | + enableServicePortFallback: true, |
| 218 | + )) { |
| 219 | + uri = debugSession.ddsUri!; |
| 220 | + if (!machineMode) { |
| 221 | + print( |
| 222 | + 'Started the Dart Development Service (DDS) at $uri', |
| 223 | + ); |
| 224 | + } |
| 225 | + } else if (!machineMode) { |
| 226 | + print( |
| 227 | + 'WARNING: Failed to start the Dart Development Service (DDS). ' |
| 228 | + 'Some development features may be disabled or degraded.', |
| 229 | + ); |
| 230 | + } |
| 231 | + } finally { |
| 232 | + await vmService.dispose(); |
| 233 | + } |
| 234 | + return uri; |
| 235 | + } |
57 | 236 | }
|
0 commit comments