Skip to content

Commit

Permalink
Swap NuGetCommandV2 tool invocations to async (#20873)
Browse files Browse the repository at this point in the history
* Swap NuGetCommandV2 tool invocations to async

* Set patch v0

---------

Co-authored-by: Coby Allred <coallred@microsoft.com>
  • Loading branch information
cobya and Coby Allred authored Feb 21, 2025
1 parent 28875fe commit 228aa9b
Showing 10 changed files with 159 additions and 134 deletions.
11 changes: 11 additions & 0 deletions Tasks/NuGetCommandV2/Tests/L0.ts
Original file line number Diff line number Diff line change
@@ -406,6 +406,8 @@ describe('NuGetCommand Suite', function () {
tr.run();
assert(tr.stdErrContained, "stderr output is here");
assert(tr.failed, 'should have failed');
assert.equal(tr.errorIssues.length, 1, "should have 1 error");
assert.equal(tr.errorIssues[0], "loc_mock_Error_NugetFailedWithCodeAndErr 1 stderr output is here", "should have error from nuget");
done();
});

@@ -416,6 +418,9 @@ describe('NuGetCommand Suite', function () {
tr.run();
assert(tr.stdErrContained, "stderr output is here");
assert(tr.failed, 'should have failed');
assert.equal(tr.errorIssues.length, 2, "should have 1 error from nuget and one from task");
assert.equal(tr.errorIssues[0], "loc_mock_Error_NugetFailedWithCodeAndErr 1 stderr output is here", "should have error from nuget");
assert.equal(tr.errorIssues[1], "loc_mock_Error_PackageFailure", "should have error from task runner");
done();
});

@@ -426,6 +431,9 @@ describe('NuGetCommand Suite', function () {
tr.run();
assert(tr.stdErrContained, "stderr output is here");
assert(tr.failed, 'should have failed');
assert.equal(tr.errorIssues.length, 2, "should have 1 error from nuget and one from task");
assert.equal(tr.errorIssues[0], "Error: loc_mock_Error_UnexpectedErrorVstsNuGetPush 1 stderr output is here", "should have error from nuget");
assert.equal(tr.errorIssues[1], "loc_mock_PackagesFailedToPublish", "should have error from task runner");
done();
});

@@ -446,6 +454,9 @@ describe('NuGetCommand Suite', function () {
tr.run();
assert(tr.stdErrContained, "stderr output is here");
assert(tr.failed, 'should have failed');
assert.equal(tr.errorIssues.length, 2, "should have 1 error from nuget and one from task");
assert.equal(tr.errorIssues[0], "loc_mock_Error_NugetFailedWithCodeAndErr 1 stderr output is here", "should have error from nuget");
assert.equal(tr.errorIssues[1], "loc_mock_PackagesFailedToInstall", "should have error from task runner");
done();
});

3 changes: 3 additions & 0 deletions Tasks/NuGetCommandV2/Tests/RestoreTests/failRestore.ts
Original file line number Diff line number Diff line change
@@ -33,6 +33,9 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
},
"findMatch": {
"single.sln" : ["c:\\agent\\home\\directory\\single.sln"]
},
"rmRF": {
"c:\\agent\\home\\directory\\tempNuGet_.config": { success: true }
}
};
nmh.setAnswers(a);
27 changes: 17 additions & 10 deletions Tasks/NuGetCommandV2/nugetcustom.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import { logError } from 'azure-pipelines-tasks-packaging-common/util';
import peParser = require("azure-pipelines-tasks-packaging-common/pe-parser/index");
import * as pkgLocationUtils from "azure-pipelines-tasks-packaging-common/locationUtilities";
import * as telemetry from "azure-pipelines-tasks-utility-common/telemetry";
import {IExecSyncResult} from "azure-pipelines-task-lib/toolrunner";
import { IExecOptions } from "azure-pipelines-task-lib/toolrunner";
import { getVersionFallback } from "azure-pipelines-tasks-packaging-common/nuget/ProductVersionHelper";

