<p dir="auto">I assumed <code class="notranslate">macroexpand</code> is supposed to produce the same code whether or not if it expands all the macros at once or sequentially expands macros with <code class="notranslate">recursive = false</code> option.</p>
<p dir="auto">Here is a MRE where <code class="notranslate">macroexpand(m, ex; recursive = true)</code> produces valid code while sequential application of <code class="notranslate">macroexpand(m, ex; recursive = false)</code> leads to different code (and invalid code as a consequence).</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="using Test

ex = :(@test_warn &quot;foo&quot; println(stderr::IO, &quot;foo&quot;))

fully_expanded = macroexpand(@__MODULE__, ex)
Core.eval(@__MODULE__, fully_expanded) # runs w/o any problem

partially_expanded = macroexpand(@__MODULE__, ex; recursive = false)                  # expand `@test_warn`
partially_expanded2 = macroexpand(@__MODULE__, partially_expanded; recursive = false) # expand `@test`
Core.eval(@__MODULE__, partially_expanded2) # leads to syntax error
Core.eval(@__MODULE__, partially_expanded)  # this also leads to syntax error"><pre class="notranslate"><span class="pl-k">using</span> Test

ex <span class="pl-k">=</span> :(<span class="pl-c1">@test_warn</span> <span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span> <span class="pl-c1">println</span>(<span class="pl-c1">stderr</span><span class="pl-k">::</span><span class="pl-c1">IO</span>, <span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>))

fully_expanded <span class="pl-k">=</span> <span class="pl-c1">macroexpand</span>(<span class="pl-c1">@__MODULE__</span>, ex)
Core<span class="pl-k">.</span><span class="pl-c1">eval</span>(<span class="pl-c1">@__MODULE__</span>, fully_expanded) <span class="pl-c"><span class="pl-c">#</span> runs w/o any problem</span>

partially_expanded <span class="pl-k">=</span> <span class="pl-c1">macroexpand</span>(<span class="pl-c1">@__MODULE__</span>, ex; recursive <span class="pl-k">=</span> <span class="pl-c1">false</span>)                  <span class="pl-c"><span class="pl-c">#</span> expand `@test_warn`</span>
partially_expanded2 <span class="pl-k">=</span> <span class="pl-c1">macroexpand</span>(<span class="pl-c1">@__MODULE__</span>, partially_expanded; recursive <span class="pl-k">=</span> <span class="pl-c1">false</span>) <span class="pl-c"><span class="pl-c">#</span> expand `@test`</span>
Core<span class="pl-k">.</span><span class="pl-c1">eval</span>(<span class="pl-c1">@__MODULE__</span>, partially_expanded2) <span class="pl-c"><span class="pl-c">#</span> leads to syntax error</span>
Core<span class="pl-k">.</span><span class="pl-c1">eval</span>(<span class="pl-c1">@__MODULE__</span>, partially_expanded)  <span class="pl-c"><span class="pl-c">#</span> this also leads to syntax error</span></pre></div>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; macro foo()
       f = esc(:f); :(let $f(x) = x; $f(3); end)
       end
@foo (macro with 1 method)

julia&gt; @foo
ERROR: syntax: invalid function name &quot;(escape f)&quot;

julia&gt; macroexpand(:(@foo))
:($(Expr(:error, &quot;invalid function name \&quot;(escape f)\&quot;&quot;)))"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-k">macro</span> <span class="pl-en">foo</span>()
       f <span class="pl-k">=</span> <span class="pl-en">esc</span>(<span class="pl-c1">:f</span>); :(<span class="pl-k">let</span> <span class="pl-k">$</span><span class="pl-en">f</span>(x) <span class="pl-k">=</span> x; <span class="pl-k">$</span><span class="pl-c1">f</span>(<span class="pl-c1">3</span>); <span class="pl-k">end</span>)
       <span class="pl-k">end</span>
<span class="pl-c1">@foo</span> (<span class="pl-k">macro</span> with <span class="pl-c1">1</span> method)

julia<span class="pl-k">&gt;</span> <span class="pl-c1">@foo</span>
ERROR<span class="pl-k">:</span> syntax<span class="pl-k">:</span> invalid <span class="pl-k">function</span> name <span class="pl-s"><span class="pl-pds">"</span>(escape f)<span class="pl-pds">"</span></span>

julia<span class="pl-k">&gt;</span> <span class="pl-c1">macroexpand</span>(:(<span class="pl-c1">@foo</span>))
:(<span class="pl-k">$</span>(<span class="pl-c1">Expr</span>(<span class="pl-c1">:error</span>, <span class="pl-s"><span class="pl-pds">"</span>invalid function name <span class="pl-cce">\"</span>(escape f)<span class="pl-cce">\"</span><span class="pl-pds">"</span></span>)))</pre></div>
<p dir="auto">It looks like <code class="notranslate">macroexpand.scm</code> is forgetting to unescape somewhere.   <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/JeffBezanson/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/JeffBezanson">@JeffBezanson</a>, any ideas?</p>
<p dir="auto">(I'm running into this when trying to fix a bug I found in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="202254218" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/20164" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/20164/hovercard" href="https://github.com/JuliaLang/julia/pull/20164">#20164</a>.)</p>