SNP ministers were warned they had “a very real problem” defending their legal battle with Alex Salmond two months before it finally collapsed.

Newly published legal advice given to the Scottish Government on October 31, 2018 said there was “an extremely concerning” issue that could gut their entire case.

By December 6, the Government’s outside lawyers were advising ministers to give up.

They said: “Ultimately, our own view is that the ‘least worst’ option would be to concede the Petition. We understand how unpalatable that advice will be, and we do not tender it lightly.

“But we cannot let the respondents sail forth into January’s hearing without the now very real risks of doing so being crystal clear to all concerned.”

However the Government pressed on regardless, with the Lord Advocate James Wolffe being “very clear that there was no question or need to drop the case”. 

By December 19, the external counsel said they had been caused “extreme professional embarrassment” by repeated late disclosure of documents and mistakes in affidavits.

They said maintaining a defence was potentially “unstateable”.

The Government did not concede until the first week of January 2019.

The advice was only released after months of refusals by the Government following the threat of a no confidence vote in deputy first minister John Swinney.

Despite Holyrood twice voting for its release last November, Mr Swinney only asked the Government’s law officers for permission to release it on Monday.

The material appeared to lend weight to Mr Salmond’s claim that the Government prolonged its doomed defence longer than was legally justifiable and so abused public funds.

A cross-party committee of inquiry is looking at how the Government bungled a probe into sexual misconduct allegations made against Mr Salmond in 2018.

The former First Minister had the exercise set aside in a a judicial review in January 2019, showing it had been “tainted by apparent bias” and getting £512,000 in legal costs.

The Government’s key mistake was to appoint an investigating officer, Judith McKinnon, who had been in substantial prior contact with Mr Salmond's two accusers instead of someone unconnected, as the Government's own complaints procedure stipulated.

 In a letter to the inquiry accompanying the legal advice, Mr Swinney said legal advice involved “a range of opinions” from both in-house and external lawyers.

He said: “The concluded legal opinion of the Scottish Government is therefore a result of a  range of views being considered. It is not the case that the opinion of external counsel alone constitutes the legal advice to Ministers.”

He also highlighted that the advice from external counsel had changed over time as more facts about the Government’s activities came to light.

In late September 2018, Mr Salmond’s challenge was regarded as “weak” and capable of being resisted successfully.

However in October 2018, external counsel raised concerns about the investigating officer, Judith McKinnon, having had prior contact with Mr Salmond’s accusers.

This appeared to conflict with the Government’s own procedure on investigating harassment complaints, which said the investigating officer “will have had no prior involvement with any aspect of the matter being raised”.

On 30 October 2018, the Government’s external junior counsel, solicitor advocate Christine O’Neill flagged up the possible problem of Ms McKinnon’s prior involvement with the complainers.

The next day, senior external counsel, Roddy Dunlop QC, said in an email he had discussed the matter with the Lord Advocate “as I am very concerned indeed”.

He sent an urgent note on the issue to a Government official at almost 11pm, apologising for the later hour, but adding “I’m afraid I see no other option”.

In it, he said Ms McKinnon’s prior involvement had not been regarded as a problem when she was appointed, as she was not involved in the events giving rise to the complaints.

However he went on: “I regret that I do not read the procedure that way. As presently advised, I consider that this represents a very real problem indeed.

“If I am correct.. Then the procedure was not followed: rather, an express embargo was ignored in a way which may well vitiate the entire proceedings.

“It would be extremely wrong for me to suggest that this revelation is another other than an extremely concerning one.

“I should stress, of course, that I am not suggesting bad faith on the part of anyone, least of all Ms McKinnon.

“But the fact remains that the Procedure indicates - to my mind, at least, that she was not eligible to be appointed as IO [Investigating officer].

“If I am right in that regard then arguably that infects all that followed thereon.”

In a further opinion on December 6, Mr Dunlop and Ms O’Neill, said the Government ‘s least worst option was to concede the case based on Ms McKinnon’s prior involvement.

The note listed a series of calls and meetings between Ms McKinnon and the women, which by now Mr Salmond’s lawyers were saying breached the Government’s own procedure.

It said: “The IO did not come to the matter ‘fresh’”: rather, her appointment followed discussions with the complainers in which options were discussed; one complainer was told that she would be advised if others came forward; and indeed the terms of the not-yet-in-force procedure were put to the complainers for their views. 

“This is, we regret to advise, a substantial problem.”

They said there was a real possibility of the court seeing this as apparent bias.

They concluded: “There are only two options.

“One is to  concede the Petition and, if so advised, return to square one. 

