Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NuGetCommandV2 fails gracefully when used on ubuntu 24 without mono #20801

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,7 @@
"loc.messages.Error_NugetFailedWithCodeAndErr": "The nuget command failed with exit code(%s) and error(%s)",
"loc.messages.Warning_IncludeNuGetOrgEnabled": "IncludeNugetOrg is currently enabled for this task. To resolve this warning, edit your build task and set 'includeNuGetOrg' to 'false' or deselect 'Use packages from NuGet.org'.",
"loc.messages.Error_IncludeNuGetOrgEnabled": "Packages failed to restore. Edit your build task and set 'includeNuGetOrg' to 'false' or deselect 'Use packages from NuGet.org'.",
"loc.messages.Warning_UnsupportedServiceConnectionAuth": "The service connection does not use a supported authentication method. Please use a service connection with personal access token based auth."
"loc.messages.Warning_UnsupportedServiceConnectionAuth": "The service connection does not use a supported authentication method. Please use a service connection with personal access token based auth.",
"loc.messages.LIB_WhichNotFound_Linux": "Unable to locate executable file: '%s'. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.",
"loc.messages.Error_IncompatibleUbuntuVersion": "The task has failed because you are using Ubuntu 22.04 without mono. See [link] for more information."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to assume that the [link] will be replaced by actual link before merging this PR ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
77 changes: 77 additions & 0 deletions Tasks/NuGetCommandV2/UbuntuDetectionHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as os from 'os';
import * as fs from 'fs';
import * as tl from "azure-pipelines-task-lib/task";

export function detectUnsupportedUbuntuVersion(): boolean {
const platform = os.platform();
if (platform === 'linux') {
const lbsContents = _readLinuxVersionFile();
const distribution = _parseLinuxDistribution(lbsContents);
if (distribution === 'Ubuntu') {
const version = parseFloat(_parseUbuntuVersion(lbsContents));
if (version >= 22.04) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mistaken I apologize. I'm not sure where I got 22.04 from. 24.04 should be the proper cutoff (though as long as we check for mono as well, it shouldn't be a big deal)

try {
tl.which('mono', true);
}
catch (error) {
if (error.message === tl.loc("LIB_WhichNotFound_Linux", 'mono')) {
return true;
amp-powell marked this conversation as resolved.
Show resolved Hide resolved
}
throw error;
}
}
}
}
return false
}

function _readLinuxVersionFile(): string {
const lsbReleaseFile = '/etc/lsb-release';
const osReleaseFile = '/etc/os-release';
let contents = '';

if (fs.existsSync(lsbReleaseFile)) {
contents = fs.readFileSync(lsbReleaseFile).toString();
} else if (fs.existsSync(osReleaseFile)) {
contents = fs.readFileSync(osReleaseFile).toString();
}

return contents;
}

function _parseLinuxDistribution(lbsContents): string {
const lines = lbsContents.split('\n');
for (const line of lines) {
const parts = line.split('=');
if (
parts.length === 2 &&
(parts[0].trim() === 'DISTRIB_ID' ||
parts[0].trim() === 'DISTRIB_DESCRIPTION')
) {
return parts[1]
.trim()
.split(/\s+/)[0]
.replace(/^"/, '')
.replace(/"$/, '');
}
}
return '';
}

function _parseUbuntuVersion(lbsContents): string {
const lines = lbsContents.split('\n');
for (const line of lines) {
const parts = line.split('=');
if (
parts.length === 2 &&
(parts[0].trim() === 'VERSION_ID' ||
parts[0].trim() === 'DISTRIB_RELEASE')
) {
return parts[1]
.trim()
.replace(/^"/, '')
.replace(/"$/, '');
}
}
return '';
}
5 changes: 5 additions & 0 deletions Tasks/NuGetCommandV2/nugetcommandmain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as nugetCustom from "./nugetcustom";
import * as nugetPack from "./nugetpack";
import * as nugetPublish from "./nugetpublisher";
import * as nugetRestore from "./nugetrestore";
import * as ubuntuDetectionHelper from "./UbuntuDetectionHelper";

const NUGET_EXE_CUSTOM_LOCATION: string = "NuGetExeCustomLocation";

Expand All @@ -21,6 +22,10 @@ async function main(): Promise<void> {
let nugetVersion: string;
let msBuildVersion: string;
try {
if (ubuntuDetectionHelper.detectUnsupportedUbuntuVersion()) {
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_IncompatibleUbuntuVersion"));
return;
}
msBuildVersion = await nuGetGetter.getMSBuildVersionString();
nuGetPath = tl.getVariable(nuGetGetter.NUGET_EXE_TOOL_PATH_ENV_VAR)
|| tl.getVariable(NUGET_EXE_CUSTOM_LOCATION);
Expand Down
Loading
Loading