Corey A. Stewart at his primary election watch party in Woodbridge on June 13. Stewart came close to winning the Republican gubernatorial nomination in Virginia. (Linda Davidson/The Washington Post)

It wasn’t supposed to happen this way. Pre-election polls suggested that former Republican National Committee Chairman Ed Gillespie would coast to an easy victory in Virginia’s Republican gubernatorial primary June 13.

After all, Gillespie outspent his competitors several times over in the spring campaign, and he’d become a darling of the state’s Republican establishment when he almost defeated the supposedly unbeatable Democrat Mark Warner in Virginia’s U.S. Senate contest three years ago.

But as primary night dragged on, insurgent Republican candidate Corey A. Stewart stubbornly refused to give up, at one point coming within 1,000 votes of wresting the gubernatorial nomination from Gillespie.

Finally, with all precincts reporting, the unofficial tally from the Virginia Board of Elections gave Gillespie the GOP gubernatorial nod by a little more than 4,300 votes, out of more than 366,000 cast in the party’s primary.

Stewart, the onetime chairman of Donald Trump’s Virginia campaign, had campaigned for gun rights and against illegal aliens and mounted an unapologetic defense of monuments venerating Virginia’s Confederate Civil War heroes. He railed against “political correctness” and frequently goaded Gillespie, calling him “Establishment Ed.”

In the process, Stewart rallied Virginia voters loyal to President Trump: voters who were angry at being ignored by the political establishment, who felt left behind by economic conditions and who were fed up with rapid cultural changes that they felt devalued their ancestors and their own sense of place in society.

Opinion surveys didn’t capture this intensity, much as the conventional wisdom had failed to recognize the scope of public discontent that propelled Trump into the White House last year. Stewart’s near-victory in Virginia makes it clear that Trump voters are still angry and still eager to make a difference at the polls.

It’s no surprise that this year’s political campaign in Virginia is viewed as an early referendum on the Trump presidency and may offer some clues about the congressional mid-term elections next year.

In the immediate future, unhappy Trump voters could mean trouble for Gillespie, who held the president and his policies at arm’s length during the primary campaign. Gillespie insists he is a true conservative who supports tax cuts and business growth.

To have any chance of winning the general election this fall, Gillespie needs Trump voters. But he also needs to appeal to the moderate and independent voters who have been key to success in Virginia elections.

It will be a tricky path ahead for Gillespie. Trump’s disapproval ratings are high with Virginia’s general electorate. According to a recent Washington Post-Schar School survey, only 36 percent of Virginians approve of the president’s performance.

Gillespie also faces Virginia Democrats who are fired up by their dislike of Trump and who appear to be uniting quickly behind Lt. Gov. Ralph Northam, the Democratic nominee for governor, after their own spirited but respectful primary contest.

The overall enthusiasm level favors Democrats. A record 542,812 voters participated in the Democratic primary for governor; 366,100 voted Republican.

Challenger Tom Perriello wasted no time primary night congratulating Northam for “a great victory” and offering his “full and unequivocal support” to the Democratic ticket.

In contrast, Stewart told his supporters, “There’s a word you won’t hear from me, that’s unity.… We’ve been backing down too long in defense of our culture, our heritage and our country.”

Trump-Stewart voters certainly won’t vote for the Democrats in November. The big question is whether they’ll vote Republican or stay home in protest.

Mark J. Rozell is dean of the Schar School of Policy and Government at George Mason University.