Fix a bogus exception in Glob.

R=rnystrom@google.com

Review URL: https://codereview.chromium.org//725263002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@41750 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 409bdec..4a1e352 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,8 @@
+## 1.0.3
+
+* Fix a bug where `Glob.list()` and `Glob.listSync()` would incorrectly throw
+  exceptions when a directory didn't exist on the filesystem.
+
 ## 1.0.2
 
 * Fixed `Glob.list()` on Windows.
diff --git a/lib/src/list_tree.dart b/lib/src/list_tree.dart
index 44eaaf3..bf6f7a4 100644
--- a/lib/src/list_tree.dart
+++ b/lib/src/list_tree.dart
@@ -13,6 +13,11 @@
 import 'stream_pool.dart';
 import 'utils.dart';
 
+/// The errno for a file or directory not existing.
+///
+/// This is consistent across platforms.
+const _ENOENT = 2;
+
 /// A structure built from a glob that efficiently lists filesystem entities
 /// that match that glob.
 ///
@@ -319,8 +324,16 @@
       children.forEach((sequence, child) {
         if (entity is! Directory) return;
         if (!sequence.matches(basename)) return;
-        resultPool.add(child.list(p.join(dir, basename),
-            followLinks: followLinks));
+        var stream = child.list(p.join(dir, basename), followLinks: followLinks)
+            .handleError((_) {}, test: (error) {
+          // Ignore errors from directories not existing. We do this here so
+          // that we only ignore warnings below wild cards. For example, the
+          // glob "foo/bar/*/baz" should fail if "foo/bar" doesn't exist but
+          // succeed if "foo/bar/qux/baz" doesn't exist.
+          return error is FileSystemException &&
+              error.osError.errorCode == _ENOENT;
+        });
+        resultPool.add(stream);
       });
     },
         onError: resultController.addError,
@@ -361,8 +374,20 @@
       entities.addAll(children.keys
           .where((sequence) => sequence.matches(basename))
           .expand((sequence) {
-        return children[sequence].listSync(
-            p.join(dir, basename), followLinks: followLinks);
+        try {
+          return children[sequence].listSync(
+              p.join(dir, basename), followLinks: followLinks).toList();
+        } on FileSystemException catch (error) {
+          // Ignore errors from directories not existing. We do this here so
+          // that we only ignore warnings below wild cards. For example, the
+          // glob "foo/bar/*/baz" should fail if "foo/bar" doesn't exist but
+          // succeed if "foo/bar/qux/baz" doesn't exist.
+          if (error.osError.errorCode == _ENOENT) {
+            return const [];
+          } else {
+            rethrow;
+          }
+        }
       }));
 
       return entities;
diff --git a/pubspec.yaml b/pubspec.yaml
index 8c4deb2..997de2d 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: glob
-version: 1.0.2
+version: 1.0.3
 author: "Dart Team <misc@dartlang.org>"
 homepage: http://www.dartlang.org
 description: Bash-style filename globbing.
diff --git a/test/list_test.dart b/test/list_test.dart
index ff02343..3327524 100644
--- a/test/list_test.dart
+++ b/test/list_test.dart
@@ -251,6 +251,18 @@
         p.join("foo", "baz", "qux")
       ])));
     });
+
+    test("lists a subdirectory that sometimes exists", () {
+      d.dir("top", [
+        d.dir("dir1", [
+          d.dir("subdir", [d.file("file")])
+        ]),
+        d.dir("dir2", [])
+      ]).create();
+
+      expect(list("top/*/subdir/**"),
+          completion(equals([p.join("top", "dir1", "subdir", "file")])));
+    });
   });
 }