自定义Azure Pipeline TypeScript任务中如何让\n换行符正常生效
Let's break down your issue and fix that unexpected %0A showing up instead of a newline.
The Problem
When you pass a string with \n to tl.setResult, Azure Pipelines URL-encodes the message before displaying it. That's why your \n gets converted to %0A (the URL-encoded version of a newline character), and you don't see the line break you expect.
Solution 1: Use tl.log for Multi-Line Output (Best for Logs)
If you want the multi-line text to show up in the task's log stream, split your output into separate tl.log calls. This ensures natural line breaks without encoding issues:
var errorsAndWarnings = await getBuildErrors(pipelineRunId, true); // Output each line separately to the log tl.log("Finished Service Operation"); tl.log(errorsAndWarnings); // Set a concise result message tl.setResult(tl.TaskResult.Succeeded, "Service operation completed successfully");
Solution 2: Use HTML Line Breaks for Task Result Summary
If you specifically want the multi-line text to appear in the task's result summary (the area that shows up when you expand the task in the pipeline run), replace \n with HTML <br> tags. The result summary supports basic HTML rendering:
var errorsAndWarnings = await getBuildErrors(pipelineRunId, true); // Use <br> instead of \n for HTML-based line breaks const resultMessage = `Finished Service Operation<br>${errorsAndWarnings}`; tl.setResult(tl.TaskResult.Succeeded, resultMessage);
Why Your Original Code Didn't Work
The tl.setResult method sends the message to Azure Pipelines' backend, which URL-encodes special characters like newlines. When the message is displayed, the encoded %0A isn't converted back to a visible line break—hence the unexpected text instead of a new line.
Choose the solution that fits where you want the multi-line text to appear: use tl.log for the detailed log, or HTML <br> tags for the task result summary.
内容的提问来源于stack exchange,提问作者CodeMonkey

