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

Fix --project=@script when outside script directory #56351

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" ? (dir * env[length("@script")+1:end]) : dir
return current_project(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
7 changes: 5 additions & 2 deletions src/jloptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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 @@ -169,11 +170,12 @@ static const char opts[] =
" --help-hidden Print uncommon options not shown by `-h`\n\n"

// startup options
" --project[={<dir>|@temp|@.}] Set <dir> as the active project/environment.\n"
" --project[={<dir>|@temp|@.|@script[<rel>]}] Set <dir> as the active project/environment.\n"
" Or, create a temporary environment with `@temp`\n"
" The default @. option will search through parent\n"
" directories until a Project.toml or JuliaProject.toml\n"
" file is found.\n"
" file is found. @script is similar, but searches up from\n"
" the programfile or a path relative to programfile.\n"
" -J, --sysimage <file> Start up with the given system image file\n"
" -H, --home <dir> Set location of `julia` executable\n"
" --startup-file={yes*|no} Load `JULIA_DEPOT_PATH/config/startup.jl`; \n"
Expand Down Expand Up @@ -1007,6 +1009,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:
if (!jl_options.use_experimental_features) {
if (jl_options.trim != JL_TRIM_NO)
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
10 changes: 10 additions & 0 deletions test/cmdlineargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
@test expanded == readchomp(addenv(`$exename -e 'println(Base.active_project())'`, "JULIA_PROJECT" => "@foo", "HOME" => homedir()))
end

# --project=@script handling
let expanded = abspath(joinpath(@__DIR__, "project", "ScriptProject"))
script = joinpath(expanded, "bin", "script.jl")
# Check running julia with --project=@script both within and outside the script directory
@testset "--@script from $name" for (name, dir) in [("project", expanded), ("outside", pwd())]
@test joinpath(expanded, "Project.toml") == readchomp(Cmd(`$exename --project=@script $script`; dir))
@test joinpath(expanded, "SubProject", "Project.toml") == readchomp(Cmd(`$exename --project=@script/../SubProject $script`; dir))
end
end

# handling of `@temp` in --project and JULIA_PROJECT
@test tempdir() == readchomp(`$exename --project=@temp -e 'println(Base.active_project())'`)[1:lastindex(tempdir())]
@test tempdir() == readchomp(addenv(`$exename -e 'println(Base.active_project())'`, "JULIA_PROJECT" => "@temp", "HOME" => homedir()))[1:lastindex(tempdir())]
Expand Down
2 changes: 2 additions & 0 deletions test/project/ScriptProject/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "ScriptProject"
uuid = "6646321a-c4de-46ad-9761-435e5bb1f223"
2 changes: 2 additions & 0 deletions test/project/ScriptProject/SubProject/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "SubProject"
uuid = "50d58d6a-5ae2-46f7-9677-83c51ca667d5"
1 change: 1 addition & 0 deletions test/project/ScriptProject/bin/script.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
println(Base.active_project())
Loading