0

I'm currently having an issue where our Azure Devops pipelines fail during the VSTest task with the error "Unhandled: Cannot find module 'xml2js'". This error occurs within 1-2 seconds of the task starting.

Error I'm getting from Pipeline

We have a self-hosted devops agent which is where I believe the problem is. But I'm not sure how to pinpoint the issue.

We are using Visual Studio 2022 Build Tools on the server.

The project I'm trying to run the pipeline for is a .Net 6 Console Application, with xUnit unit tests. But this issue occurs across all of our codebase.

YAML of pipeline:

trigger:
- master

pool:
  name: 'Default'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  major: 1
  minor: $[format('{0:yyMM}.{0:dd}', pipeline.startTime)]
  PatchVersion: $[counter(format('{0}.{1}', variables['major'],variables['minor']),0)]
  PRODUCT_VERSION: $[format('{0}.{1}.{2}', variables['major'],variables['minor'],variables['PatchVersion'])]
  isMaster: eq(system.pullRequest.sourceBranch, 'refs/heads/master')

steps:
- task: NuGetToolInstaller@1

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: 'restore'
    projects: |
      **/*.csproj
      **/*.vbproj
    feedsToUse: 'select'
    vstsFeed: '9f16daf9-036f-4252-af27-40195b272a21'
    noCache: true

- task: VersionDotNetCoreAssemblies@3
  inputs:
    Path: '$(Build.SourcesDirectory)'
    VersionNumber: '$(PRODUCT_VERSION)'
    Injectversion: false
    VersionRegex: '\d+(\.\d+)+'
    FilenamePattern: '.csproj'
    AddDefault: true
    OutputVersion: 'OutputedVersion'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true

- task: NuGetCommand@2 --added based on Vrushti's suggestion
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'select'
    vstsFeed: '9f16daf9-036f-4252-af27-40195b272a21'

- task: VSTest@2 //this is where the problem occurs.
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: CopyFiles@2
  inputs:
    Contents: '**/bin/**'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'
    
- task: PublishBuildArtifacts@1

I tried running npm install xml2js -g on the devops server and restarting the server. This did not change anything.

Let me know if I can provide any other relevant details. Sorry if this is a dumb question.

Jdog
  • 48
  • 7

2 Answers2

0

Check npm packages like "xml2js," and add "NuGetCommand" and "Npm" tasks to restore packages before running "VSTest" to solve the issue.

  • What do you mean by "Check npm packages like 'xml2js'"? And does the dotnet restore in the 2nd step not take care of that? I updated my YAML to above and got the same result. – Jdog Jul 26 '23 at 19:02
0

After changing my VSTest@2 step to a DotNetCoreCLI "test" command, I stopped having issues. You can see my updated yaml below.

Based on this SO question, it looks like VSTest isn't the correct way to run tests for xUnit: Difference between vstest.console.exe and dotnet test commands and I should instead use "dotnet test" for a "framework agnostic" testing task.

However the task description in devops explicitly states you can use it to test xUnit tests.

trigger:
- master

pool:
  name: 'Default'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  major: 1
  minor: $[format('{0:yyMM}.{0:dd}', pipeline.startTime)]
  PatchVersion: $[counter(format('{0}.{1}', variables['major'],variables['minor']),0)]
  PRODUCT_VERSION: $[format('{0}.{1}.{2}', variables['major'],variables['minor'],variables['PatchVersion'])]
  isMaster: eq(system.pullRequest.sourceBranch, 'refs/heads/master')

steps:
- task: NuGetToolInstaller@1

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: 'restore'
    projects: |
      **/*.csproj
      **/*.vbproj
    feedsToUse: 'select'
    vstsFeed: '9f16daf9-036f-4252-af27-40195b272a21'
    noCache: true

- task: VersionDotNetCoreAssemblies@3
  inputs:
    Path: '$(Build.SourcesDirectory)'
    VersionNumber: '$(PRODUCT_VERSION)'
    Injectversion: false
    VersionRegex: '\d+(\.\d+)+'
    FilenamePattern: '.csproj'
    AddDefault: true
    OutputVersion: 'OutputedVersion'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    clean: true

- task: DotNetCoreCLI@2 //changed VSTest@2 step to this 
  inputs:
    command: test
    projects: '**/*Test/*.csproj'
    arguments: '--configuration $(buildConfiguration)'

- task: CopyFiles@2
  inputs:
    Contents: '**/bin/**'
    TargetFolder: '$(Build.ArtifactStagingDirectory)'
    
- task: PublishBuildArtifacts@1
Jdog
  • 48
  • 7