\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\path node(n1){text};
\path[draw,line cap=rect](n1.north east)--(n1.north west);
% uncomment following line to see gaps clearly
%\path[draw](n1.north west)--(n1.south west);
\end{tikzpicture}
\end{document}

What is going on here? Let's start with
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\path node[draw](n1){text};
\path[fill=blue](n1.north east) circle[radius=0.1pt]
(n1.north west)circle[radius=0.1pt];
\path[draw,red](n1.north east)--(n1.north west);
\end{tikzpicture}
\end{document}

This does not have any unnecessary gap. If you uncomment
\path[draw,red](n1.north east)--(n1.north west);
you get

So the gap is there. This is because TikZ is rather "generous" with the bounding box, here it adds the "natural extension" of the line which emerges when you do line cap=rect. Note that the bounding box gets much more "overestimated", see e.g. here. AFAIK there is no built-in way to obtain the exact bounding box.