class NuGetExecutionOptions {
@@ -38,8 +38,7 @@ export async function run(nuGetPath: string): Promise<void> {

const version = await peParser.getFileVersionInfoAsync(nuGetPath);
const parsedVersion = getVersionFallback(version);
if(parsedVersion.a < 3 || (parsedVersion.a <= 3 && parsedVersion.b < 5))
{
if (parsedVersion.a < 3 || (parsedVersion.a <= 3 && parsedVersion.b < 5)) {
tl.setResult(tl.TaskResult.Failed, tl.loc("Info_NuGetSupportedAfter3_5", version.strings.ProductVersion));
return;
}
@@ -85,7 +84,7 @@ export async function run(nuGetPath: string): Promise<void> {
args,
authInfo);

runNuGet(executionOptions);
await runNuGet(executionOptions);
} catch (err) {
tl.error(err);

@@ -97,20 +96,28 @@ export async function run(nuGetPath: string): Promise<void> {
}
}

function runNuGet(executionOptions: NuGetExecutionOptions): IExecSyncResult {
async function runNuGet(executionOptions: NuGetExecutionOptions): Promise<number> {
const nugetTool = ngToolRunner.createNuGetToolRunner(
executionOptions.nuGetPath,
executionOptions.environment,
executionOptions.authInfo);
nugetTool.line(executionOptions.args);
nugetTool.arg("-NonInteractive");

const execResult = nugetTool.execSync();
if (execResult.code !== 0) {
telemetry.logResult("Packaging", "NuGetCommand", execResult.code);
// Listen for stderr output to write timeline results for the build.
let stdErrText = "";
nugetTool.on('stderr', (data: Buffer) => {
stdErrText += data.toString('utf-8');
});

const execResult = await nugetTool.exec({ ignoreReturnCode: true } as IExecOptions);

if (execResult !== 0) {
telemetry.logResult("Packaging", "NuGetCommand", execResult);
throw tl.loc("Error_NugetFailedWithCodeAndErr",
execResult.code,
execResult.stderr ? execResult.stderr.trim() : execResult.stderr);
execResult,
stdErrText.trim());
}

return execResult;
}
61 changes: 29 additions & 32 deletions Tasks/NuGetCommandV2/nugetpack.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import * as path from "path";
import * as ngToolRunner from "azure-pipelines-tasks-packaging-common/nuget/NuGetToolRunner2";
import * as packUtils from "azure-pipelines-tasks-packaging-common/PackUtilities";
import INuGetCommandOptions from "azure-pipelines-tasks-packaging-common/nuget/INuGetCommandOptions2";
import {IExecSyncResult} from "azure-pipelines-task-lib/toolrunner";
import { IExecOptions } from "azure-pipelines-task-lib/toolrunner";
import * as telemetry from "azure-pipelines-tasks-utility-common/telemetry";

class PackOptions implements INuGetCommandOptions {
@@ -42,26 +42,22 @@ export async function run(nuGetPath: string): Promise<void> {
let toolPackage = tl.getBoolInput("toolPackage");
let outputDir = undefined;

try
{
try {
// If outputDir is not provided then the root working directory is set by default.
// By requiring it, it will throw an error if it is not provided and we can set it to undefined.
outputDir = tl.getPathInput("outputDir", true);
}
catch(error)
{
catch (error) {
outputDir = undefined;
}

try{
if(versioningScheme !== "off" && includeRefProj)
{
try {
if (versioningScheme !== "off" && includeRefProj) {
tl.warning(tl.loc("Warning_AutomaticallyVersionReferencedProjects"));
}

let version: string = undefined;
switch(versioningScheme)
{
switch (versioningScheme) {
case "off":
break;
case "byPrereleaseNumber":
@@ -73,34 +69,30 @@ export async function run(nuGetPath: string): Promise<void> {
case "byEnvVar":
tl.debug(`Getting version from env var: ${versionEnvVar}`);
version = tl.getVariable(versionEnvVar);
if(!version)
{
if (!version) {
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_NoValueFoundForEnvVar"));
break;
}
break;
case "byBuildNumber":
tl.debug("Getting version number from build number")

if(tl.getVariable("SYSTEM_HOSTTYPE") === "release")
{
if (tl.getVariable("SYSTEM_HOSTTYPE") === "release") {
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_AutomaticallyVersionReleases"));
return;
}

let buildNumber: string = tl.getVariable("BUILD_BUILDNUMBER");
let buildNumber: string = tl.getVariable("BUILD_BUILDNUMBER");
tl.debug(`Build number: ${buildNumber}`);

let versionRegex = /\d+\.\d+\.\d+(?:\.\d+)?/;
let versionMatches = buildNumber.match(versionRegex);
if (!versionMatches)
{
if (!versionMatches) {
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_NoVersionFoundInBuildNumber"));
return;
}

if (versionMatches.length > 1)
{
if (versionMatches.length > 1) {
tl.warning(tl.loc("Warning_MoreThanOneVersionInBuildNumber"))
}

@@ -110,8 +102,7 @@ export async function run(nuGetPath: string): Promise<void> {

tl.debug(`Version to use: ${version}`);

if(outputDir && !tl.exist(outputDir))
{
if (outputDir && !tl.exist(outputDir)) {
tl.debug(`Creating output directory: ${outputDir}`);
tl.mkdirP(outputDir);
}
@@ -134,12 +125,10 @@ export async function run(nuGetPath: string): Promise<void> {
});

let props: string[] = [];
if(configuration && configuration !== "$(BuildConfiguration)")
{
if (configuration && configuration !== "$(BuildConfiguration)") {
props.push(`Configuration=${configuration}`);
}
if(propertiesInput)
{
if (propertiesInput) {
props = props.concat(propertiesInput.split(";"));
}

@@ -161,15 +150,15 @@ export async function run(nuGetPath: string): Promise<void> {
environmentSettings);

for (const file of filesList) {
pack(file, packOptions);
await pack(file, packOptions);
}
} catch (err) {
tl.error(err);
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_PackageFailure"));
}
}

function pack(file: string, options: PackOptions): IExecSyncResult {
async function pack(file: string, options: PackOptions): Promise<number> {
console.log(tl.loc("Info_AttemptingToPackFile") + file);

let nugetTool = ngToolRunner.createNuGetToolRunner(options.nuGetPath, options.environment, undefined);
@@ -210,12 +199,20 @@ function pack(file: string, options: PackOptions): IExecSyncResult {
nugetTool.arg(options.verbosity);
}

let execResult = nugetTool.execSync();
if (execResult.code !== 0) {
telemetry.logResult('Packaging', 'NuGetCommand', execResult.code);
// Listen for stderr output to write timeline results for the build.
let stdErrText = "";
nugetTool.on('stderr', (data: Buffer) => {
stdErrText += data.toString('utf-8');
});

const execResult = await nugetTool.exec({ ignoreReturnCode: true } as IExecOptions);

if (execResult !== 0) {
telemetry.logResult('Packaging', 'NuGetCommand', execResult);
throw tl.loc("Error_NugetFailedWithCodeAndErr",
execResult.code,
execResult.stderr ? execResult.stderr.trim() : execResult.stderr);
execResult,
stdErrText.trim());
}

return execResult;
}
Loading
Oops, something went wrong.

0 comments on commit 228aa9b

Please sign in to comment.