In a hypothetical war between France and the HRE, is there some legal mechanism that determines which sovereign the Duke of Bar must fight for?
Short answer is that they pick a side and may or may not lose their lands.
Somewhat longer answer, the Last Argument of Kings, ie strongest rules. In practice, France probably has greater sway, depending on who the emperor is, and also what the precise war is about. But Bar was pretty firmly in the French orbit, and only got eaten alongside Lorraine in the 1700s as part of a general landswap deal with Austria.
Contrast the city of Metz, which passed to France in the 1500s by treaty, or the affiliated Three Bishoprics, which were de facto already controlled by France from the 1300s due to their power to appoint the bishops. Legally they stayed part of the Empire until Westphalia.
So not really any mechanism, but the HRE has legal claims that could persist for quite some time, and Emperor Charles did famously besiege Metz after it submitted to France.
Better example is the Hundred Years War. Edward claimed the French throne in part to make it easier to justify Flanders' revolt against France, since then they could claim to be acting on behalf of their rightful liege. Various vagrant types would rove through Occitania and France, giving loose allegiance to one side or the other.
Many of these same concerns were raised during various peace negotiations between England and France, and were a big part of why the conflict lasted as long as it did. The French were willing to be quite generous to the English King if he was willing to accept French suzerainty on the Continent, even restoring most of Aquitaine to English control, but the issue of sovereignty made the matter too difficult for England to accept, since they wanted to know what would happen if (or rather, when) England and France went to war again and the French King demanded his "loyal" vassal the Duke of Aquitaine levy troops/taxes to support the war effort. Since the whole conflict was sparked precisely by the French King sending his agents to meddle in English fiefs, and the clear tendency was for France to try and assert its royal sovereignty over English possessions.