Comparative Advantage is trivially wrong in the real world. If a potential enemy is better at making bullets than you are, you're probably still going to make bullets rather than trading for them, because if you ever come into conflict, you don't want your supply of ammunition choked. This has real world analogues in Chinese-manufactured electrical components that go into military hardware.
From a strategic perspective, the US probably needs to onshore chip manufacturing before China's chip industry reaches parity with Taiwan's. If they don't, China could effectively blockade the island and remain unaffected, while all of its competitors are. The US would lose the initiative, and have to make a reactive decision on whether or not it wants to be in a shooting war, which is a bad place to be.
Econ 101 was nice in theory when the US had no rivals. But it does now, and a country that relies on its military and technological edge as part of its economic strategy (i.e. reserve currency status, exerting soft power through global institutions that are backed up by a credible threat of violence) can't be in a position where it gets outbuilt by its competitors.
Nothing you've said invalidates comparative advantage. It is just accepting a cost (reduced economic output) in favor of some strategic advantage. It doesn't matter if its a good idea, or a moral idea, or a strategic idea, the cost is still a loss in economic output.
Sure, you can think of it as purchasing security with the cost of reduced economic output. The problem is that the benefits of autonomy, and the costs of the externalities of free trade, are difficult if not impossible to accurately value. Not only are they intangible, their true value can often only be assessed in retrospect.