@@ -262,11 +262,41 @@ extension Driver {
262262 try dependencyGraph. resolvePlaceholderDependencies ( using: externalDependencyArtifactMap!)
263263 }
264264
265- // Re-scan Clang modules at all the targets they will be built against
265+ // Re-scan Clang modules at all the targets they will be built against.
266266 try resolveVersionedClangDependencies ( dependencyGraph: & dependencyGraph)
267+
268+ // Set dependency modules' paths to be saved in the module cache.
269+ try updateDependencyModulesWithModuleCachePath ( dependencyGraph: & dependencyGraph)
270+
267271 return dependencyGraph
268272 }
269273
274+ /// Update the given inter-module dependency graph to set module paths to be within the module cache,
275+ /// if one is present.
276+ private mutating func updateDependencyModulesWithModuleCachePath( dependencyGraph:
277+ inout InterModuleDependencyGraph )
278+ throws {
279+ let moduleCachePath = parsedOptions. getLastArgument ( . moduleCachePath) ? . asSingle
280+ if moduleCachePath != nil {
281+ for (moduleId, moduleInfo) in dependencyGraph. modules {
282+ // Output path on the main module is determined by the invocation arguments.
283+ guard moduleId. moduleName != dependencyGraph. mainModuleName else {
284+ continue
285+ }
286+ let modulePath = moduleInfo. modulePath
287+ // Only update paths on modules which do not already specify a path beyond their module name
288+ // and a file extension.
289+ if modulePath == moduleId. moduleName + " .swiftmodule " ||
290+ modulePath == moduleId. moduleName + " .pcm " {
291+ // Use VirtualPath to get the OS-specific path separators right.
292+ let modulePathInCache =
293+ try VirtualPath ( path: moduleCachePath!) . appending ( component: modulePath) . description
294+ dependencyGraph. modules [ moduleId] !. modulePath = modulePathInCache
295+ }
296+ }
297+ }
298+ }
299+
270300 /// Create a job if needed for simple requests that can be immediately
271301 /// forwarded to the frontend.
272302 public mutating func immediateForwardingJob( ) throws -> Job ? {
0 commit comments