Skip to content

Commit 228aa9b

Browse files
cobyaCoby Allred
and
Coby Allred
authored
Swap NuGetCommandV2 tool invocations to async (#20873)
* Swap NuGetCommandV2 tool invocations to async * Set patch v0 --------- Co-authored-by: Coby Allred <[email protected]>
1 parent 28875fe commit 228aa9b

10 files changed

+159
-134
lines changed

Tasks/NuGetCommandV2/Tests/L0.ts

+11
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ describe('NuGetCommand Suite', function () {
406406
tr.run();
407407
assert(tr.stdErrContained, "stderr output is here");
408408
assert(tr.failed, 'should have failed');
409+
assert.equal(tr.errorIssues.length, 1, "should have 1 error");
410+
assert.equal(tr.errorIssues[0], "loc_mock_Error_NugetFailedWithCodeAndErr 1 stderr output is here", "should have error from nuget");
409411
done();
410412
});
411413

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

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

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

Tasks/NuGetCommandV2/Tests/RestoreTests/failRestore.ts

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
3333
},
3434
"findMatch": {
3535
"single.sln" : ["c:\\agent\\home\\directory\\single.sln"]
36+
},
37+
"rmRF": {
38+
"c:\\agent\\home\\directory\\tempNuGet_.config": { success: true }
3639
}
3740
};
3841
nmh.setAnswers(a);

Tasks/NuGetCommandV2/nugetcustom.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { logError } from 'azure-pipelines-tasks-packaging-common/util';
77
import peParser = require("azure-pipelines-tasks-packaging-common/pe-parser/index");
88
import * as pkgLocationUtils from "azure-pipelines-tasks-packaging-common/locationUtilities";
99
import * as telemetry from "azure-pipelines-tasks-utility-common/telemetry";
10-
import {IExecSyncResult} from "azure-pipelines-task-lib/toolrunner";
10+
import { IExecOptions } from "azure-pipelines-task-lib/toolrunner";
1111
import { getVersionFallback } from "azure-pipelines-tasks-packaging-common/nuget/ProductVersionHelper";
1212

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

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

