Republican Ed Gillespie and Democratic Lieutenant Governor Ralph Northam are running in this year's closely watched race for Virginia governor. (Claritza Jimenez/The Washington Post)

President Trump has been a presence in Virginia’s contest for governor since before the candidates were even set, but he has now roiled the race by jumping in directly and attacking the Democratic nominee through his preferred method: a nighttime tweet.

“Ralph Northam, who is running for Governor of Virginia, is fighting for the violent MS-13 killer gangs & sanctuary cities. Vote Ed Gillespie!” Trump tweeted to his 40 million followers at 9:58 p.m. Thursday.

Coming four weeks before Election Day, it marked the president’s first foray into the race, which is the nation’s marquee statewide election this year and is being closely watched by the national parties as a hint of what may come in the 2018 midterms.

While Trump has endorsed other candidates in special elections this year, none of those endorsements has taken place in a true swing state that could test the president’s political strength or his effect on the GOP agenda on the state level.

Virginia has posed a particularly tricky challenge for Gillespie, who has struggled to energize Trump supporters without alienating moderates and independents. He needs those voters to overcome a Democratic base that is united behind their candidate, Lt. Gov. Ralph Northam, and motivated to vote on Nov. 7.

Trump is deeply unpopular in Virginia, the only Southern state that Hillary Clinton won last year.

The president’s tweet immediately set off speculation about whether Gillespie, a longtime establishment Republican who ran the Republican National Committee and worked for President George W. Bush, had turned to the man who has upended the traditional GOP for help.

Gillespie said he neither sought the president’s endorsement nor asked him to tweet on his behalf.

“I’m not going into conversations between the White House and the campaign or the RNC and the campaign,” Gillespie said during an interview with The Washington Post on Friday. “We talk to folks all the time. But we did not request a tweet, and I didn’t know a tweet was coming. It came as a surprise to me.”

But then, potentially concerned he might be seen as repudiating Trump, he added: “Just to be clear, again, I didn’t ask him not to endorse. I assumed that given that I’m the Republican nominee and he’s the Republican president, that he was endorsing me.”

The president’s endorsement came on the same day that a new Washington Post-Schar School poll showed Northam with a 13-point lead over Gillespie among likely voters. That margin is far greater than in other public polls, which have shown the two men neck and neck or Northam with a slight lead.

The latest stories and details on the 2017 Virginia general election and race for governor.

Northam wasted no time in using the Trump tweet in a fundraising pitch, and a string of other high-profile Democrats — including Gov. Terry McAuliffe, U.S. Sen. Brian Schatz of Hawaii and former Senate candidate Jason Kander of Missouri — formed a chorus on Twitter, urging their donor networks to send money to the Virginia Democrat. By 3 p.m. Friday, the Northam campaign had raised $69,364 from more than 1,700 donors and was on pace to take in $110,000 in 24 hours.

Gillespie said he learned about Trump’s jump into the race by reading it on his phone Thursday night. “I saw it come up on my iPhone, and my thought was, ‘My ads must be running in D.C.,’ ” Gillespie said.

His reaction to it was somewhat muted. He made no mention of the president’s endorsement on his social media feeds and did not address it until asked about it Friday morning on a media conference call.

Asked why he did not publicize Trump’s endorsement as he has others, Gillespie said, “The president has 40 million Twitter followers,” pausing to chuckle, “so I think obviously that got out there without the need for a retweet.”

The response struck some as odd. “I almost fell off my chair in disbelief,” said John Fredericks, a conservative radio host who helped lead Trump’s Virginia campaign and listened to Gillespie’s conference call. “Why wouldn’t you retweet an endorsement from the president of the United States, who has an immensely loyal following?” Gillespie “missed a huge opportunity to amplify the president’s message,” Fredericks added. “. . . He never even said ‘thank you.’ ”

Gillespie’s low-key reaction may be a “wise strategy,” said Mark Rozell, the dean of the Schar School of Policy and Government at George Mason University.

“It somewhat enables him to have it both ways,” Rozell said. “Trump has signaled to the pro-Trump voters, ‘He’s our guy.’ Gillespie has signaled to other voters, ‘I wasn’t looking for this.’ Not responding with great enthusiasm to Trump’s endorsement I don’t think will hurt him with Trump supporters. They listen to Trump — and Trump only, for many of them.”

By elevating the issue of gang violence, Trump’s endorsement could hurt Northam by distracting from health care, education and other areas where Northam polls well, Rozell said.

