Skip to content

Commit 86f73b0

Browse files
dralletjefonsp
authored andcommitted
Pkg.activate inside macros should still deactivate nbpkg, fixes #1475
Had to force push, you better do something silly @fonsp
1 parent bb203f1 commit 86f73b0

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/analysis/ExpressionExplorer.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,35 @@ function strip_indexing(x, inside::Bool=false)
266266
end
267267
end
268268

269+
"Module so I don't pollute the whole ExpressionExplorer scope"
270+
module MacroHasSpecialHeuristicInside
271+
import ...Pluto
272+
import ..ExpressionExplorer, ..SymbolsState
273+
274+
"""
275+
Uses `cell_precedence_heuristic` to determine if we need to include the contents of this macro in the symstate.
276+
This helps with things like a Pkg.activate() that's in a macro, so Pluto still understands to disable nbpkg.
277+
"""
278+
function macro_has_special_heuristic_inside(; symstate::SymbolsState, expr::Expr)::Bool
279+
# Also, because I'm lazy and don't want to copy any code, imma use cell_precedence_heuristic here.
280+
# Sad part is, that this will also include other symbols used in this macro... but come'on
281+
local fake_cell = Pluto.Cell()
282+
local fake_reactive_node = Pluto.ReactiveNode(symstate)
283+
local fake_expranalysiscache = Pluto.ExprAnalysisCache(
284+
parsedcode=expr,
285+
module_usings_imports=ExpressionExplorer.compute_usings_imports(expr),
286+
)
287+
local fake_topology = Pluto.NotebookTopology(
288+
nodes=Pluto.DefaultDict(Pluto.ReactiveNode, Dict(fake_cell => fake_reactive_node)),
289+
codes=Pluto.DefaultDict(Pluto.ExprAnalysisCache, Dict(fake_cell => fake_expranalysiscache))
290+
)
291+
292+
return Pluto.cell_precedence_heuristic(fake_topology, fake_cell) < 8
293+
end
294+
# Having written this... I know I said I was lazy... I was wrong
295+
end
296+
297+
269298
###
270299
# MAIN RECURSIVE FUNCTION
271300
###
@@ -381,7 +410,16 @@ function explore!(ex::Expr, scopestate::ScopeState)::SymbolsState
381410
# "You should make a new function for that" they said, knowing I would take the lazy route.
382411
for arg in ex.args[begin+1:end]
383412
macro_symstate = explore!(arg, ScopeState())
384-
union!(symstate, SymbolsState(macrocalls=macro_symstate.macrocalls))
413+
414+
# Also, when this macro has something special inside like `Pkg.activate()`,
415+
# we're going to treat it as normal code (so these heuristics trigger later)
416+
# (Might want to also not let this to @eval macro, as an extra escape hatch if you
417+
# really don't want pluto to see your Pkg.activate() call)
418+
if arg isa Expr && MacroHasSpecialHeuristicInside.macro_has_special_heuristic_inside(symstate=macro_symstate, expr=arg)
419+
union!(symstate, macro_symstate)
420+
else
421+
union!(symstate, SymbolsState(macrocalls=macro_symstate.macrocalls))
422+
end
385423
end
386424

387425
# Some macros can be expanded on the server process

test/ExpressionExplorer.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,30 @@ Some of these @test_broken lines are commented out to prevent printing to the te
452452
macrocalls=[Symbol("@parent"), Symbol("@child"), Symbol("@grandchild")],
453453
)
454454
end
455+
@testset "Macros and heuristics" begin
456+
@test test_expression_explorer(
457+
expr=:(@macro import Pkg),
458+
macrocalls=[Symbol("@macro")],
459+
definitions=[:Pkg],
460+
)
461+
@test test_expression_explorer(
462+
expr=:(@macro Pkg.activate("..")),
463+
macrocalls=[Symbol("@macro")],
464+
references=[:Pkg],
465+
funccalls=[[:Pkg, :activate]],
466+
)
467+
@test test_expression_explorer(
468+
expr=:(@macro Pkg.add("Pluto.jl")),
469+
macrocalls=[Symbol("@macro")],
470+
references=[:Pkg],
471+
funccalls=[[:Pkg, :add]],
472+
)
473+
@test test_expression_explorer(
474+
expr=:(@macro include("Firebasey.jl")),
475+
macrocalls=[Symbol("@macro")],
476+
funccalls=[[:include]],
477+
)
478+
end
455479
@testset "String interpolation & expressions" begin
456480
@test testee(:("a $b"), [:b], [], [], [])
457481
@test testee(:("a $(b = c)"), [:c], [:b], [], [])

0 commit comments

Comments
 (0)