I have this slight variation of an example from the answers package

\documentclass[12pt,a4paper]{article}
\usepackage{ifthen}
\Newassociation{sol}{Solution}{ans}
\newboolean{InSol}\setboolean{InSol}{False}
\newenvironment{Sol}
% Need to know whether we are in a solution or not
% So we set a defined boolean to test this
{\setboolean{InSol}{True}\begin{sol}}
%
{\end{sol}\setboolean{InSol}{False}}

\begin{document}
\Opensolutionfile{ans}[ans1]

First exercise

\begin{Sol}
First solution.
\end{Sol}

Second exercise

\begin{Sol}
Second solution.
\end{Sol}
\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}


And it produces following answer file

\begin{Solution}{}

First solution.

\end{Sol}

Second exercise

\begin{Sol}

Second solution.

\end{Sol}
\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}


Is this a bug or am I doing something stupid?

You should not use \begin{sol} and \end{sol} in the definition of the environment, but \sol and \endsol. There's no need to reset the conditional to false, because the grouping provided by the environment will do it.

\documentclass[12pt,a4paper]{article}
\usepackage{ifthen}

\Newassociation{sol}{Solution}{ans}

\newboolean{InSol}\setboolean{InSol}{false}
\newenvironment{Sol}
% Need to know whether we are in a solution or not
% So we set a defined boolean to test this
{\setboolean{InSol}{true}\sol}
{\endsol}

\begin{document}
\Opensolutionfile{ans}[\jobname ans]

First exercise

\begin{Sol}
First solution.
\end{Sol}

Second exercise

\begin{Sol}
Second solution.
\end{Sol}
\Closesolutionfile{ans}
\section{Solutions}
\input{\jobname ans}
\end{document}


## Contents of the answers file

\begin{Solution}{}
First solution.
\end{Solution}
\begin{Solution}{}
Second solution.
\end{Solution}

• @Jack It's not in the documentation, as far as I can see, but the suspect it was needed came to mind as soon as I saw the strange behavior. – egreg Mar 10 '16 at 22:13