“Putting the focus at least temporarily on immigration, these gangs, I think somewhat helps Gillespie to make the case that these are the issues that should drive voters’ concern,” Rozell said. “But you know, I keep coming back to the polling data, and I don’t see a lot of evidence there that immigration is an issue.”

The recent Post-Schar poll found roughly 6 in 10 Virginia voters said illegal immigration was not a problem in their communities, while about 10 percent listed it as their top priority.

Republicans say their internal polling has Gillespie virtually tied with Northam and running with strong support from Republicans, making a Trump endorsement less of a necessity.

According to one senior administration official, Trump is “plugged in” when it comes to Virginia and has been tracking the race from the White House for months. But the official was not aware that Trump had planned to endorse Gillespie on Thursday.

“He assumes that his endorsement helps,” the official said. “There’s a lot of questions about what expected turnout will be.”

A second administration official said Trump did not tell anyone he planned to endorse Gillespie. The source said that Trump does not want a loss in Virginia and that the White House will watch the race in the next two weeks to determine how much more the president might engage.

The officials spoke on the condition of anonymity because they were not authorized to comment on the race.

The Post-Schar poll released Thursday found that although Northam had a comfortable lead, the race is fluid, with 1 out of every 4 voters saying they could change their minds before Election Day.

Through much of the campaign, Gillespie has tried to keep the focus on local issues and off Trump, a mission that seemed particularly futile when the president made controversial remarks in the aftermath of a deadly rally by white supremacists in Charlottesville. Trump’s response to Charlottesville and the resulting national debate over race and history became intertwined with the governor’s race.

Gillespie nearly lost the Republican nomination in June to Corey A. Stewart, a bombastic campaigner who attacked illegal immigration and fashioned himself in the Trump mold.

White House political advisers urged the Gillespie campaign to hire some of Trump’s strategists and go hard after Trump voters in the general election.

By late September, Gillespie had enlisted at least one Trump adviser and rolled out four TV ads and mailings that sought to tie Northam to the MS-13 gangs — a claim that has been labeled “misleading” by the nonpartisan FactCheck.org and racist by immigration advocates.

The ads are a variation on a single theme and feature images of tattooed gang members or an ominous, dark, hooded figure holding up a baseball bat as the MS-13 motto “Kill, Rape, Control” flashes on the screen. The narrator warns of violent crime linked to the street gang in Virginia and accuses Northam of putting Virginia families at risk.

It refers to a tiebreaking vote cast in February by Northam in the state Senate against a GOP bill that would have banned “sanctuary cities” in Virginia. It was a preemptive measure: Virginia does not have any such cities, a fact that Gillespie has acknowledged.

But the ads say that Northam’s actions allowed “dangerous illegal immigrants back on the street.”

Former state attorney general Jerry Kilgore, now finance chairman of the Republican Party of Virginia, surmised that Trump saw Gillespie’s MS-13 ad on TV and simply took to Twitter. “Just the substance of the tweet, he must have been watching one of the ads,” Kilgore said.

Trump’s tweet was in sync with an issue, MS-13 gang violence, that excites his supporters.

“He’s placing targeted messages to this base,” said former House speaker Newt Gingrich, an outside adviser to the president. “I think he’s going to do what he thinks makes the most sense.”

“They want to bring out the most voters in this race,” he added.

Northam, who is a pediatrician and former Army doctor, condemned the Republican assault on WAMU radio’s “Kojo Nnamdi Show.”

“For anybody to look at my history and look at my record, one of public service as a veteran, as a doctor who has taken care of sick children, to say I condone or influence gangs in the commonwealth of Virginia is totally inaccurate, it’s despicable, and all it does is continue to promote the fearmongering,” Northam said. “That’s something we’ve seen out of Washington, and now I see Mr. Gillespie is going to be his chief lobbyist here in Virginia.”

Gillespie would not say whether Trump will come to Virginia on his behalf. “I’m happy to get the support of everybody I can, all hands on deck — the president, the vice president, members of the Cabinet,” he told The Post.

Kilgore predicted the endorsement would not shake up the race one way or the other. “Democrats have been running against Trump since November,” he said. “. . . It’s not like they’re going to run even harder against Trump.”

Fredericks, the conservative radio host, noted that the president, too, could benefit from a Gillespie win. “If Gillespie wins this race, it changes the Democrats’ entire national narrative,” he said.

Paul Schwartzman, Abby Phillip and Jenna Johnson contributed to this report.