From a4f2ad984d6eade39bf085fde8bd78fc235c616f Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Wed, 7 Dec 2016 19:51:27 -0800 Subject: [PATCH] Fix context.dart to properly handle nested zones (#7189) This fixes an infinite loop in the code that walks the parent context chain looking for a variable. This also includes a fix in build_info.dart whereby if the context is set but the config is not yet set, we were trying to dereference null. --- .../flutter_tools/lib/src/base/context.dart | 19 ++++++++++--------- .../flutter_tools/lib/src/build_info.dart | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/context.dart b/packages/flutter_tools/lib/src/base/context.dart index 96a21f1421d..96768a43a53 100644 --- a/packages/flutter_tools/lib/src/base/context.dart +++ b/packages/flutter_tools/lib/src/base/context.dart @@ -14,11 +14,13 @@ class AppContext { Map _instances = {}; Zone _zone; + AppContext() : _zone = Zone.current; + bool isSet(Type type) { if (_instances.containsKey(type)) return true; - AppContext parent = _calcParent(Zone.current); + AppContext parent = _calcParent(_zone); return parent != null ? parent.isSet(type) : false; } @@ -26,7 +28,7 @@ class AppContext { if (_instances.containsKey(type)) return _instances[type]; - AppContext parent = _calcParent(_zone ?? Zone.current); + AppContext parent = _calcParent(_zone); return parent?.getVariable(type); } @@ -51,12 +53,10 @@ class AppContext { if (parentZone == null) return null; - AppContext deps = parentZone['context']; - if (deps == this) { - return _calcParent(parentZone); - } else { - return deps; - } + AppContext parentContext = parentZone['context']; + return parentContext == this + ? _calcParent(parentZone) + : parentContext; } dynamic runInZone(dynamic method(), { @@ -70,11 +70,12 @@ class AppContext { } dynamic _run(dynamic method()) async { + Zone previousZone = _zone; try { _zone = Zone.current; return await method(); } finally { - _zone = null; + _zone = previousZone; } } } diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index bcadae22b87..8d8eb87cc84 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -121,7 +121,7 @@ HostPlatform getCurrentHostPlatform() { String getBuildDirectory() { // TODO(johnmccutchan): Stop calling this function as part of setting // up command line argument processing. - if (context == null) + if (context == null || config == null) return 'build'; String buildDir = config.getValue('build-dir') ?? 'build';