As for tariffs, the U.S. Supreme Court in February curbed the president’s ability to impose duties on countries willy-nilly.

Mr. Kirkegaard added that Mr. Trump would most likely be deterred by the reaction of financial markets, which could look askance at a resumption of a trade war with Europe. Mr. Trump has often shown he is willing to back down when markets recoil from his policies.

Mr. Trump has repeatedly criticized Spain for not committing to spend 5 percent of its gross domestic product on military spending in line with targets set by other members of the Atlantic alliance. Last fall, he threatened to impose higher tariffs on Spain than on the rest of the European Union in hopes of pressuring Madrid to get in line.

Spain has raised its defense spending and agreed to reach a level of 2.1 percent of the size of its economy.

Mr. Sánchez has further piqued Mr. Trump’s ire by criticizing the war in Iran as illegal and refusing to allow the United States to use its military bases during the conflict. That incident in March caused the president to threaten to cut off trade.

Spain’s welcoming policies toward immigrants have also irked Mr. Trump. And Mr. Sánchez has portrayed his program, including in an opinion article in The New York Times, as a better alternative to policies pursued by “MAGA-style leaders.”

Immigrants are a major reason that Spain’s economy is one of the fastest growing in Europe. Economic activity is projected to grow 2.3 percent this year, according to the Bank of Spain.

Spain is less dependent on the U.S. market than many other European countries. Trade between the two countries accounted for roughly 4.4 percent of Spain’s total output compared with around 10 percent of the euro area as a whole, said Miguel Otero-Iglesias, a senior analyst at Elcano Royal Institute in Spain. It also runs a trade deficit with the United States, making it less vulnerable to American pressure.

Still, targeted restrictions could bite. During Mr. Trump’s first term, his administration imposed tariffs on Spain’s black olives, arguing that producers received unfair subsidies. The policy was subsequently challenged and overturned by the World Trade Organization, but Spain’s share of the black olive market in the United States fell precipitously.

While Mr. Trump may no longer welcome Spanish trade or visitors, Spain will undoubtedly continue to open its arms to American investors, not to mention millions of tourists.

The United States is the biggest foreign direct investor in Spain. With some of the cheapest electricity prices in Europe, in part because of the country’s build-out of renewable energy, Spain is also drawing in new investment related to energy-hungry artificial intelligence projects.

As the U.S. State Department wrote in its 2025 investment report: “Spain’s excellent world-class infrastructure, well-educated work force in critical sectors, large domestic market and relatively lower energy costs make it an appealing foreign investment destination.”

Carlos Barragán and Jeanna Smialek contributed reporting.