0

I have a pwsh task that executes apt install as part of a psake exec. The apt install appears to throw a warning that makes it into the pipeline output stream no matter how I try to trap it:

"WARNING: apt does not have a stable CLI interface. Use with caution in scripts"

##[debug]Processed: ##vso[task.issue type=error;]%0AWARNING: apt does not have a stable CLI interface. Use with caution in scripts.%0A%0A

I have wrapped it in a try-catch, set exit 0 but nothing has worked. What am I doing wrong, haven't thought of yet, or is something else the issue that I'm not aware of yet?

Note: I would like the pwsh task to fail on most standard errors, but obviously not a warning, especially one for code that succeeds.

PowerShell

if ($IsLinux) {

    Exec {
        try {
            Write-Host 'Executing apt install on Linux...'
            sudo apt install nuget
            Exit 0
        } catch {
            Exit 0
        }
    }

}...

Pipeline Task

steps:
- pwsh: |
        if ($null -eq (Get-Module -Name psake -listAvailable)) {
          Install-Module psake -AcceptLicense -Force
        }
        Import-Module psake
        Invoke-psake ./build/tasks.ps1 -taskList Test
        exit ([int](-not $psake.build_success))
  displayName: Execute Unit Tests and Build Package
  failOnStderr: true
  name: Build_and_Test

Relevant Log Output

Executing Init
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 Installing package 'PSDepend'                                                      Downloaded 0.00 MB out of 0.07 MB.                                              [                                                                    ]                                                                                                                                                                       Installing package 'PSDepend'                                                      Unzipping                                                                       [ooooooooooooooooooooooooooooooooooooooooooooooo                     ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Installing package 'BuildHelpers'                                                  Downloaded 0.00 MB out of 0.08 MB.                                              [                                                                    ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       Installing package 'Pester'                                                        Downloaded 0.00 MB out of 0.85 MB.                                              [                                                                    ]                                                                                                                                                                       Installing package 'Pester'                                                        Downloaded 0.09 MB out of 0.85 MB.                                              [oooo                                                                ]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      Executing Clean
Executing Build

    Directory: /home/vsts/work/1/s

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----            3/6/20  3:39 PM                dist
Checking for NuGet install...
NuGet not installed, installing...
Executing apt install on Linux...

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
The following packages were automatically installed and are no longer required:
  bc dns-root-data dnsmasq-base ebtables grub-pc-bin landscape-common
  liblldb-6.0 liblldb-8 liblxc-common liblxc1 libuv1 linux-headers-4.15.0-88
  lxcfs lxd lxd-client python3-attr python3-automat python3-click
  python3-colorama python3-constantly python3-hyperlink python3-incremental
  python3-pam python3-pyasn1 python3-pyasn1-modules python3-service-identity
  python3-twisted python3-twisted-bin python3-zope.interface uidmap xdelta3
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nuget
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 1307 kB of archives.
After this operation, 5826 kB of additional disk space will be used.
Get:1 https://download.mono-project.com/repo/ubuntu stable-bionic/main amd64 nuget all 5.5.0.6319.bin-0xamarin1+ubuntu1804b1 [1307 kB]

Fetched 1307 kB in 0s (12.1 MB/s)
Selecting previously unselected package nuget.
(Reading database ... 
(Reading database ... 5%
(Reading database ... 10%
(Reading database ... 15%
(Reading database ... 20%
(Reading database ... 25%
(Reading database ... 30%
(Reading database ... 35%
(Reading database ... 40%
(Reading database ... 45%
(Reading database ... 50%
(Reading database ... 55%
(Reading database ... 60%
(Reading database ... 65%
(Reading database ... 70%
(Reading database ... 75%
(Reading database ... 80%
(Reading database ... 85%
(Reading database ... 90%
(Reading database ... 95%
(Reading database ... 100%
(Reading database ... 267481 files and directories currently installed.)
Preparing to unpack .../nuget_5.5.0.6319.bin-0xamarin1+ubuntu1804b1_all.deb ...
Unpacking nuget (5.5.0.6319.bin-0xamarin1+ubuntu1804b1) ...
Setting up nuget (5.5.0.6319.bin-0xamarin1+ubuntu1804b1) ...

##[debug]Exit code 0 received from tool '/usr/bin/pwsh'
##[debug]STDIO streams have closed for tool '/usr/bin/pwsh'
##[debug]task result: Failed
##[error]PowerShell wrote one or more lines to the standard error stream.
##[debug]Processed: ##vso[task.issue type=error;]PowerShell wrote one or more lines to the standard error stream.
##[debug]Processed: ##vso[task.complete result=Failed;]PowerShell wrote one or more lines to the standard error stream.
##[error]
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

##[debug]Processed: ##vso[task.issue type=error;]%0AWARNING: apt does not have a stable CLI interface. Use with caution in scripts.%0A%0A
Finishing: Execute Unit Tests and Build Package
Robb Vandaveer
  • 1,481
  • 20
  • 25
  • 1
    Have a look [here](https://askubuntu.com/questions/990823/apt-gives-unstable-cli-interface-warning). `apt` is designed for end-users (human) and its output may be changed between versions. – Theo Mar 07 '20 at 12:44
  • That is what I've learned. I've actually switched from using apt to apt-get but have left the question as I'd still like to understand how to deal with output like this in PowerShell/pipeline. – Robb Vandaveer Mar 07 '20 at 13:11

1 Answers1

0

There is a known issue for agent Windows-2019, the powershell installed on windows-2019 agents write the warning messages to the error stream and return 1 as exit code, which causes the task to fail.

Please check this similar issue reported on this thread for more information. You can join in the thread or open a new issue for this case.

As above thread mentioned, you can try using AzurePipelines/windows-2016 agent or setting ErrorActionPreference = Continue as workaround.

If you are using a on-premise agent. You can also try manually upgrading the powershell version on your on-premise agent machine.

Update:

I tested and the script would run successfully if failOnStderr is set to false. If failOnStderr is false, the task will rely on the exit code to determine failure. Otherwise this task will fail if any errors are written to the error pipeline, or if any data is written to the Standard Error stream.

Since the issue is cause by powershell writing the warning to the error stream, you can redirect the out stream by adding 2>&1 to the script.

sudo apt install nuget 2>&1
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • This is a Linux hosted agent – Robb Vandaveer Mar 09 '20 at 14:06
  • Hi @RobbVandaveer it is an issue of the current version powershell installed on the agent. I tested and the script would run successfully if `failOnStderr` set to `false`. You can also redirect the powershell error stream using `2>&1. Please check above update – Levi Lu-MSFT Mar 10 '20 at 09:31
  • Thank you for the responses. The issue I see with either of those two solutions would be that the pipeline would not fail if an actual error occurred. Is that a correct statement? – Robb Vandaveer Mar 10 '20 at 18:27
  • No, with failOnStderr set to false, the task will rely on the exit code to determine failure. Otherwise this task will fail if any errors are written to the error pipeline, or if any data is written to the Standard Error stream. And `sudo apt install nuget 2>&1` will only redirect the out stream for this command, it doesnot affect other scripts. – Levi Lu-MSFT Mar 11 '20 at 02:08