Issue 47097. Handle the case when a folder symbolic link target does not exist.

Bug: https://github.com/dart-lang/sdk/issues/47097
Change-Id: Ia32f742adf3ef24c67ec3e75cfa7dee969ff4afc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212483
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/context_locator.dart b/pkg/analyzer/lib/src/dart/analysis/context_locator.dart
index 7927eca..38d0316 100644
--- a/pkg/analyzer/lib/src/dart/analysis/context_locator.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/context_locator.dart
@@ -334,8 +334,12 @@
     }
 
     // Stop infinite recursion via links.
-    var canonicalFolderPath = folder.resolveSymbolicLinksSync().path;
-    if (!visited.add(canonicalFolderPath)) {
+    try {
+      var canonicalFolderPath = folder.resolveSymbolicLinksSync().path;
+      if (!visited.add(canonicalFolderPath)) {
+        return;
+      }
+    } on FileSystemException {
       return;
     }
 
diff --git a/pkg/analyzer/lib/src/dart/analysis/context_root.dart b/pkg/analyzer/lib/src/dart/analysis/context_root.dart
index 068948f..58a4b93 100644
--- a/pkg/analyzer/lib/src/dart/analysis/context_root.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/context_root.dart
@@ -96,13 +96,25 @@
     Folder folder,
     String includedPath,
   ) sync* {
-    for (Resource resource in folder.getChildren()) {
+    List<Resource> children;
+    try {
+      children = folder.getChildren();
+    } on FileSystemException {
+      return;
+    }
+
+    for (Resource resource in children) {
       String path = resource.path;
       if (!_isExcluded(path, includedPath)) {
         if (resource is File) {
           yield path;
         } else if (resource is Folder) {
-          var canonicalPath = resource.resolveSymbolicLinksSync().path;
+          String canonicalPath;
+          try {
+            canonicalPath = resource.resolveSymbolicLinksSync().path;
+          } on FileSystemException {
+            return;
+          }
           if (visited.add(canonicalPath)) {
             yield* _includedFilesInFolder(visited, resource, includedPath);
             visited.remove(canonicalPath);
diff --git a/pkg/analyzer/test/src/dart/analysis/context_locator_test.dart b/pkg/analyzer/test/src/dart/analysis/context_locator_test.dart
index 63b08a5..31462c1 100644
--- a/pkg/analyzer/test/src/dart/analysis/context_locator_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/context_locator_test.dart
@@ -95,6 +95,36 @@
     ]);
   }
 
+  void test_locateRoots_link_folder_notExistingTarget() {
+    var rootFolder = newFolder('/test');
+    newFile('/test/lib/a.dart');
+    newFolder('/test/lib/foo');
+    resourceProvider.newLink(
+      convertPath('/test/lib/foo'),
+      convertPath('/test/lib/bar'),
+    );
+
+    var roots = contextLocator.locateRoots(
+      includedPaths: [rootFolder.path],
+    );
+    expect(roots, hasLength(1));
+
+    var root = findRoot(roots, rootFolder);
+    expect(root.includedPaths, unorderedEquals([rootFolder.path]));
+    expect(root.excludedPaths, isEmpty);
+    expect(root.optionsFile, isNull);
+    expect(root.packagesFile, isNull);
+
+    _assertAnalyzedFiles(root, [
+      '/test/lib/a.dart',
+    ]);
+
+    _assertAnalyzed(root, [
+      '/test/lib/a.dart',
+      '/test/lib/foo/b.dart',
+    ]);
+  }
+
   void test_locateRoots_link_folder_toParentInRoot() {
     Folder rootFolder = newFolder('/test');
     newFile('/test/lib/a.dart');