what is `@.` in Julia `--project` command line option?

Evg*_*eny 5 command-line-interface julia

When starting Julia in the command line one can specify project directory. One of the options is @., resumably the current directory. What is @. in this context?

# from `julia --help`
--project[={<dir>|@.}]    Set <dir> as the home project/environment
Run Code Online (Sandbox Code Playgroud)

From the docs:

If the variable is set to @., Julia tries to find a project directory that contains Project.toml or JuliaProject.toml file from the current directory and its parents.

I realise the cli --project argument parsing occurs in this C code, and apparently used by this init code, although I'm not sure of the sequence of events in between.

Specifically in initdefs.jl we have:

project = (JLOptions().project != C_NULL ?
    unsafe_string(Base.JLOptions().project) :
    get(ENV, "JULIA_PROJECT", nothing))
HOME_PROJECT[] =
    project == nothing ? nothing :
    project == "" ? nothing :
    project == "@." ? current_project() : abspath(project)
Run Code Online (Sandbox Code Playgroud)

My reading of this code is that @. is just an arbitrary token, could it have been a simple . for cli? How does Julia search "the current directory and its parents."

There is similar notation used in LOAD_PATH(["@", "@v#.#", "@stdlib"]), as discussed here and here. Does @. belong to the same family of expansion as LOAD_PATH symbols?

fre*_*kre 13

家庭项目和 --project

--project标志定义了“家庭项目”或“家庭环境”。环境由Project.toml/Manifest.toml定义,并定义哪些包可用于using/ import

您可以设置--project为 (i) 目录(带或不带Project.toml)(ii)到 a 的路径Project.toml或(iii)到@.. (i) 和 (ii) 是不言自明的——Julia 会将位于路径上的项目视为主项目。现在对于 (iii),如果您设置了--project=@.Julia 将尝试查找现有 Project.toml文件并将其用作主项目。考虑以下示例:

~$ tree .
.
??? A
??? Project.toml

1 directory, 1 file
Run Code Online (Sandbox Code Playgroud)

whereA是一个空目录。我们可以轻松尝试(i)和(ii):

# existing file
[~]$ julia --project=Project.toml -E 'Base.active_project()'
"~/Project.toml"

# existing directory
[~]$ julia --project=A -E 'Base.active_project()'
"~/A/Project.toml"

# non-existing directory
[~]$ julia --project=B -E 'Base.active_project()'
"~/B/Project.toml"

# non-existing file
[~]$ julia --project=B/Project.toml -E 'Base.active_project()'
"~/B/Project.toml"
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的最后三个示例中,该Project.toml文件不存在,但会在需要时创建(例如,当Pkg用于操作包时)。

现在,将此与 的行为进行比较@.,后者将查找现有项目文件:

# from our root directory
[~]$ julia --project=@. -E 'Base.active_project()'
"~/Project.toml"

# from inside the A directory
[~/A]$ julia --project=@. -E 'Base.active_project()'
"~/Project.toml"
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,我们都找到了相同的Project.toml文件。使用@.选项 Julia 首先在当前目录中查找Project.toml文件,如果未找到,则上一级到父文件夹并在那里查找,依此类推。这就是第二个例子中发生的事情;Julia 没有Project.toml在空A目录中找到文件,所以我们向上移动到父目录,并在Project.toml那里找到了我们的文件。

是的,我们可以为此选择一些其他标记,但不是.因为它已经具有意义;它是当前目录的路径,并且是与--project.

负载路径和 "@"

要加载一个包Example,我们Example在它的[deps]部分定义一个主项目是不够的;主项目还需要显示在 Julias 加载路径 ( Base.load_path()) 中。默认情况下,加载路径从["@", "@v#.#", "@stdlib"]where "@v#.#"expands 扩展到~/.julia/environments/v#.#with#替换为 Julias 主要和次要版本号,并"@stdlib"扩展到 Julias 标准库的目录。"@"扩展为 1. 活动项目(用Pkg.activate/激活pkg> activate)或 2. 家庭项目。我们可以使用以下命令检查扩展的加载路径Base.load_path()

# without home project; @ expands to nothing
[~]$ julia -E 'Base.load_path()'
["~/.julia/environments/v1.0/Project.toml", "~/julia10/usr/share/julia/stdlib/v1.0"]

# with home project; @ expands to our specified home project
[~]$ julia --project=@. -E 'Base.load_path()'
["~/Project.toml", "~/.julia/environments/v1.0/Project.toml", "~/julia10/usr/share/julia/stdlib/v1.0"]
Run Code Online (Sandbox Code Playgroud)

最后,如果我们"@"从加载路径中删除,我们是否定义了一个 home 项目并不重要:

[~]$ export JULIA_LOAD_PATH="" && julia --project=@. -E 'Base.load_path()'
String[]
Run Code Online (Sandbox Code Playgroud)