Skip to content

Commit

Permalink
Replace UnstableIO with IOContext{IO} (#3735)
Browse files Browse the repository at this point in the history
(cherry picked from commit 195e17e)
  • Loading branch information
mkitti authored and KristofferC committed Apr 25, 2024
1 parent bd78795 commit 7fc3bdb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ext/REPLExt/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let
original_load_path = copy(LOAD_PATH)
__init__()
Pkg.UPDATED_REGISTRY_THIS_SESSION[] = true
Pkg.DEFAULT_IO[] = Pkg.UnstableIO(devnull)
Pkg.DEFAULT_IO[] = Pkg.unstableio(devnull)
withenv("JULIA_PKG_SERVER" => nothing, "JULIA_PKG_UNPACK_REGISTRY" => nothing) do
tmp = Pkg._run_precompilation_script_setup()
cd(tmp) do
Expand Down
6 changes: 3 additions & 3 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import FileWatching

import Base: StaleCacheKey

import ..depots, ..depots1, ..logdir, ..devdir, ..printpkgstyle, ..UnstableIO
import ..depots, ..depots1, ..logdir, ..devdir, ..printpkgstyle
import ..Operations, ..GitTools, ..Pkg, ..Registry
import ..can_fancyprint, ..pathrepr, ..isurl, ..PREV_ENV_PATH
using ..Types, ..TOML
Expand Down Expand Up @@ -1146,8 +1146,8 @@ function precompile(ctx::Context, pkgs::Vector{PackageSpec}; internal_call::Bool
end

io = ctx.io
if io isa UnstableIO
# precompile does quite a bit of output and using the UnstableIO can cause
if io isa IOContext{IO}
# precompile does quite a bit of output and using the IOContext{IO} can cause
# some slowdowns, the important part here is to not specialize the whole
# precompile function on the io
io = io.io
Expand Down
6 changes: 3 additions & 3 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ..Artifacts: ensure_artifact_installed, artifact_names, extract_all_hashe
artifact_exists, select_downloadable_artifacts
using Base.BinaryPlatforms
import ...Pkg
import ...Pkg: pkg_server, Registry, pathrepr, can_fancyprint, printpkgstyle, stderr_f, OFFLINE_MODE, UnstableIO
import ...Pkg: pkg_server, Registry, pathrepr, can_fancyprint, printpkgstyle, stderr_f, OFFLINE_MODE
import ...Pkg: UPDATED_REGISTRY_THIS_SESSION, RESPECT_SYSIMAGE_VERSIONS, should_autoprecompile

#########
Expand Down Expand Up @@ -2108,8 +2108,8 @@ end
function subprocess_handler(cmd::Cmd, ctx, sandbox_ctx, error_msg::String)
stdout = sandbox_ctx.io
stderr = stderr_f()
stdout isa UnstableIO && (stdout = stdout.io)
stderr isa UnstableIO && (stderr = stderr.io)
stdout isa IOContext{IO} && (stdout = stdout.io)
stderr isa IOContext{IO} && (stderr = stderr.io)
@debug "Running command" cmd
p = run(pipeline(ignorestatus(cmd); stdout, stderr), wait = false)
interrupted = false
Expand Down
19 changes: 11 additions & 8 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ const RESPECT_SYSIMAGE_VERSIONS = Ref(true)
# For globally overriding in e.g. tests
const DEFAULT_IO = Ref{Union{IO,Nothing}}(nothing)

struct UnstableIO <: IO
io::IO
# See discussion in https://github.com/JuliaLang/julia/pull/52249
function unstableio(@nospecialize(io::IO))
# Needed to prevent specialization https://github.com/JuliaLang/julia/pull/52249#discussion_r1401199265
_io = Base.inferencebarrier(io)
IOContext{IO}(
_io,
get(_io,:color,false) ? Base.ImmutableDict{Symbol,Any}(:color, true) : Base.ImmutableDict{Symbol,Any}()
)
end
Base.write(io::UnstableIO, b::UInt8) = write(io.io, b)::Int
Base.get(io::UnstableIO, val, default) = get(io.io, val, default)
Base.print(io::UnstableIO, arg::Union{SubString{String}, String}) = print(io.io, arg)
stderr_f() = something(DEFAULT_IO[], UnstableIO(stderr))
stdout_f() = something(DEFAULT_IO[], UnstableIO(stdout))
stderr_f() = something(DEFAULT_IO[], unstableio(stderr))
stdout_f() = something(DEFAULT_IO[], unstableio(stdout))
const PREV_ENV_PATH = Ref{String}("")

can_fancyprint(io::IO) = ((io isa Base.TTY) || (io isa UnstableIO && io.io isa Base.TTY)) && (get(ENV, "CI", nothing) != "true")
can_fancyprint(io::IO) = ((io isa Base.TTY) || (io isa IOContext{IO} && io.io isa Base.TTY)) && (get(ENV, "CI", nothing) != "true")
should_autoprecompile() = Base.JLOptions().use_compiled_modules == 1 && Base.get_bool_env("JULIA_PKG_PRECOMPILE_AUTO", true)

include("utils.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ let

Pkg.UPDATED_REGISTRY_THIS_SESSION[] = true
# Default 30 sec grace period means we hang 30 seconds before precompiling finishes
DEFAULT_IO[] = UnstableIO(devnull)
DEFAULT_IO[] = unstableio(devnull)
Downloads.DOWNLOADER[] = Downloads.Downloader(; grace = 1.0)

# We need to override JULIA_PKG_UNPACK_REGISTRY to fix https://github.com/JuliaLang/Pkg.jl/issues/3663
Expand Down

0 comments on commit 7fc3bdb

Please sign in to comment.