Republican gubernatorial candidate Ed Gillespie is walking a fine line on immigration. (Steve Helber/Associated Press)

With about three weeks to go before Virginia Republicans pick their nominee for governor, front-runner Ed Gillespie is engaged in an awkward two-step as he tries to appeal to Donald Trump voters as well as more mainstream “big tent” Republicans.

In TV ads, Gillespie sells himself as a twofer — a seasoned government hand and an advocate for change, a Washington insider itching to go to bat for the average Joe.

In Facebook ads targeted to the kinds of voters who swept Trump into the White House, the Republican strikes a harder tone, with images of federal immigration agents taking someone away in handcuffs, a massive border wall and piles of driver’s licenses presumably issued to undocumented immigrants.

But even there, Gillespie hedges — his lobbying firm earned more than $1 million helping Tyson Foods with matters including “amnesty proposals” and criminal charges related to illegal-immigrant employees. Gillespie takes pains to criticize Democrats inclined to shield undocumented immigrants from deportation, not the immigrants themselves.

“He’s having to play the hokey pokey a little bit here, one foot in, one foot out,” said Benjamin Melusky, a political scientist at Old Dominion University. “If he goes too far to the right to pick up the far-right primary electorate, that’s going to be a little bit of a chain around his neck going into November.”

Gillespie has tried to steer clear of issues roiling Washington and skipped so many candidate forums that his most vocal GOP rival in the June 13 primary has dubbed him “No-Show Ed.” It is a strategy that has kept Gillespie on message and out of controversy, but also has exposed him to criticism from the left and the right.

Polls suggest that Gillespie, whose campaign declined to comment on its strategy, has little to worry about in the primary, with Prince William County Supervisor Corey Stewart and state Sen. Frank Wagner (Virginia Beach) trailing by double digits.

But if Gillespie wins the nomination, his challenge in the race for the Nov. 7 general election will only get trickier.

A recent Washington Post-Schar School poll found that Trump’s widespread unpopularity in Virginia, and voter anger about the Republican plan to replace the Affordable Care Act, threaten to drag down Gillespie behind either of the two Democrats competing for their party’s nomination.

In head-to-head matchups, Gillespie trails both Democrats by almost identical margins: former congressman Tom Perriello by 50 percent to 37 percent, and Lt. Gov. Ralph Northam by 49 percent to 38 percent among registered voters.

Gillespie must figure out how to excite his party’s conservative base without turning off the moderate Republicans and independents essential to winning the swing state.

That has meant doing his best to ignore Trump’s most polarizing actions, such as banning travelers from seven Muslim-majority countries in January and firing FBI Director James B. Comey.

The strategy can backfire. Gillespie was mocked for tweeting about a mundane campaign event — “.@CathyGillespie & I were at the historic @HanoverTavern today” — while the rest of Twitter was on fire about the immigration ban. When he finally released a statement about Comey’s ouster, it came down studiously on neither side.

“While it is any president’s prerogative to hire or fire an FBI Director, there have been many questions and concerns raised about this decision and I look forward to learning more about its timing and rationale as they’re answered,” his statement said.

Asked in a recent TV interview whether he would welcome Trump’s help on the trail, Gillespie looked cornered. “Obviously, help from President Trump, Vice President Pence — I’m happy to have all the help I can get,” he said.

Gillespie’s supporters say the way he is pitching himself — as a pragmatic, business-friendly Republican focused on kitchen-table issues such as a 10 percent income tax cut and government reform — appeals to voters in an increasingly diverse state.

“I know firsthand how the system works, and I know when it’s not working for middle-class families,” Gillespie says in his TV ad, which features a photo of him with George W. Bush as well as pictures of his parents, who owned a small New Jersey grocery store. “As governor, I’ll stand up to special interests, eliminate tax breaks for big businesses, simplify our tax code, and cut taxes for families and small businesses. I’ll be a governor for all Virginians.”

Gillespie’s dance might be especially difficult when it comes to immigration, an issue that keeps rearing its head as Trump’s travel ban and deportation policies reverberate in the commonwealth.

