<h1 dir="auto">Environment</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Windows build number: Microsoft Windows [Version 10.0.18362.239]
Windows Terminal version (if applicable): (built from latest source as of July 17th, 2019, morning)"><pre lang="none" class="notranslate"><code class="notranslate">Windows build number: Microsoft Windows [Version 10.0.18362.239]
Windows Terminal version (if applicable): (built from latest source as of July 17th, 2019, morning)
</code></pre></div>
<p dir="auto">I am working on implementing some new VT escape sequences (PR <a href="https://github.com/microsoft/terminal/pull/1978" data-hovercard-type="pull_request" data-hovercard-url="/microsoft/terminal/pull/1978/hovercard">here</a>), but I've run into a strange problem. What the sequences do (or are supposed to do) is not important here. The problem is that in certain circumstances, something is <em>reordering</em> VT escape sequences in my content (which is very surprising to me).</p>
<p dir="auto">The control sequences in question are XTPUSHSGR and XTPOPSGR: <code class="notranslate">[CSI]#p</code> and <code class="notranslate">[CSI]#q</code>, respectively. Note in the string below, how there is an XTPUSHSGR sequence, then a normal SGR sequence to set the color to red, then an XTPOPSGR sequence (and then a normal SGR sequence to reset the color).</p>
<p dir="auto">When I open OpenConsole.sln in Visual Studio 2019, with the CascadiaPackage set as the default project to debug, and then <kbd>F5</kbd> to start debugging it, it launches a terminal running Windows PowerShell. When I send my string to the console, something is <em>moving</em> the XTPUSHSGR and XTPOPSGR sequences to the very beginning of the string!</p>
<h1 dir="auto">Steps to reproduce</h1>

<ol dir="auto">
<li>Start OpenConsole.sln in Visual Studio 2019. Make sure CascadiaPackage is the default project, and platform is set to x64, and build the solution if necessary.</li>
<li><kbd>F5</kbd> to start debugging a new instance. You should now have a PowerShell instance in the terminal.</li>
<li>In the PowerShell instance in the terminal, run the following commands:</li>
</ol>
<div class="highlight highlight-source-powershell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content=" $esc = [char] 0x1b
 $s = &quot;Hello. Push:${esc}[#p ${esc}[91m this text is RED Pop:${esc}[#q FG should &quot; +
      &quot;no longer be red (unless it was red before) courtesy reset: ${esc}[0m&quot;"><pre class="notranslate"> <span class="pl-smi">$esc</span> <span class="pl-k">=</span> [<span class="pl-k">char</span>] <span class="pl-c1">0x1b</span>
 <span class="pl-smi">$s</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>Hello. Push:<span class="pl-smi">${esc}</span>[#p <span class="pl-smi">${esc}</span>[91m this text is RED Pop:<span class="pl-smi">${esc}</span>[#q FG should <span class="pl-pds">"</span></span> <span class="pl-k">+</span>
      <span class="pl-s"><span class="pl-pds">"</span>no longer be red (unless it was red before) courtesy reset: <span class="pl-smi">${esc}</span>[0m<span class="pl-pds">"</span></span></pre></div>
<ol start="4" dir="auto">
<li>Set a breakpoint at <code class="notranslate">TerminalControl.dll!Microsoft::Console::VirtualTerminal::StateMachine::ProcessString</code> (the two-parameter one).</li>
<li>In the PowerShell instance in the terminal, run the command <code class="notranslate">$s</code> to output the string.</li>
<li>When you hit the breakpoint, observe the value of <code class="notranslate">rgwch</code>.</li>
</ol>
<h1 dir="auto">Expected behavior</h1>

<p dir="auto">The content of the string should be just as it was set in PowerShell.</p>
<h1 dir="auto">Actual behavior</h1>

<p dir="auto">The XTPUSHSGR and XTPOPSGR sequences have been moved to the beginning of the string!</p>
<p dir="auto">My first thought was that PowerShell must be doing something funny, but I attached windbg to the PowerShell and set a breakpoint on kernel32!WriteConsoleW, and the string passed in there is correct. So something is happening after the string has left PowerShell's hands.</p>
<h1 dir="auto">Steps to reproduce</h1>
<p dir="auto">build OpenConsole.sln</p>

<h1 dir="auto">Expected behavior</h1>
<p dir="auto">conpty.dll should be output</p>

<h1 dir="auto">Actual behavior</h1>
<p dir="auto">conpty is built as a static lib</p>