From ddd5c287364920ac685c665ab8a6ab0377d200da Mon Sep 17 00:00:00 2001 From: Frames White Date: Tue, 28 May 2024 19:04:01 +0800 Subject: [PATCH] Rename at-scriptdir project argument to at-script and search upwards for Project.toml (#53356) Closes https://github.com/JuliaLang/julia/issues/53352 I also noticed it wasn't mentioned in the NEWs.md and so I added it . Reconciling the relative path behavior, with the search upwads behavour requested in #53352 is a thing. (cherry picked from commit a60f22eeee30c84bac4744a97caf105e4cfb1175) --- NEWS.md | 1 + base/initdefs.jl | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index adccd7b825a13..927fa4ea5fb81 100644 --- a/NEWS.md +++ b/NEWS.md @@ -55,6 +55,7 @@ Command-line option changes difference between defining a `main` function and executing the code directly at the end of the script ([#50974]). * The `--compiled-modules` and `--pkgimages` flags can now be set to `existing`, which will cause Julia to consider loading existing cache files, but not to create new ones ([#50586], [#52573]). +* The `--project` argument now accepts `@script` to give a path to a directory with a Project.toml relative to the passed script file. `--project=@script/foo` for the `foo` subdirectory. If no path is given after (i.e. `--project=@script`) then (like `--project=@.`) the directory and its parents are searched for a Project.toml ([#50864] and [#53352]) Multi-threading changes ----------------------- diff --git a/base/initdefs.jl b/base/initdefs.jl index 56c2c0c587272..96bdc7957bcca 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -272,7 +272,7 @@ function load_path_expand(env::AbstractString)::Union{String, Nothing} env == "@" && return active_project(false) env == "@." && return current_project() env == "@stdlib" && return Sys.STDLIB - if startswith(env, "@scriptdir") + if startswith(env, "@script") if @isdefined(PROGRAM_FILE) dir = dirname(PROGRAM_FILE) else @@ -283,7 +283,12 @@ function load_path_expand(env::AbstractString)::Union{String, Nothing} end dir = dirname(ARGS[1]) end - return abspath(replace(env, "@scriptdir" => dir)) + 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 end env = replace(env, '#' => VERSION.major, count=1) env = replace(env, '#' => VERSION.minor, count=1)