Perhaps the most sobering realization to emerge from last week's carnage in Barcelona is that not even a country as welcoming to Muslim immigrants as Spain is immune to home-grown Islamist terrorism.

There is little doubt that Islamophobia and the rise of anti-immigration parties in France, Germany, the Netherlands, Britain and even Finland have contributed to the radicalization of young Muslims in those countries. When prominent far-right politicians clamour for burka bans or call Islam an existentialist threat, it's hard not for Muslim youth – immigrants or native born – to take it more or less personally.

But the far right is not a political factor in Spain. The scars of fascism are still fresh enough – Francisco Franco's four-decade-long fascist regime ended only with his death in 1975 – that Spaniards remain particularly allergic to any nativist or isolationist discourse. Spain also benefited enormously from immigration following its 1986 entry into the European Union. And no region of the country has been more welcoming to North African immigrants than Catalonia, of which Barcelona is the capital, in part because such newcomers are seen as more willing to learn Catalan than Spanish-speaking Latin Americans.

Story continues below advertisement

Opinion: Islamic extremism is a growing presence in Spain

The concentration of Moroccan immigrants around Barcelona has been offered up as one explanation for last week's attacks on La Rambla and 120 kilometres west in Cambrils. Yet, Catalonia's Muslim community is arguably Western Europe's most well-integrated. It's true that there are Salafists among them. But Spain's two million Muslims face little of the stigmatization their French or British counterparts live with. Most feel quite at home in their adopted country.

Another unsatisfying explanation for the question of why this happened in Spain is the Islamic State's stated goal of re-establishing a caliphate there. Most of Spain's territory was conquered by 8th-century Muslim invaders who named it al-Andalus. The caliphate's borders were progressively eroded by Christian conquerors and, by the 16th century, most Muslims had been forced to convert or leave the country. But rather than denigrate its Muslim past, modern Spain celebrates it.

Spanish cities abound with medieval Moorish castles and Neo-Mudejar architecture from the late 19th century, when the country experienced a revival in Islamic design. Barcelona itself offers some of the best examples of the latter, from the Arco de Triunfo built for the 1888 World's Fair to Antoni Gaudi's Casa Vicens from the same period. There is no place in Europe with which North African immigrants can feel a greater historical or visual connection. While that might inspire dreams of a renewed caliphate in some, it seems more likely to foster a feeling of familiarity and belonging among Muslim newcomers and their children.

The far more deadly 2004 bombings of Madrid's commuter rail network, linked to an al-Qaeda terrorist cell, differ from last week's attacks in that they were seen as retribution for Spain's heavy involvement in U.S.-led wars in Iraq and Afghanistan. They had little to do with the alienation of young Muslims in Spain. And while Spain is part of the current coalition battling the Islamic State, its contributions have been modest. The Islamic State has a long hit list, but Spain has not been at the top of it.

Still, last week's attacks have changed everything and the political fallout could be even greater than in 2004, when terrorists struck Madrid three days before a national election. This time, Spanish voters could turn on the ruling People's Party of Prime Minister Mariano Rajoy amid reports that the Spanish Interior Ministry failed to pass on CIA warnings of an impending Barcelona attack to Catalonia's Mossos d'Esquadra, the local police force that has had full jurisdiction over the region since 2008.

Distrust between the two levels of government and their respective police forces is at an all-time high after Catalonia's sovereigntist government recently named an ardent separatist to run the Mossos. Madrid insists the Mossos must apply Spanish law to prevent Catalonia from holding an independence referendum on Oct. 1. The head of the Mossos thinks otherwise.

Story continues below advertisement

Last week, Mr. Rajoy, Catalonian President Carles Puigdemont and King Felipe VI put on a united front. But the blame game that followed their display of unity threatens to exacerbate regional tensions rather than attenuate them.

The greatest risk, however, is the scapegoating of the country's Muslims. Spain has set an example for the rest of Europe in the treatment of its Muslim population. It would be a tragedy if it forgets that now.