Skip to content

Commit

Permalink
Add program_file to JLOptions and use for parsing project-at-script
Browse files Browse the repository at this point in the history
As detailed in JuliaLang#55069, PROGRAM_FILE and ARGS are unset when
`load_path_expand` is first called during `Base.__init__()`, this
adds `program_file` to JLOptions as sets it during `jl_parse_opts`

Removed `unsafe_load_commands` path in `load_path_expand` as ARGS is
unset at this point.
  • Loading branch information
awadell1 committed Oct 26, 2024
1 parent 14ff5a7 commit 35020cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
24 changes: 8 additions & 16 deletions base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,14 @@ function load_path_expand(env::AbstractString)::Union{String, Nothing}
env == "@temp" && return mktempdir()
env == "@stdlib" && return Sys.STDLIB
if startswith(env, "@script")
if @isdefined(PROGRAM_FILE)
dir = dirname(PROGRAM_FILE)
else
cmds = unsafe_load_commands(JLOptions().commands)
if any(cmd::Pair{Char, String}->cmd_suppresses_program(first(cmd)), cmds)
# Usage error. The user did not pass a script.
return nothing
end
dir = dirname(ARGS[1])
end
if env == "@script" # complete match, not startswith, so search upwards
return current_project(dir)
else
# starts with, so assume relative path is after
return abspath(replace(env, "@script" => dir))
end
program_file = JLOptions().program_file
program_file = program_file != C_NULL ? unsafe_string(program_file) : nothing
isnothing(program_file) && return nothing # User did not pass a script

# Expand trailing relative path
dir = dirname(program_file)
dir = env != "@script" ? joinpath(dir, env[length("@script"):end]) : dir
return current_path(dir)
end
env = replace(env, '#' => VERSION.major, count=1)
env = replace(env, '#' => VERSION.minor, count=1)
Expand Down
1 change: 1 addition & 0 deletions base/options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct JLOptions
nprocs::Int32
machine_file::Ptr{UInt8}
project::Ptr{UInt8}
program_file::Ptr{UInt8}
isinteractive::Int8
color::Int8
historyfile::Int8
Expand Down
2 changes: 2 additions & 0 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ JL_DLLEXPORT void jl_init_options(void)
0, // nprocs
NULL, // machine_file
NULL, // project
NULL, // program_file
0, // isinteractive
0, // color
JL_OPTIONS_HISTORYFILE_ON, // history file
Expand Down Expand Up @@ -969,6 +970,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
"This is a bug, please report it.", c);
}
}
jl_options.program_file = optind < argc ? strdup(argv[optind]) : "";
parsing_args_done:
jl_options.code_coverage = codecov;
jl_options.malloc_log = malloclog;
Expand Down
1 change: 1 addition & 0 deletions src/jloptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct {
int32_t nprocs;
const char *machine_file;
const char *project;
const char *program_file;
int8_t isinteractive;
int8_t color;
int8_t historyfile;
Expand Down

0 comments on commit 35020cd

Please sign in to comment.