Skip to content

Commit e342f1c

Browse files
topolarityKristofferC
authored andcommitted
trimming: Avoid using indirectly-loaded packages (#57589)
Although a package is loaded, we may not have top-level `using` rights. Instead, we need to pull the Module straight out of the loaded modules array in order to do these hacks. (cherry picked from commit 1cead2b)
1 parent 6f34f91 commit e342f1c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

contrib/juliac-buildscript.jl

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,15 @@ let mod = Base.include(Base.__toplevel__, inputfile)
208208
end
209209

210210
# Additional method patches depending on whether user code loads certain stdlibs
211+
let loaded = Base.loaded_modules_array()
212+
function find_loaded_module(name)
213+
idx = findfirst((m) -> Symbol(m) === name, loaded)
214+
idx === nothing && return nothing
215+
return loaded[idx]
216+
end
211217

212-
let loaded = Symbol.(Base.loaded_modules_array()) # TODO better way to do this
213-
if :SparseArrays in loaded
214-
using SparseArrays
218+
SparseArrays = find_loaded_module(:SparseArrays)
219+
if SparseArrays !== nothing
215220
@eval SparseArrays.CHOLMOD begin
216221
function __init__()
217222
ccall((:SuiteSparse_config_malloc_func_set, :libsuitesparseconfig),
@@ -225,8 +230,9 @@ let loaded = Symbol.(Base.loaded_modules_array()) # TODO better way to do this
225230
end
226231
end
227232
end
228-
if :Artifacts in loaded
229-
using Artifacts
233+
234+
Artifacts = find_loaded_module(:Artifacts)
235+
if Artifacts !== nothing
230236
@eval Artifacts begin
231237
function _artifact_str(__module__, artifacts_toml, name, path_tail, artifact_dict, hash, platform, _::Val{lazyartifacts}) where lazyartifacts
232238
# If the artifact exists, we're in the happy path and we can immediately
@@ -241,26 +247,30 @@ let loaded = Symbol.(Base.loaded_modules_array()) # TODO better way to do this
241247
end
242248
end
243249
end
244-
if :Pkg in loaded
245-
using Pkg
250+
251+
Pkg = find_loaded_module(:Pkg)
252+
if Pkg !== nothing
246253
@eval Pkg begin
247254
__init__() = rand() #TODO, methods that do nothing don't get codegened
248255
end
249256
end
250-
if :StyledStrings in loaded
251-
using StyledStrings
257+
258+
StyledStrings = find_loaded_module(:StyledStrings)
259+
if StyledStrings !== nothing
252260
@eval StyledStrings begin
253261
__init__() = rand()
254262
end
255263
end
256-
if :Markdown in loaded
257-
using Markdown
264+
265+
Markdown = find_loaded_module(:Markdown)
266+
if Markdown !== nothing
258267
@eval Markdown begin
259268
__init__() = rand()
260269
end
261270
end
262-
if :JuliaSyntaxHighlighting in loaded
263-
using JuliaSyntaxHighlighting
271+
272+
JuliaSyntaxHighlighting = find_loaded_module(:JuliaSyntaxHighlighting)
273+
if JuliaSyntaxHighlighting !== nothing
264274
@eval JuliaSyntaxHighlighting begin
265275
__init__() = rand()
266276
end

0 commit comments

Comments
 (0)