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