Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should @main be run after julia -L file.jl -e "do_something()"? #57552

Open
lukas-weber opened this issue Feb 27, 2025 · 4 comments
Open

Should @main be run after julia -L file.jl -e "do_something()"? #57552

lukas-weber opened this issue Feb 27, 2025 · 4 comments

Comments

@lukas-weber
Copy link

lukas-weber commented Feb 27, 2025

I noticed recently that julia will run the new @main entry point even after running an expression with the -e flag. This is a bit surprising. Consider the following workflow.

# runscript.jl
using MyPackage

config = generate_config()

function (@main)(args)
    run_heavy_calculation(args)
end

This script can then be submitted e.g. via slurm for some production run. It would be really nice if I could do the following

julia -L runscript.jl -e "run_diagnostics(config)"

to do some sanity checks or print some configuration parameters without running the heavy calculation. However, in julia 1.11.3, @main will be run after run_diagnostics.

A workaround is to do julia -L runscript.jl -e "(@main)(_) = (run_diagnostics(config);0)". However, I would expect -e to behave like running julia -L runscript.jl and then entering the commands myself, which is clearly not the case. The same occurs with the -E flag, which is even more surprising.

Maybe changing this behavior would be a breaking change and we are stuck with it. Maybe it is surprising enough to be considered a bug that can be fixed?

@Keno
Copy link
Member

Keno commented Feb 28, 2025

I think there's a question of what happens if you do -e include("runscript.jl"). Does main run? I think people probably expect it would because they'd expect it to be equivalent to running runscript.jl directly which does run main. I think this points to the problem being -L. I could see an argument for adding a special case that suppresses main when passed to -L (to facilitate precisely this use case), but it's a bit weird.

@lukas-weber
Copy link
Author

I think if -e 'include("runscript.jl"); do_something()' behaves differently from -L runscript.jl -e 'do_something()', it would be more confusing than the current situation.

An alternative solution would be to add a --nomain flag after all.

@Keno
Copy link
Member

Keno commented Feb 28, 2025

The other thing I can think of is that -e could be semantically considered as defining a main internally, so it would only run main if that main was defined inside -e. I think that could be acceptable.

@lukas-weber
Copy link
Author

That’s probably the perfect solution for my use case. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants