Why `fill` covers `append after command` elements?
Late options don't cover every property. The append after command
is a clever shortcut mechanism for \pgfpositionnodelater<...>
commands (here in particular \pgfpositionnodelaterpath
). Hence the fill has been already stored but not executed. However the shape etc. has been decided otherwise you wouldn't be able to use that trick for placements. After the placement and other details then \pgfpositionnodenow
is invoked and it overprints whatever there was before it.
You can argue maybe the drawing/filling/clipping might have been separated but that would be a feature request for T.Tantau.
I'm really indebted to @Zarko for drawing this question to my attention in this post. My proposal is to draw things on the foreground layer.
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{backgrounds}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\begin{document}
\begin{tikzpicture}
%Left figure: Works without `fill`.
\begin{scope}[xshift=-4cm]
\node[draw, minimum size=3cm, append after command={\pgfextra \node[anchor=north] at (\tikzlastnode.north) {A};\endpgfextra}] {test};
\end{scope}
%Center figure. Fill covers everything inside
\node[draw, minimum size=3cm, fill=green, append after command={\pgfextra{
\begin{pgfonlayer}{foreground}
\node[anchor=north] at (\tikzlastnode.north) {A};
\end{pgfonlayer}}}] {test};
%Right figure: two commands
\begin{scope}[xshift=4cm]
\node[draw, minimum size=3cm, fill=green] (test) {test};
\node[anchor=north] at (test.north) {A};
\end{scope}
\end{tikzpicture}
\end{document}