Updated at 10 p.m. ET

Former Republican National Committee Chairman Ed Gillespie just barely survived the Virginia GOP primary for governor Tuesday night in a shockingly tight contest, and will now face off against Democratic Lt. Gov. Ralph Northam in the key November contest.

Northam easily defeated former U.S. Rep. Tom Perriello in the more closely watched Democratic contest by nearly 12 points, which pitted the progressive Perriello against the establishment Northam.

But it was in the overlooked Republican race where Gillespie, the party's 2014 nominee for Senate, only narrowly beat far-right challenger Corey Stewart, a staunch ally of President Trump.

Stewart, the chairman of the Prince William County Board of Supervisors and a former co-chairman of Trump's campaign in Virginia, ran a controversial campaign, promising to fight the removal of Virginia's Confederate memorials and to crack down on illegal immigration, and pledging his loyalty to Trump and his policies.

Gillespie, in comparison, has hedged on whether he supports the president or would want his help in November — a clear general election strategy for a purple state that's increasingly trended blue in statewide contests.

With nearly all votes counted, Gillespie was leading Stewart by about 1.2 percentage points, 43.7 percent to 42.5 percent, which is just outside the margin for a recount in the state.

The Democratic contest was expected to be the closer one of the night. Northam had long been considered the Democrats' successor to Gov. Terry McAuliffe, who's prohibited by law from running for a second term. But then Perriello, a one-term congressman who lost in the 2010 GOP wave, threw his hat into the race in January, running on a very anti-Trump platform.

Perriello had the backing of Vermont Sen. Bernie Sanders and Massachusetts Sen. Elizabeth Warren, and had hoped an energized progressive force of new voters could catapult him into an upset. Meanwhile, Northam had all of the state's elected officials in his corner. The dichotomy evoked comparisons to the 2016 Democratic presidential primary contest between the upstart Sanders and the establishment Hillary Clinton.

Northam, a pediatric neurologist by training, also heavily outspent Perriello on TV, including a memorable spot where he minced no words in proclaiming that Trump was a "narcissistic maniac."

While Gillespie's win is a relief for national Republicans, the far closer than expected result should sound alarm bells for the fall. Stewart's strength reveals a still deeply fractured primary electorate — and shows Trump and support for his agenda still remain popular among the GOP faithful. Now the critical question is whether Stewart supporters will turn out for Gillespie in November.

Meanwhile, Democrats were on pace to have record turnout in their primary for a non-presidential year contest, according to the University of Virginia's Center for Politics' Geoffrey Skelley. That's the type of enthusiasm Democrats have been banking on to build a midterm election backlash to Trump, and the 2017 Virginia race will be a critical test of whether they can do that.

Copyright 2017 NPR. To see more, visit http://www.npr.org/.