Search code examples
c#.netazureazure-pipelinescsc

C# compiler (csc.exe) on Azure Pipelines


I've created a tool that needs you to have csc.exe compiler installed and added to PATH.

I want to test that the program works correctly using Azure Pipelines, but I don't know how to install and add it to PATH variable.

How can I do that (and remove the error 'csc' is not recognized as an internal or external command, operable program or batch file.)?

Here is my pipeline run:

https://dev.azure.com/LumitoLuma/GitHub/_build/results?buildId=30&view=logs&j=12f1170f-54f2-53f3-20dd-22fc7dff55f9

And here it's code:

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      echo "##vso[task.setvariable variable=JAVA_HOME]$(JAVA_HOME_11_X64)"
      echo "##vso[task.setvariable variable=PATH]$(JAVA_HOME_11_X64)\bin;$(PATH)"
  displayName: Setting up Java

- task: NuGetCommand@2
  inputs:
    command: 'custom'
    arguments: 'install Microsoft.Net.Compilers'

- script: install.bat
  displayName: Installing JCC

Thanks a lot for your help!


Solution

  • How can I do that (and remove the error 'csc' is not recognized as an internal or external command, operable program or batch file.)?

    The windows-hosted agent has corresponding VS installed. Since you're using windows-latest element, Azure DevOps will use the windows2019 with VS2019 installed for your pipeline. You can check the different paths for Csc.exe below:

    For VS2017 Enterprise:

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Roslyn\csc.exe
    

    For VS2019 Enterprise:

    C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Roslyn\csc.exe
    

    For .net 4.0 framework:

    C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
    

    Workaround:

    Use multi-line script to Set the path of csc.exe first, then call the install.bat.

    - script: |
        SET PATH=%PATH%;"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Roslyn\"
        install.bat
      displayName: 'Run a multi-line script'
    

    You can use script above when you're using windows-latest agent. And you can modify the path whenever you want to use another agent. Also, distinguish the difference between one-line script and multi-line script:

    - script: echo Hello, world!
      displayName: 'Run a one-line script'
    
    - script: |
        echo Add other tasks to build, test, and deploy your project.
        echo See https://aka.ms/yaml
      displayName: 'Run a multi-line script'