Alvaro Morata's late strike snatched Spain a 1-0 Nations League victory over Portugal in Braga on Tuesday and sent them into the semi-finals.
La Roja needed to win to finish top of League A Group 2, but struggled on another difficult night for the Euro 2020 semi-finalists, until Morata's 88th minute breakthrough.
Spain have not been able to hit top form in the internationals leading up to the Qatar 2022 World Cup, but this hard-fought victory provides a welcome morale boost.
Portugal had the better of the game but Spain brightened in the final stages, with the introduction of several substitutes, including Nico Williams, who headed across goal for Morata to gleefully turn in at the back post.
Luis Enrique made wholesale changes to the side that suffered Spain's first home defeat since 2018 against Switzerland on Saturday, retaining only four starters.
Morata was brought in to lead the line, while the Asturian coach rotated his entire midfield trio.
Spain still dominated possession, as their gameplan demands, but to little effect as Portugal created the more dangerous openings in the first half.
Athletic Bilbao goalkeeper Unai Simon made a solid save to deny Ruben Neves and then a brilliant one to keep Liverpool striker Diogo Jota at bay.
Cristiano Ronaldo appealed for a penalty in a race for the ball with Simon but the referee spotted he went to ground without any contact and waved play on.
With Portuguese confidence increasing, Bruno Fernandes lashed an effort narrowly off-target, celebrated prematurely by swathes of the stadium as the ball appeared to settle in the net.
Just as they did against Switzerland, Spain failed to get a single shot on target in the first half, and after Simon made another good save from Ronaldo early in the second half, Luis Enrique took action.
The coach sent on Pedri, Gavi and Yeremy Pino to try and give La Roja more inspiration going forward, although it was the hosts who almost broke the deadlock when Dani Carvajal deflected a strike narrowly over his own crossbar.
At the other end Morata replied with a tame effort which was easily collected by Diogo Costa, who also thwarted Spain substitute Williams.
The changes livened Luis Enrique's team up and Costa was required again to shovel away a far more emphatic strike from Atletico Madrid striker Morata, before the breakthrough ultimately arrived.
Dani Carvajal launched a crossfield ball into the area, with Williams' header leaving Morata with the simple task of firing into the empty net to put the runners-up of the last edition of the Nations League back into the final four.