Skip to content

Commit eab0f76

Browse files
authored
Merge pull request #16 from DilumAluthge/dpa/get-server-dir
Vendor the fixed `get_server_dir`, but compare its output to that of the upstream `Pkg.PlatformEngines.get_server_dir`, and print a warning if the two values differ
2 parents a2d2d12 + 7794dfb commit eab0f76

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Interactive browser-based authentication to private Julia Pkg servers.
77
#### Step 1: Make sure that PkgAuthentication.jl is installed in the default global Julia package environment (`v1.x`)
88

99
```julia
10-
julia> delete!(ENV, "JULIA_PKG_SERVER")
10+
julia> delete!(ENV, "JULIA_PKG_SERVER");
1111

1212
julia> import Pkg
1313

src/PkgAuthentication.jl

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function authenticate(;
9999

100100
_assert_pkg_server_env_var_is_set()
101101

102-
server = get_pkg_server()::String
102+
server = pkg_server()
103103
server = rstrip(server, '/')
104104
server = string(server, "/", auth_suffix)
105105

@@ -347,20 +347,6 @@ end
347347

348348
## utils
349349

350-
@static if Base.VERSION >= v"1.4-"
351-
# Note that we add `::Union{String, Nothing}` to force conversion from `SubString` to `String`
352-
get_pkg_server()::Union{String, Nothing} = Pkg.pkg_server()
353-
else
354-
# This function does not exist in Julia 1.3
355-
# Note that we add `::Union{String, Nothing}` to force conversion from `SubString` to `String`
356-
function get_pkg_server()::Union{String, Nothing}
357-
server = get(ENV, "JULIA_PKG_SERVER", "https://pkg.julialang.org")
358-
isempty(server) && return nothing
359-
startswith(server, r"\w+://") || (server = "https://$server")
360-
return rstrip(server, '/')
361-
end
362-
end
363-
364350
is_new_auth_mechanism() =
365351
isdefined(Pkg, :PlatformEngines) &&
366352
isdefined(Pkg.PlatformEngines, :get_server_dir) &&
@@ -373,11 +359,25 @@ is_token_valid(toml) =
373359
(get(toml, "expires_at", nothing) isa Union{Integer, AbstractFloat} ||
374360
get(toml, "expires", nothing) isa Union{Integer, AbstractFloat})
375361

376-
@static if Base.VERSION >= v"1.10-" # TODO: change this to "1.9-" if we are able to fix Pkg in time for 1.9
377-
const get_server_dir = Pkg.PlatformEngines.get_server_dir
362+
@static if Base.VERSION >= v"1.4-"
363+
const pkg_server = Pkg.pkg_server
364+
else
365+
# This function does not exist in Julia 1.3
366+
function pkg_server()
367+
server = get(ENV, "JULIA_PKG_SERVER", "https://pkg.julialang.org")
368+
isempty(server) && return nothing
369+
startswith(server, r"\w+://") || (server = "https://$server")
370+
return rstrip(server, '/')
371+
end
372+
end
373+
374+
@static if Base.VERSION >= v"1.10-" # TODO: change this to 1.9 once the nightlies have updated
375+
const _get_server_dir = Pkg.PlatformEngines.get_server_dir
378376
else
379-
# This implementation of `get_server_dir` handles `domain:port` servers correctly (fixed on Pkg#master but not in older Julia versions).
380-
function get_server_dir(url::AbstractString, server = get_pkg_server())
377+
function _get_server_dir(
378+
url::AbstractString,
379+
server::AbstractString,
380+
)
381381
server === nothing && return
382382
url == server || startswith(url, "$server/") || return
383383
m = match(r"^\w+://(?:[^\\/@]+@)?([^\\/:]+)(?:$|/|:)", server)
@@ -389,6 +389,20 @@ else
389389
end
390390
end
391391

392+
function get_server_dir(
393+
url::AbstractString,
394+
server::Union{AbstractString, Nothing} = pkg_server(),
395+
)
396+
server_dir_pkgauth = _get_server_dir(url, server)
397+
server_dir_pkg = Pkg.PlatformEngines.get_server_dir(url, server)
398+
if server_dir_pkgauth != server_dir_pkg
399+
msg = "The PkgAuthentication server directory is not equal to the Pkg server directory." *
400+
"Unexpected behavior may occur."
401+
@warn msg server_dir_pkgauth server_dir_pkg
402+
end
403+
return server_dir_pkgauth
404+
end
405+
392406
function token_path(url::AbstractString)
393407
@static if is_new_auth_mechanism()
394408
server_dir = get_server_dir(url)
@@ -398,7 +412,7 @@ function token_path(url::AbstractString)
398412
end
399413
# older auth mechanism uses a different token location
400414
default = joinpath(Pkg.depots1(), "token.toml")
401-
get(ENV, "JULIA_PKG_TOKEN_PATH", default)
415+
return get(ENV, "JULIA_PKG_TOKEN_PATH", default)
402416
end
403417

404418
const OPEN_BROWSER_HOOK = Ref{Union{Base.Callable, Nothing}}(nothing)
@@ -498,7 +512,7 @@ function install(; maxcount::Integer = 3)
498512
throw(ArgumentError("`maxcount` must be greater than or equal to one"))
499513
end
500514
_assert_pkg_server_env_var_is_set()
501-
server = get_pkg_server()
515+
server = String(pkg_server())
502516
auth_handler = generate_auth_handler(maxcount)
503517
@static if PkgAuthentication.is_new_auth_mechanism()
504518
Pkg.PlatformEngines.register_auth_error_handler(server, auth_handler)

0 commit comments

Comments
 (0)