“We  have  no  doubt whatsoever  that  this  is  not  an  attractive  option:  it  would  require  the  conceding  of  (doubtless substantial) expenses, and would be trumpeted everywhere by the petitioner.

“The other is simply to press on  regardless. That is, in many ways, even less attractive: the expenses will be far higher, and the trumpeting far louder, if the case proceeds to a written judgment. 

“Moreover, and potentially of more concern, is the real prospect of damaging criticism from Lord Pentland.

“He is not a judge known to pull his punches, and we are both concerned at the possibility of criticism, both from the bench in the course of the hearing and in any written judgment, which would not reflect well on the respondents.  

“Ultimately, our own view is that the ‘least worst’ option would be to concede the Petition. We understand how unpalatable that advice will be, and we do not tender it lightly. 

“But we cannot let the respondents sail forth into January’s hearing without the now very real risks of doing so being crystal clear to all concerned.” 

However both the Government’s own law officers, the Lord Advocate and Solicitor General, were “very clear that there was no question or need to drop the case”. 

But by December 19, Mr Dunlop and Ms O’Neill said their “dismay” with the case.

The late and “regrettable” disclosure of government documents had caused them “extreme professional embarrassment”, having given the court what proved false reassurances.

They said late documents from Ms McKinnon in particular were “inexplicable”, and predicted a “torrid time” for officials having to explain it to the court. 

The lawyers said the omissions meant they had given the court an account of Ms McKinnon’s contacts with one woman that was “plainly and demonstrably untrue”.

The fact that a sworn affidavit missed out key facts was “frankly alarming”.

They warned: “We are now in a position where we think that maintaining a defence of the appointment of the IO may be unstateable. 

“Given the timescales we are reluctant to take a final view on this, but there is a real risk that we so conclude. 

“Second, we are each in a position which is, so far as dealings with the other side and the court are concerned, close to untenable.”

However within Government, there was still talk about continuing the case.

By December 31, however, the Lord Advocate was advising on the terms of conceding - they should be as narrow as possible - and this was finally done in early January.

The inquiry tried for months to see the Government’s legal advice to see if this is the case, but Mr Swinney refused, despite two votes in parliament for disclosure.

He cited the Government’s legal privilege and the long-standing convention that advice to ministers is secret.

However the Scottish ministerial code allows disclosure of legal advice in the public interest if the law officers consent to a request from ministers to release it.

The SNP Government has already released legal advice to three judge-led inquiries.

After the Tories tabled a no confidence vote at Holyrood, and the other opposition parties agreed to back it, putting his job in the line, Mr Swinney changed his position.

Mr Swinney said: “The documents demonstrate that the Scottish Government did not ignore legal advice in continuing to defend the judicial review, contrary to the terms of the Scottish Ministerial Code or the Civil Service Code.

“The documents demonstrate that there was no ‘malicious’ intent against Mr Salmond. 

“The Scottish Government was within its rights to defend a judicial review raised against it by Mr Salmond and to continue to defend it whilst it still had a stateable case. 

“There were good public policy reasons for continuing to defend the case and to seek a determination from the Court.

“After initial consideration in September and assurance that appropriate arrangements were in place to protect the identity of the complainers, the Scottish Government did not seek at any stage to sist the judicial review.

“Once it became clear that the Scottish Government no longer had a stateable case after the Commission hearings in late December 2018, the Government quickly reviewed the position and conceded the case.  

“The view that the Government no longer had a stateable case was agreed by both internal and external advisers.

“Contrary to claims that have been made, the Court did not ‘judge’ that the Scottish Government Procedure was “unlawful” or, indeed, “illegal”.  

“The Government conceded the case on a single ground of apparent bias in the application of the Procedure in this specific case.”

Labour MSP Jackie Baillie said: “The legal advice plainly states that while the Scottish Government had grounds to view aspects of its case as ‘defensible’, serious questions remained around procedural unfairness from the very beginning.

“The advice states plainly that there was a ‘real risk’ that the Court would be persuaded by the challenge on ‘procedural unfairness’.

“Despite this warning, the Scottish Government persisted at the cost of over half a million pounds of taxpayers’ money and to the detriment of the women involved.

“The redactions littered throughout the published documents most certainly do not meet the Parliament’s call for the advice to be published in full and there are documents missing.

“The comments from counsel make it explicitly clear that the conduct of the investigation and the actions of those involved greatly jeopardised the proceedings. That Counsel were expressing grave concerns in October and that the Lord Advocate wished to continue with proceedings as late as December 2018 simply beggars belief.

“The Scottish Government’s unlawful handling of harassment complaints appears to be indefensible, and I look forward to having the opportunity to question the First Minister on the failings of her government.”