Notice that the points where the tangent function is undefined are exactly the same as the points to which your second method leads you, where the cosine is $0$. In that sense, you're getting the same answer both ways.
I don't often see this mentioned except when I mention it: in trigonometry, as when working with rational functions, it makes sense to take the codomain of the functions to be $\mathbb R\cup\{\infty\}$, where $\infty$ is neither $+\infty$ nor $-\infty$, but rather is a single $\infty$ at both ends of the real line. That makes trigonometric functions everywhere continuous. Then you'd say $1/0=\infty$ and $\tan(\pi/2)=\infty$ and $\cot(\pi/2)=0$.