[Tex/LaTex] Using listing with minted gets wrong listoflistings

hyperreflistingslistsmintedtable of contents

I wanted to make a \begin{minted} block to show on the ToC in Listings section with a link to its label. So I did (with links to my Github working repo)

\begin{listing}[!h]
    \begin{minted}[
        linenos,
        frame=single,
        numbersep=6pt,
        baselinestretch=1,
        fontfamily=courier,
        gobble=4,
        fontsize=\tiny,
    ]{text}
    code code code
    \end{minted}
    \caption{some caption}
    \label{code:mytextfile}
\end{listing}

using minted listing

to label my code to get the right link, and

\renewcommand\listingscaption{Code}
\renewcommand\listoflistingscaption{List of Source codes}

% https://tex.stackexchange.com/a/99656/5125
\renewcommand{\listoflistings}{%
    \cleardoublepage
    \addcontentsline{toc}{chapter}{\listoflistingscaption}%
    \listof{listing}{\listoflistingscaption}%
}

template and preamble

to show a List of Listings on the ToC (were not showing with a simple \listoflistings, so I put addcontentsline), with custom texts on it. This produces a problem that, at the "Source code List", clicking on the entry for the listing takes the document to the cover. I cannot go better than this: if I change something, or the "List of Source codes" entry takes to "List of Figures", or it disappears.


Edit: duplicate, althought I could not fix my problem yet.

Best Answer

Going against what I thought it was, the problem was the hyperref package. As this other post here hinted, the include's order was bogus, but also I had to add a \phantomsection.

So, my preamble was

\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% color
\usepackage[table,dvipsnames]{xcolor}

% Blue links
\usepackage[unicode,colorlinks=true,linkcolor=blue]{hyperref}

% "Natbib" style for references
\usepackage[backend=biber,bibencoding=utf8,style=numeric-comp]{biblatex}

% \makeglossaries
% http://en.wikibooks.org/wiki/LaTeX/Glossary#Using_defined_terms
\usepackage[xindy,toc]{glossaries}

% http://texblog.org/tag/addcontentsline/
\usepackage[numbib]{tocbibind}

% \newminted{cpp}
% [chapter] to number by chapter
\usepackage[chapter]{minted}

% TODO anotations
\usepackage[colorinlistoftodos,portuguese]{todonotes}

\usepackage{float}
\usepackage{caption}       % \begin{caption}
\usepackage{csquotes}      % required by {babel}
\usepackage{graphicx}      % \includegraphics
\usepackage{subcaption}    % \begin{subfigure}
\usepackage{verbatim}      % \begin{comment}
\usepackage{fullpage}      % thinner margins
\usepackage{indentfirst}   % indent 1st paragraphs
\usepackage{setspace}      % \setstretch
\usepackage{afterpage}     % \afterpage https://tex.stackexchange.com/q/88657

% turning 'fi' ligatures off
% http://www.latex-community.org/forum/viewtopic.php?f=5&t=953#p13896
\usepackage{microtype}
\DisableLigatures{encoding = *, family = *}

Changing the include's order, turned into

\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% color
\usepackage[table,dvipsnames]{xcolor}

\usepackage{caption}       % \begin{caption}
\usepackage{csquotes}      % required by {babel}
\usepackage{graphicx}      % \includegraphics
\usepackage{subcaption}    % \begin{subfigure}
\usepackage{verbatim}      % \begin{comment}
\usepackage{fullpage}      % thinner margins
\usepackage{indentfirst}   % indent 1st paragraphs
\usepackage{setspace}      % \setstretch
\usepackage{afterpage}     % \afterpage https://tex.stackexchange.com/q/88657

% \newminted{cpp}
% [chapter] to number by chapter
\usepackage[chapter]{minted}

% Blue links
\usepackage[unicode,colorlinks=true,linkcolor=blue]{hyperref}

% turning 'fi' ligatures off
% http://www.latex-community.org/forum/viewtopic.php?f=5&t=953#p13896
\usepackage{microtype}
\DisableLigatures{encoding = *, family = *}

% "Natbib" style for references
\usepackage[backend=biber,bibencoding=utf8,style=numeric-comp]{biblatex}

% \makeglossaries
% http://en.wikibooks.org/wiki/LaTeX/Glossary#Using_defined_terms
\usepackage[xindy,toc]{glossaries}

% http://texblog.org/tag/addcontentsline/
\usepackage[numbib]{tocbibind}

% TODO anotations
\usepackage[colorinlistoftodos,portuguese]{todonotes}

and the renew command for \listoflistings with the \phantomsection

% https://tex.stackexchange.com/a/99656/5125
\renewcommand{\listoflistings}{%
    \cleardoublepage
    \phantomsection
    \addcontentsline{toc}{chapter}{\listoflistingscaption}%
    \listof{listing}{\listoflistingscaption}%
}

Now, clicking the list of listings entry on table of contents guides to the right page of the listing.