Skip to content

Commit 583981f

Browse files
authored
Set temporary env whenever Julia is started with --project=@temp (#51149)
This makes it convenient to start and run Julia in a temporary environment. This brings `Pkg.activate(; temp=true)` functionality to cmdline. - Update relevant docs. - Add tests for `--project=@temp` and `JULIA_PROJECT="@temp"`.
1 parent 37f48b2 commit 583981f

File tree

5 files changed

+9
-2
lines changed

5 files changed

+9
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Command-line option changes
4242
* Enabling or disabling color text in Julia can now be controlled with the
4343
[`NO_COLOR`](https://no-color.org/) or [`FORCE_COLOR`](https://force-color.org/) environment
4444
variables. ([#53742]).
45+
* `--project=@temp` starts Julia with a temporary environment.
4546

4647
Multi-threading changes
4748
-----------------------

base/initdefs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ function load_path_expand(env::AbstractString)::Union{String, Nothing}
277277
# if you put a `@.` in LOAD_PATH manually, it's expanded late
278278
env == "@" && return active_project(false)
279279
env == "@." && return current_project()
280+
env == "@temp" && return mktempdir()
280281
env == "@stdlib" && return Sys.STDLIB
281282
if startswith(env, "@script")
282283
if @isdefined(PROGRAM_FILE)

doc/src/manual/command-line-interface.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ The following is a complete list of command-line switches available when launchi
166166
|`-v`, `--version` |Display version information|
167167
|`-h`, `--help` |Print command-line options (this message)|
168168
|`--help-hidden` |Print uncommon options not shown by `-h`|
169-
|`--project[={<dir>\|@.}]` |Set `<dir>` as the active project/environment. The default `@.` option will search through parent directories until a `Project.toml` or `JuliaProject.toml` file is found.|
169+
|`--project[={<dir>\|@temp\|@.}]` |Set `<dir>` as the active project/environment. Or, create a temporary environment with `@temp`. The default `@.` option will search through parent directories until a `Project.toml` or `JuliaProject.toml` file is found.|
170170
|`-J`, `--sysimage <file>` |Start up with the given system image file|
171171
|`-H`, `--home <dir>` |Set location of `julia` executable|
172172
|`--startup-file={yes*\|no}` |Load `JULIA_DEPOT_PATH/config/startup.jl`; if [`JULIA_DEPOT_PATH`](@ref JULIA_DEPOT_PATH) environment variable is unset, load `~/.julia/config/startup.jl`|

src/jloptions.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ static const char opts[] =
115115
" --help-hidden Print uncommon options not shown by `-h`\n\n"
116116

117117
// startup options
118-
" --project[={<dir>|@.}] Set <dir> as the active project/environment.\n"
118+
" --project[={<dir>|@temp|@.}] Set <dir> as the active project/environment.\n"
119+
" Or, create a temporary environment with `@temp`\n"
119120
" The default @. option will search through parent\n"
120121
" directories until a Project.toml or JuliaProject.toml\n"
121122
" file is found.\n"

test/cmdlineargs.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
257257
@test expanded == readchomp(addenv(`$exename -e 'println(Base.active_project())'`, "JULIA_PROJECT" => "@foo", "HOME" => homedir()))
258258
end
259259

260+
# handling of `@temp` in --project and JULIA_PROJECT
261+
@test tempdir() == readchomp(`$exename --project=@temp -e 'println(Base.active_project())'`)[1:lastindex(tempdir())]
262+
@test tempdir() == readchomp(addenv(`$exename -e 'println(Base.active_project())'`, "JULIA_PROJECT" => "@temp", "HOME" => homedir()))[1:lastindex(tempdir())]
263+
260264
# --quiet, --banner
261265
let p = "print((Base.JLOptions().quiet, Base.JLOptions().banner))"
262266
@test read(`$exename -e $p`, String) == "(0, -1)"

0 commit comments

Comments
 (0)