@@ -803,6 +803,70 @@ final class ExplicitModuleBuildTests: XCTestCase {
803803 }
804804 }
805805
806+ func testRegisterModuleDependencyFlag( ) throws {
807+ let ( stdlibPath, shimsPath, _, _) = try getDriverArtifactsForScanning ( )
808+ try withTemporaryDirectory { path in
809+ try localFileSystem. changeCurrentWorkingDirectory ( to: path)
810+ let moduleCachePath = path. appending ( component: " ModuleCache " )
811+ try localFileSystem. createDirectory ( moduleCachePath)
812+ let main = path. appending ( component: " testRegisterModuleDependency.swift " )
813+ // Note: We're NOT importing module E in the source code
814+ try localFileSystem. writeFileContents ( main, bytes:
815+ """
816+ import A;
817+ """
818+ )
819+ let cHeadersPath : AbsolutePath =
820+ try testInputsPath. appending ( component: " ExplicitModuleBuilds " )
821+ . appending ( component: " CHeaders " )
822+ let swiftModuleInterfacesPath : AbsolutePath =
823+ try testInputsPath. appending ( component: " ExplicitModuleBuilds " )
824+ . appending ( component: " Swift " )
825+ let sdkArgumentsForTesting = ( try ? Driver . sdkArgumentsForTesting ( ) ) ?? [ ]
826+
827+ var driver = try Driver ( args: [ " swiftc " ,
828+ " -I " , cHeadersPath. nativePathString ( escaped: true ) ,
829+ " -I " , swiftModuleInterfacesPath. nativePathString ( escaped: true ) ,
830+ " -I " , stdlibPath. nativePathString ( escaped: true ) ,
831+ " -I " , shimsPath. nativePathString ( escaped: true ) ,
832+ " -explicit-module-build " ,
833+ " -module-cache-path " , moduleCachePath. nativePathString ( escaped: true ) ,
834+ " -working-directory " , path. nativePathString ( escaped: true ) ,
835+ " -disable-implicit-concurrency-module-import " ,
836+ " -disable-implicit-string-processing-module-import " ,
837+ " -register-module-dependency " , " E " ,
838+ " -emit-loaded-module-trace " ,
839+ main. nativePathString ( escaped: true ) ] + sdkArgumentsForTesting)
840+ let dependencyGraph = try driver. scanModuleDependencies ( )
841+ let jobs = try driver. planBuild ( )
842+ // E SHOULD be in the dependency graph (registered for scanning)
843+ XCTAssertTrue ( dependencyGraph. modules. keys. contains ( . swift( " E " ) ) ,
844+ " Module E should be in dependency graph when registered via -register-module-dependency " )
845+ // Checking that registered module compiled
846+ let moduleEJobs = jobs. filter { job in
847+ job. outputs. contains { output in
848+ output. file. basename. contains ( " E " ) && output. file. extension == " swiftmodule "
849+ }
850+ }
851+ XCTAssertFalse ( moduleEJobs. isEmpty,
852+ " Module E should have a build job when registered via -register-module-dependency " )
853+ // Checking that registered module is not loaded for the main compilation
854+ try driver. run ( jobs: jobs)
855+ XCTAssertFalse ( driver. diagnosticEngine. hasErrors)
856+ // Checking the output given by the -emit-loaded-module-trace flag
857+ let traceFile = path. appending ( component: " testRegisterModuleDependency.trace.json " )
858+ XCTAssertTrue ( localFileSystem. exists ( traceFile) , " Module trace file should exist " )
859+ let traceData = try localFileSystem. readFileContents ( traceFile)
860+ let traceJSON = try traceData. withData { data in
861+ try JSONSerialization . jsonObject ( with: data, options: [ ] ) as? [ String : Any ]
862+ }
863+ let jsonString = String ( decoding: traceData. contents, as: UTF8 . self)
864+ XCTAssertFalse ( jsonString. contains ( " \" name \" : \" E \" " ) ,
865+ " Module E should not be loaded in the final compilation since it's not imported " )
866+
867+ }
868+ }
869+
806870 // Ensure that (even when not in '-incremental' mode) up-to-date module dependencies
807871 // do not get re-built
808872 func testExplicitModuleBuildIncrementalEndToEnd( ) throws {
0 commit comments