It depends.
Some weapons systems, such as the F-35, are developed from the outset in conjunction with international partners. The United Kingdom, Italy, the Netherlands, Turkey, Australia, Norway, Denmark, and Canada together contributes $4.4 billion towards development costs of the F-35. The United Kingdom contributed 10% of the planned development costs as the sole Tier 1 partner.
Other weapons systems are developed by the likes of Boeing and Lockheed and are never picked up by any military, so it is the shareholders--and not the taxpayers--who are on the hook for that. Lockheed's Skunkworks does a lot of that. Some of what they learn gets incorporated into subsequent designs that are picked up by the military. But plenty of it just goes in the trash.
And then there are other weapons systems for which there is a design competition. The F-35 is another example. Boeing also submitted a design in the Joint Strike Fighter design competition (the XF-32), but it ultimately wasn't chosen. The taxpayers don't pay for that; Boeing bears all the risk there.