@@ -995,8 +995,38 @@ ClangImporter::createClangInvocation(ClangImporter *importer,
995995 &tempDiagClient,
996996 /* owned*/ false );
997997
998- return clang::createInvocationFromCommandLine (invocationArgs, tempClangDiags,
999- nullptr , false , CC1Args);
998+ auto CI = clang::createInvocationFromCommandLine (
999+ invocationArgs, tempClangDiags, nullptr , false , CC1Args);
1000+
1001+ if (!CI) {
1002+ return CI;
1003+ }
1004+
1005+ // FIXME: clang fails to generate a module if there is a `-fmodule-map-file`
1006+ // argument pointing to a missing file.
1007+ // Such missing module files occur frequently in SourceKit. If the files are
1008+ // missing, SourceKit fails to build SwiftShims (which wouldn't have required
1009+ // the missing module file), thus fails to load the stdlib and hence looses
1010+ // all semantic functionality.
1011+ // To work around this issue, drop all `-fmodule-map-file` arguments pointing
1012+ // to missing files and report the error that clang would throw manually.
1013+ // rdar://77516546 is tracking that the clang importer should be more
1014+ // resilient and provide a module even if there were building it.
1015+ auto VFS = clang::createVFSFromCompilerInvocation (
1016+ *CI, *tempClangDiags,
1017+ importer->Impl .SwiftContext .SourceMgr .getFileSystem ());
1018+ std::vector<std::string> FilteredModuleMapFiles;
1019+ for (auto ModuleMapFile : CI->getFrontendOpts ().ModuleMapFiles ) {
1020+ if (VFS->exists (ModuleMapFile)) {
1021+ FilteredModuleMapFiles.push_back (ModuleMapFile);
1022+ } else {
1023+ importer->Impl .diagnose (SourceLoc (), diag::module_map_not_found,
1024+ ModuleMapFile);
1025+ }
1026+ }
1027+ CI->getFrontendOpts ().ModuleMapFiles = FilteredModuleMapFiles;
1028+
1029+ return CI;
10001030}
10011031
10021032std::unique_ptr<ClangImporter>
0 commit comments