I’d like to run a command like
julia --project=@script_dir long/path/to/my/package/src/script.jl
rather than what I currently have to use, where the path is repeated verbatim
julia --project=long/path/to/my/package long/path/to/my/package/src/script.jl
Another inconvenient workaround is using Pkg
within the script to activate the environment of @__DIR__
or the parent path thereof.
The proposed behavior is that julia
wil recursively search the user-supplied script_dir
and parent directories until Project.toml
is found and use that as the virtual environment. What do you think?
1 Like
jules
January 16, 2025, 12:02pm
2
Thanks for the links! I vaguely remember reading about it somewhere, but I couldn’t find any information about it when I ran julia -h
today to look at the command line option list.
I just tried @script
and it didn’t work. Looks like there’s an open issue about it:
opened 02:04PM - 08 Jul 24 UTC
bug
Launching a script with `julia --project=@script foo.jl` does not activate the s… cript project if ran outside the containing folder.
In `test_at_script.jL`
```julia
#!/usr/bin/env -S julia --project=@script
@info "Check Active Env" Base.active_project() PROGRAM_FILE LOAD_PATH Base.load_path_expand("@script")
# Check that ../Project.toml got activated
active_project = realpath(Base.active_project())
expected = realpath(joinpath(@__DIR__, "..", "Project.toml"))
@assert active_project == expected "$active_project != $expected"
```
Expected Directory Structure:
```
Documents/
julia_at_script_issue/
Project.toml
bin/
test_at_script.jl
```
> Alternatively, run `script.sh` in the attached achieve: [julia_at_script_issue.zip](https://github.com/user-attachments/files/16130231/julia_at_script_issue.zip)
Running from `Documents/julia_at_script_issue/` with `julia --startup-file=no --project=@script bin/test_at_script.jl` or `./bin/test_at_script.jl` gives the following (expected) output:
```shell
❯ julia --startup-file=no --project=@script bin/test_at_script.jl
┌ Info: Check Active Env
│ Base.active_project() = "/Users/alexwadell/Documents/julia_at_script_issue/Project.toml"
│ PROGRAM_FILE = "bin/test_at_script.jl"
│ LOAD_PATH =
│ 3-element Vector{String}:
│ "@"
│ "@v#.#"
│ "@stdlib"
└ Base.load_path_expand("@script") = "Project.toml"
```
However, running from outside of `Documents/julia_at_script_issue` activates the default environment instead:
```shell
❯ julia --project=@script --startup-file=no julia_at_script_issue/bin/test_at_script.jl
┌ Info: Check Active Env
│ Base.active_project() = "/Users/alexwadell/.julia/environments/v1.11/Project.toml"
│ PROGRAM_FILE = "julia_at_script_issue/bin/test_at_script.jl"
│ LOAD_PATH =
│ 3-element Vector{String}:
│ "@"
│ "@v#.#"
│ "@stdlib"
└ Base.load_path_expand("@script") = "julia_at_script_issue/Project.toml"
ERROR: LoadError: AssertionError: /Users/alexwadell/.julia/environments/v1.11/Project.tom
l != /Users/alexwadell/Documents/julia_at_script_issue/Project.toml
Stacktrace:
[1] top-level scope
@ ~/Documents/julia_at_script_issue/bin/test_at_script.jl:5
in expression starting at /Users/alexwadell/Documents/julia_at_script_issue/bin/test_at_scr
ipt.jl:5
```
Oddly, enough `Base.load_path_expand("@cript")` resolves the correct project file.
Setting `JULIA_LOAD_PATH` directly does result in the expect behavior outside the project directory:
```shell
❯ JULIA_LOAD_PATH=@script julia --startup-file=no julia_at_script_issue/bin/test_at_script.jl
┌ Info: Check Active Env
│ Base.active_project() = "julia_at_script_issue/Project.toml"
│ PROGRAM_FILE = "julia_at_script_issue/bin/test_at_script.jl"
│ LOAD_PATH =
│ 1-element Vector{String}:
│ "@script"
└ Base.load_path_expand("@script") = "julia_at_script_issue/Project.toml"
```
## Julia Info
```
Julia Version 1.11.0-rc1
Commit 3a35aec36d1 (2024-06-25 10:23 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: macOS (arm64-apple-darwin22.4.0)
CPU: 8 × Apple M1
WORD_SIZE: 64
LLVM: libLLVM-16.0.6 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)
Environment:
JULIA_PKG_USE_CLI_GIT = true
```
## Related PRs
#50864
#53356
This should be fixed and now that my attention is called to the PR to fix and test it, I’ll make sure those get merged (they seem to be stalled for lack of review). In the meantime, if you put this at the top of your script file, it has the same effect:
push!(empty!(LOAD_PATH), @__DIR__)
7 Likes