88-
runNuGet(executionOptions);
87+
await runNuGet(executionOptions);
8988
} catch (err) {
9089
tl.error(err);
9190

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

100-
function runNuGet(executionOptions: NuGetExecutionOptions): IExecSyncResult {
99+
async function runNuGet(executionOptions: NuGetExecutionOptions): Promise<number> {
101100
const nugetTool = ngToolRunner.createNuGetToolRunner(
102101
executionOptions.nuGetPath,
103102
executionOptions.environment,
104103
executionOptions.authInfo);
105104
nugetTool.line(executionOptions.args);
106105
nugetTool.arg("-NonInteractive");
107106

108-
const execResult = nugetTool.execSync();
109-
if (execResult.code !== 0) {
110-
telemetry.logResult("Packaging", "NuGetCommand", execResult.code);
107+
// Listen for stderr output to write timeline results for the build.
108+
let stdErrText = "";
109+
nugetTool.on('stderr', (data: Buffer) => {
110+
stdErrText += data.toString('utf-8');
111+
});
112+
113+
const execResult = await nugetTool.exec({ ignoreReturnCode: true } as IExecOptions);
114+
115+
if (execResult !== 0) {
116+
telemetry.logResult("Packaging", "NuGetCommand", execResult);
111117
throw tl.loc("Error_NugetFailedWithCodeAndErr",
112-
execResult.code,
113-
execResult.stderr ? execResult.stderr.trim() : execResult.stderr);
118+
execResult,
119+
stdErrText.trim());
114120
}
121+
115122
return execResult;
116123
}

Tasks/NuGetCommandV2/nugetpack.ts

+29-32
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as path from "path";
44
import * as ngToolRunner from "azure-pipelines-tasks-packaging-common/nuget/NuGetToolRunner2";
55
import * as packUtils from "azure-pipelines-tasks-packaging-common/PackUtilities";
66
import INuGetCommandOptions from "azure-pipelines-tasks-packaging-common/nuget/INuGetCommandOptions2";
7-
import {IExecSyncResult} from "azure-pipelines-task-lib/toolrunner";
7+
import { IExecOptions } from "azure-pipelines-task-lib/toolrunner";
88
import * as telemetry from "azure-pipelines-tasks-utility-common/telemetry";
99

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

45-
try
46-
{
45+
try {
4746
// If outputDir is not provided then the root working directory is set by default.
4847
// By requiring it, it will throw an error if it is not provided and we can set it to undefined.
4948
outputDir = tl.getPathInput("outputDir", true);
5049
}
51-
catch(error)
52-
{
50+
catch (error) {
5351
outputDir = undefined;
5452
}
5553

56-
try{
57-
if(versioningScheme !== "off" && includeRefProj)
58-
{
54+
try {
55+
if (versioningScheme !== "off" && includeRefProj) {
5956
tl.warning(tl.loc("Warning_AutomaticallyVersionReferencedProjects"));
6057
}
6158

6259
let version: string = undefined;
63-
switch(versioningScheme)
64-
{
60+
switch (versioningScheme) {
6561
case "off":
6662
break;
6763
case "byPrereleaseNumber":
@@ -73,34 +69,30 @@ export async function run(nuGetPath: string): Promise<void> {
7369
case "byEnvVar":
7470
tl.debug(`Getting version from env var: ${versionEnvVar}`);
7571
version = tl.getVariable(versionEnvVar);
76-
if(!version)
77-
{
72+
if (!version) {
7873
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_NoValueFoundForEnvVar"));
7974
break;
8075
}
8176
break;
8277
case "byBuildNumber":
8378
tl.debug("Getting version number from build number")
8479

85-
if(tl.getVariable("SYSTEM_HOSTTYPE") === "release")
86-
{
80+
if (tl.getVariable("SYSTEM_HOSTTYPE") === "release") {
8781
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_AutomaticallyVersionReleases"));
8882
return;
8983
}
9084

91-
let buildNumber: string = tl.getVariable("BUILD_BUILDNUMBER");
85+
let buildNumber: string = tl.getVariable("BUILD_BUILDNUMBER");
9286
tl.debug(`Build number: ${buildNumber}`);
9387

9488
let versionRegex = /\d+\.\d+\.\d+(?:\.\d+)?/;
9589
let versionMatches = buildNumber.match(versionRegex);
96-
if (!versionMatches)
97-
{
90+
if (!versionMatches) {
9891
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_NoVersionFoundInBuildNumber"));
9992
return;
10093
}
10194

102-
if (versionMatches.length > 1)
103-
{
95+
if (versionMatches.length > 1) {
10496
tl.warning(tl.loc("Warning_MoreThanOneVersionInBuildNumber"))
10597
}
10698

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

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

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

136127
let props: string[] = [];
137-
if(configuration && configuration !== "$(BuildConfiguration)")
138-
{
128+
if (configuration && configuration !== "$(BuildConfiguration)") {
139129
props.push(`Configuration=${configuration}`);
140130
}
141-
if(propertiesInput)
142-
{
131+
if (propertiesInput) {
143132
props = props.concat(propertiesInput.split(";"));
144133
}
145134

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

163152
for (const file of filesList) {
164-
pack(file, packOptions);
153+
await pack(file, packOptions);
165154
}
166155
} catch (err) {
167156
tl.error(err);
168157
tl.setResult(tl.TaskResult.Failed, tl.loc("Error_PackageFailure"));
169158
}
170159
}
171160

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

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

213-
let execResult = nugetTool.execSync();
214-
if (execResult.code !== 0) {
215-
telemetry.logResult('Packaging', 'NuGetCommand', execResult.code);
202+
// Listen for stderr output to write timeline results for the build.
203+
let stdErrText = "";
204+
nugetTool.on('stderr', (data: Buffer) => {
205+
stdErrText += data.toString('utf-8');
206+
});
207+
208+
const execResult = await nugetTool.exec({ ignoreReturnCode: true } as IExecOptions);
209+
210+
if (execResult !== 0) {
211+
telemetry.logResult('Packaging', 'NuGetCommand', execResult);
216212
throw tl.loc("Error_NugetFailedWithCodeAndErr",
217-
execResult.code,
218-
execResult.stderr ? execResult.stderr.trim() : execResult.stderr);
213+
execResult,
214+
stdErrText.trim());
219215
}
216+
220217
return execResult;
221218
}

0 commit comments

Comments
 (0)