Search code examples
unit-testingnpmazure-devopsazure-pipelines

Self-Hosted DevOps Agent throws "Cannot find module 'xml2js'" error during VSTest task


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.


Solution

  • 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'
        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