Skip to content

Commit 1ba88e8

Browse files
Fix a bug with detecting DevTools server availability (#9189)
1 parent 158fc57 commit 1ba88e8

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

packages/devtools_app/lib/src/shared/preferences/preferences.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import '../diagnostics/inspector_service.dart';
2020
import '../feature_flags.dart';
2121
import '../globals.dart';
2222
import '../primitives/query_parameters.dart';
23+
import '../server/server.dart';
2324
import '../utils/utils.dart';
2425

2526
part '_cpu_profiler_preferences.dart';
@@ -216,12 +217,6 @@ class PreferencesController extends DisposableController
216217
return;
217218
}
218219

219-
// Whether DevTools was run using the `dt run` command, which runs DevTools
220-
// using `flutter run` and connects it to a locally running instance of the
221-
// DevTools server.
222-
final usingDebugDevToolsServer =
223-
(const String.fromEnvironment('debug_devtools_server')).isNotEmpty &&
224-
!kReleaseMode;
225220
final shouldEnableWasm =
226221
(enabledFromStorage || enabledFromQueryParams) &&
227222
kIsWeb &&

packages/devtools_app/lib/src/shared/server/_server_api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Future<bool> checkServerHttpApiAvailable() async {
1111
// in the server in the SDK (see `pkg\dds\lib\src\devtools\handler.dart`)
1212
// and not delegated back to DevTools shared code.
1313
final response = await get(
14-
Uri.parse('${apiPrefix}ping'),
14+
buildDevToolsServerRequestUri('${apiPrefix}ping'),
1515
).timeout(const Duration(seconds: 5));
1616
// When running with the local dev server Flutter may serve its index page
1717
// for missing files to support the hashless url strategy. Check the response

packages/devtools_app/lib/src/shared/server/server.dart

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,43 @@ final _log = Logger('devtools_server_client');
4747
bool get isDevToolsServerAvailable =>
4848
kIsWeb && storage is ServerConnectionStorage;
4949

50-
const _debugDevToolsServerFlag = 'debug_devtools_server';
50+
const _debugDevToolsServerEnvironmentVariable = String.fromEnvironment(
51+
'debug_devtools_server',
52+
);
53+
54+
/// Whether DevTools was run using the `dt run` command, which runs DevTools in
55+
/// debug mode using `flutter run` and connects it to an instance of the
56+
/// DevTools server.
57+
bool get usingDebugDevToolsServer =>
58+
_debugDevToolsServerEnvironmentVariable.isNotEmpty && !kReleaseMode;
5159

5260
String get devToolsServerUriAsString {
53-
const debugDevToolsServerUriAsString = String.fromEnvironment(
54-
_debugDevToolsServerFlag,
55-
);
5661
// Ensure we only use the debug DevTools server URI in non-release
5762
// builds. By running `dt run`, an instance of DevTools run with `flutter run`
5863
// can be connected to the DevTools server on a different port.
59-
return debugDevToolsServerUriAsString.isNotEmpty && !kReleaseMode
60-
? debugDevToolsServerUriAsString
64+
return usingDebugDevToolsServer
65+
? _debugDevToolsServerEnvironmentVariable
6166
: Uri.base.toString();
6267
}
6368

69+
/// Helper to build a request URI to the DevTools server, which may not be on
70+
/// the same origin as the DevTools app window.
71+
Uri buildDevToolsServerRequestUri(String url) {
72+
// [_debugDevToolsServerEnvironmentVariable] will be the empty string if the
73+
// [_debugDevToolsServerFlag] environment variable declaration was not set
74+
// using `--dart-define`.
75+
const baseUri = _debugDevToolsServerEnvironmentVariable;
76+
return Uri.parse(path.join(baseUri, url));
77+
}
78+
6479
/// Helper to catch any server request which could fail.
6580
///
6681
/// Returns HttpRequest or null (if server failure).
6782
Future<Response?> request(String url) async {
6883
Response? response;
6984

7085
try {
71-
// This will be the empty string if this environment declaration was not
72-
// set using `--dart-define`.
73-
const baseUri = String.fromEnvironment(_debugDevToolsServerFlag);
74-
response = await post(Uri.parse(path.join(baseUri, url)));
86+
response = await post(buildDevToolsServerRequestUri(url));
7587
} catch (_) {}
7688

7789
return response;

packages/devtools_app/test/shared/utils/future_work_tracker_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void main() {
7575

7676
test('tracks failed work', () {
7777
_wrapAndRunAsync((async) async {
78-
await runZonedGuarded(
78+
runZonedGuarded(
7979
() {
8080
final tracker = FutureWorkTracker();
8181
expect(tracker.active.value, isFalse);

0 commit comments

Comments
 (0)