diff --git a/compiler/src/prog8/compiler/ModuleImporter.kt b/compiler/src/prog8/compiler/ModuleImporter.kt index c672fada8..c4eaa0317 100644 --- a/compiler/src/prog8/compiler/ModuleImporter.kt +++ b/compiler/src/prog8/compiler/ModuleImporter.kt @@ -78,37 +78,34 @@ class ModuleImporter(private val program: Program, val existing = program.modules.singleOrNull { it.name == moduleName } if (existing!=null) - return null // TODO: why return null instead of Module instance? + return existing // try internal library first val moduleResourceSrc = getModuleFromResource("$moduleName.p8", compilationTargetName) - var importedModule: Module? = null - moduleResourceSrc.fold( - success = { - println("importing '$moduleName' (from internal ${it.origin})") - importedModule=importModule(it) - }, - failure = { - // try filesystem next - val moduleSrc = getModuleFromFile(moduleName, importingModule) - moduleSrc.fold( - success = { - println("importing '$moduleName' (from file ${it.origin})") - importedModule = importModule(it) - }, - failure = { - errors.err("no module found with name $moduleName", import.position) - } - ) - } - ) + val importedModule = + moduleResourceSrc.fold( + success = { + println("importing '$moduleName' (from internal ${it.origin})") + importModule(it) + }, + failure = { + // try filesystem next + val moduleSrc = getModuleFromFile(moduleName, importingModule) + moduleSrc.fold( + success = { + println("importing '$moduleName' (from file ${it.origin})") + importModule(it) + }, + failure = { + errors.err("no module found with name $moduleName", import.position) + return null + } + ) + } + ) - return if(importedModule==null) - null - else { - removeDirectivesFromImportedModule(importedModule!!) - importedModule - } + removeDirectivesFromImportedModule(importedModule) + return importedModule } private fun removeDirectivesFromImportedModule(importedModule: Module) { diff --git a/compiler/test/ModuleImporterTests.kt b/compiler/test/ModuleImporterTests.kt index 17889e9e3..9d724b57d 100644 --- a/compiler/test/ModuleImporterTests.kt +++ b/compiler/test/ModuleImporterTests.kt @@ -250,14 +250,14 @@ class TestModuleImporter { val result = importer.importLibraryModule(filenameNoExt) assertThat(count[n] + " call / NO .p8 extension", result, Is(nullValue())) assertFalse(errors.noErrors(), count[n] + " call / NO .p8 extension") - assertEquals(errors.errors.single(), "imported file not found: i_do_not_exist.p8") + assertEquals(errors.errors.single(), "no module found with name i_do_not_exist") errors.report() assertThat(program.modules.size, equalTo(1)) val result2 = importer.importLibraryModule(filenameWithExt) assertThat(count[n] + " call / with .p8 extension", result2, Is(nullValue())) assertFalse(importer.errors.noErrors(), count[n] + " call / with .p8 extension") - assertEquals(errors.errors.single(), "imported file not found: i_do_not_exist.p8.p8") // TODO don't duplicate the p8 extension in the import logic... + assertEquals(errors.errors.single(), "no module found with name i_do_not_exist.p8") // TODO don't add a p8 extension in the import logic... errors.report() assertThat(program.modules.size, equalTo(1)) }