From ca72e28f26b7e2fb617ce8f83fd82ade62007e07 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Sat, 31 Aug 2024 13:47:48 +0200 Subject: [PATCH] allow `-m` to run the entry point in a submodule (#55265) --- base/client.jl | 4 ++-- test/loading.jl | 1 + test/project/Rot13/src/Rot13.jl | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/base/client.jl b/base/client.jl index 0290d27b09cf0..2ca88c40aeb7e 100644 --- a/base/client.jl +++ b/base/client.jl @@ -292,12 +292,12 @@ function exec_options(opts) invokelatest(show, Core.eval(Main, parse_input_line(arg))) println() elseif cmd == 'm' - @eval Main import $(Symbol(arg)).main + entrypoint = push!(split(arg, "."), "main") + Base.eval(Main, Expr(:import, Expr(:., Symbol.(entrypoint)...))) if !should_use_main_entrypoint() error("`main` in `$arg` not declared as entry point (use `@main` to do so)") end return false - elseif cmd == 'L' # load file immediately on all processors if !distributed_mode diff --git a/test/loading.jl b/test/loading.jl index 51e0c45d2faf1..fe6f800276547 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -1584,6 +1584,7 @@ end @testset "-m" begin rot13proj = joinpath(@__DIR__, "project", "Rot13") @test readchomp(`$(Base.julia_cmd()) --startup-file=no --project=$rot13proj -m Rot13 --project nowhere ABJURER`) == "--cebwrpg abjurer NOWHERE " + @test readchomp(`$(Base.julia_cmd()) --startup-file=no --project=$rot13proj -m Rot13.Rot26 --project nowhere ABJURER`) == "--project nowhere ABJURER " end @testset "workspace loading" begin diff --git a/test/project/Rot13/src/Rot13.jl b/test/project/Rot13/src/Rot13.jl index 1d19cbbe6df91..66f077812d878 100644 --- a/test/project/Rot13/src/Rot13.jl +++ b/test/project/Rot13/src/Rot13.jl @@ -12,4 +12,17 @@ function (@main)(args) return 0 end +module Rot26 # LOL + +import ..rot13 + +rot26(str::AbstractString) = map(rot13 ∘ rot13, str) + +function (@main)(args) + foreach(arg -> print(rot26(arg), " "), args) + return 0 +end + +end + end # module Rot13