diff --git a/NEWS.md b/NEWS.md index 787a916a6c6d8..f0673fc225b3c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -40,6 +40,10 @@ This section lists changes that do not have deprecation warnings. of the socket. Previously the address of the remote endpoint was being returned ([#21825]). + * Using `ARGS` within the ~/.juliarc.jl or within a .jl file loaded with `--load` will no + longer contain the script name as the first argument. Instead the script name will be + assigned to `PROGRAM_FILE`. ([#22092]) + Library improvements -------------------- diff --git a/base/client.jl b/base/client.jl index 2f22a24ca6e7e..c2e5e4de82cd7 100644 --- a/base/client.jl +++ b/base/client.jl @@ -257,6 +257,11 @@ function process_options(opts::JLOptions) color_set = (opts.color != 0) global have_color = (opts.color == 1) global is_interactive = (opts.isinteractive != 0) + + # remove filename from ARGS + arg_is_program = opts.eval == C_NULL && opts.print == C_NULL && !isempty(ARGS) + global const PROGRAM_FILE = arg_is_program ? shift!(ARGS) : "" + while true # startup worker. # opts.startupfile, opts.load, etc should should not be processed for workers. @@ -296,11 +301,9 @@ function process_options(opts::JLOptions) break end # load file - if !isempty(ARGS) && !isempty(ARGS[1]) + if !isempty(PROGRAM_FILE) # program repl = false - # remove filename from ARGS - global PROGRAM_FILE = shift!(ARGS) if !is_interactive ccall(:jl_exit_on_sigint, Void, (Cint,), 1) end diff --git a/base/initdefs.jl b/base/initdefs.jl index 20437a4e57cba..4535a718a5fd7 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -9,7 +9,7 @@ A string containing the script name passed to Julia from the command line. Note script name remains unchanged from within included files. Alternatively see [`@__FILE__`](@ref). """ -PROGRAM_FILE = "" +:PROGRAM_FILE """ ARGS diff --git a/test/cmdlineargs.jl b/test/cmdlineargs.jl index f5ca5f9698dd0..3bcdd0a7d2c9e 100644 --- a/test/cmdlineargs.jl +++ b/test/cmdlineargs.jl @@ -248,53 +248,73 @@ let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no` # tested in test/parallel.jl, test/examples.jl) @test !success(`$exename --worker=true`) - escape(str) = replace(str, "\\", "\\\\") - # test passing arguments - let testfile = tempname() - try - # write a julia source file that just prints ARGS to STDOUT - write(testfile, """ - println(ARGS) + mktempdir() do dir + testfile = joinpath(dir, tempname()) + # write a julia source file that just prints ARGS to STDOUT + write(testfile, """ + println(ARGS) """) - @test readchomp(`$exename $testfile foo -bar --baz`) == - "String[\"foo\", \"-bar\", \"--baz\"]" - @test readchomp(`$exename $testfile -- foo -bar --baz`) == - "String[\"foo\", \"-bar\", \"--baz\"]" + cp(testfile, joinpath(dir, ".juliarc.jl")) + + withenv((is_windows() ? "USERPROFILE" : "HOME") => dir) do + output = "String[\"foo\", \"-bar\", \"--baz\"]" + @test readchomp(`$exename $testfile foo -bar --baz`) == output + @test readchomp(`$exename $testfile -- foo -bar --baz`) == output @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar --baz`) == - "String[\"foo\", \"-bar\", \"--baz\"]" - @test split(readchomp(`$exename -L $testfile $testfile`), '\n') == - ["String[\"$(escape(testfile))\"]", "String[]"] + output + @test readchomp(`$exename --startup-file=yes -e 'exit(0)' -- foo -bar --baz`) == + output + + output = "String[]\nString[]" + @test readchomp(`$exename -L $testfile $testfile`) == output + @test readchomp(`$exename --startup-file=yes $testfile`) == output + @test !success(`$exename --foo $testfile`) - @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar -- baz`) == "String[\"foo\", \"-bar\", \"--\", \"baz\"]" - finally - rm(testfile) + @test readchomp(`$exename -L $testfile -e 'exit(0)' -- foo -bar -- baz`) == + "String[\"foo\", \"-bar\", \"--\", \"baz\"]" end end - # test the script name - let a = tempname(), b = tempname() - try - write(a, """ - println(@__FILE__) - println(PROGRAM_FILE) - println(length(ARGS)) - include(\"$(escape(b))\") + # test the program name remains constant + mktempdir() do dir + a = joinpath(dir, "a.jl") + b = joinpath(dir, "b.jl") + c = joinpath(dir, ".juliarc.jl") + + write(a, """ + println(@__FILE__) + println(PROGRAM_FILE) + include(\"$(escape_string(b))\") """) - write(b, """ - println(@__FILE__) - println(PROGRAM_FILE) - println(length(ARGS)) + write(b, """ + println(@__FILE__) + println(PROGRAM_FILE) """) - @test split(readchomp(`$exename $a`), '\n') == - ["$a", "$a", "0", "$b", "$a", "0"] - @test split(readchomp(`$exename -L $b -e 'exit(0)'`), '\n') == - ["$(realpath(b))", "", "0"] - @test split(readchomp(`$exename -L $b $a`), '\n') == - ["$(realpath(b))", "", "1", "$a", "$a", "0", "$b", "$a", "0"] - finally - rm(a) - rm(b) + cp(b, c) + + readsplit(cmd) = split(readchomp(cmd), '\n') + + withenv((is_windows() ? "USERPROFILE" : "HOME") => dir) do + @test readsplit(`$exename $a`) == + [a, a, + b, a] + @test readsplit(`$exename -L $b -e 'exit(0)'`) == + [realpath(b), ""] + @test readsplit(`$exename -L $b $a`) == + [realpath(b), a, + a, a, + b, a] + @test readsplit(`$exename --startup-file=yes -e 'exit(0)'`) == + [c, ""] + @test readsplit(`$exename --startup-file=yes -L $b -e 'exit(0)'`) == + [c, "", + realpath(b), ""] + @test readsplit(`$exename --startup-file=yes -L $b $a`) == + [c, a, + realpath(b), a, + a, a, + b, a] end end