Skip to content

F# - how to figure out from within .fsx script if it's being loaded from interactive kernel #1264

@pkese

Description

@pkese

FSI allows to #load external .fsx files. Within them, I often follow the Python style pattern of if __name__ = "__main__" to detect if the given .fsx file is the main program being executed.
My pattern looks like:

if fsi.CommandLineArgs.[0].EndsWith __SOURCE_FILE__ then
    run_some_main()

However when called from the Interactive kernel, the fsi object is not present and the code fails.

I've tried wrapping it into #if INTERACTIVE, but the code still fails, because INTERACTIVE appears to be defined:

#if INTERACTIVE
if fsi.CommandLineArgs.[0].EndsWith __SOURCE_FILE__ then
    run_some_main()
#endif

It would be good to provide some means of detecting the runtime environment or if the current file is the main file being run.

Apparently there was already talk about introducing the fsi object in #646
but that's probably not the only possible solution to this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions