<p> tags can't have blocks inside them. It causes the browser to close the paragraph, leading to ugly behavior in the block. This means that code blocks break the sidenote <span> with extra </p> tags. This is mentioned here. A potential solution is to include sidenotes after the paragraph ends and use a separate shortcode for the in-text reference (working from something like this), but this breaks the highlighting functionality as it currently works.