diff --git a/src/Requires.jl b/src/Requires.jl index 6fef3bf..8b9ae54 100644 --- a/src/Requires.jl +++ b/src/Requires.jl @@ -3,18 +3,27 @@ module Requires using UUIDs """ - @include("file.jl") + @include("somefile.jl") -Behaves like `include`, but loads the target file contents at load-time before -evaluating its contents at runtime. This is useful when the target file may not -be available at runtime (for example, because of compiling a sysimg). +Behaves like `include`, but caches the target file content at macro expansion +time, and uses this as a fallback when the file doesn't exist at runtime. This +is useful when compiling a sysimg. The argument `"somefile.jl"` must be a +string literal, not an expression. `@require` blocks insert this automatically when you use `include`. """ macro include(file) file = joinpath(dirname(String(__source__.file)), file) s = String(read(file)) - :(include_string($__module__, $s, $file)) + quote + file = $file + mod = $__module__ + if isfile(file) + Base.include(mod, file) + else + include_string(mod, $s, file) + end + end end include("init.jl") diff --git a/test/runtests.jl b/test/runtests.jl index d7d40d5..d0e0e8b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -44,6 +44,7 @@ end @testset "Requires" begin mktempdir() do pkgsdir + local rm_CachedIncludeTest_submod_file cd(pkgsdir) do npcdir = joinpath("FooNPC", "src") mkpath(npcdir) @@ -65,6 +66,14 @@ end cd(npcdir) do writepkg("FooSubPC", true, true) end + npcdir = joinpath("CachedIncludeTest", "src") + mkpath(npcdir) + cd(npcdir) do + writepkg("CachedIncludeTest", true, true) + submod_file = abspath("CachedIncludeTest_submod.jl") + @test isfile(submod_file) + rm_CachedIncludeTest_submod_file = ()->rm(submod_file) + end end push!(LOAD_PATH, pkgsdir) @@ -76,6 +85,12 @@ end @test !(:SubModule in names(FooSubNPC)) @eval using FooSubPC @test !(:SubModule in names(FooSubPC)) + @eval using CachedIncludeTest + # Test that the content of the file which defines + # CachedIncludeTest.SubModule is cached by `@require` so it can be used + # even when the file itself is removed. + rm_CachedIncludeTest_submod_file() + @test !(:SubModule in names(CachedIncludeTest)) @eval using Colors @@ -85,6 +100,7 @@ end @test FooSubNPC.SubModule.flag @test :SubModule in names(FooSubPC) @test FooSubPC.SubModule.flag + @test :SubModule in names(CachedIncludeTest) cd(pkgsdir) do npcdir = joinpath("FooAfterNPC", "src")