The most recent instance came Wednesday, when outgoing Gov. Terry McAuliffe (D) pardoned Liliana Cruz Mendez of Falls Church who had a 2014 misdemeanor conviction for driving without a license. She came to the United States illegally from El Salvador a decade ago and first drew notice from police with a broken taillight. Two stays of deportation were granted by the Obama administration but have since expired. U.S. Immigration and Customs Enforcement agents detained her May 18 after she arrived for a routine check-in. Despite McAuliffe’s pardon, she still faces deportation.

“Taking Liliana away from her kids & husband won’t make VA safer,” the governor tweeted. “Feds need to focus on public safety threats & real immigration reform.”

One Republican running for governor had a lot to say about McAuliffe’s action. That Republican was not Gillespie. As Stewart and the state GOP blasted McAuliffe’s “fake pardon” and “disregard for the rule of law,” Gillespie initially declined to comment.

Two days later, campaign spokesman David Abrams offered a statement that suggested McAuliffe’s pardon was inappropriate without flatly saying so: “As governor, Ed would uphold the law and not abuse the pardoning process for political purposes.”

Immigration is particularly tricky for Gillespie, given his advocacy in 2013 for a bipartisan immigration bill and work that his lobbying firm did years earlier for Tyson Foods.

Tyson hired Quinn Gillespie & Associates in December 2001, just days before the Justice Department charged the poultry giant with illegally smuggling Mexicans into the country to work at processing plants in Virginia and elsewhere.

Tyson acknowledged some smuggling at the time but maintained that it had been the work of rogue employees and not sanctioned by corporate leaders. The company eventually was acquitted.

Tyson paid Gillespie’s firm more than $1.1 million from 2001 to 2007 to lobby Bush’s White House, the Senate and the House on a range of issues, according to federal lobbying disclosures. Gillespie was listed as a Tyson lobbyist for several of those years. He was registered to handle issues that included “amnesty proposals,” “immigration reform,” “country of origin labeling,” and “labor and workforce issues,” according to those forms.

In an interview with The Washington Post in early May, Gillespie said he did not recall doing any work for Tyson, one of many clients.

“My firm was retained,” he said. “I’m not sure it was even relative to that [criminal] issue, to be honest with you.”

Gary Mickelson, a Tyson spokesman, said Friday that the company hired Gillespie’s firm “for public affairs consulting, not lobbying, when our company was facing immigration charges. . . . Most of the work done by Quinn-Gillespie for our company did not involve Mr. Gillespie.”

Stewart skewered Gillespie for his ties to Tyson in a Facebook video posted last week. As the chairman of the Prince William board, Stewart led a crackdown on undocumented immigrants a decade ago and has taken a hard-line stance on the issue during this race.

“Which candidate for governor helped smuggle illegals from Mexico to take American jobs?” Stewart’s video begins. “Liberal, pro-amnesty lobbyist Ed Gillespie, that’s who. Ed was paid $500,000 to make Tyson’s illegal smuggling problem disappear.”

In 2013, Gillespie pushed for the “Gang of Eight” bill, which called for tighter border security but also a pathway to citizenship for 12 million undocumented immigrants already in the country. Gillespie says he never supported giving citizenship to immigrants, only a pathway to legal status. On television at the time, Gillespie said the measure still needed to be “perfected,” but he pitched it as a good compromise.

“At the end of the day, it’s good policy and it is good politics,” he said.

In the Trump era, the GOP mantra on immigration is much changed. Gillespie, who has kept Trump at a distance, has mostly aimed to soften the edges. He often notes that his father and grandfather were immigrants from Ireland. In the spring, he met with a large group of Indian American professionals in the Richmond suburbs.

At first glance, Gillespie’s Facebook ads on immigration look like a sharp break from that approach, given the pictures of the border wall, the handcuffed immigrant and the driver’s licenses.

But the text paired with the pictures strikes yet another compromise. It avoids any criticism of undocumented immigrants — a contrast with Stewart, who has vowed to “hunt them down.” Gillespie instead takes aim at McAuliffe, Northam and Perriello.

“Enforcing our nation’s longstanding immigration laws isn’t divisive and demonizing,” it reads, “it’s common sense.”