question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dotnet run don't copy files marked as CopyToOutputDirectory=Always to output on Azure Pipelines hosted agents

See original GitHub issue

I have a test pipeline on hosted agents: ConsoleApp-Win-Core-BothFiles-net5. Pipeline does following steps:

  1. downloads dll file from another pipeline

  2. downloads dylib file from another pipeline

  3. creates console app via dotnet new console -n ConsoleApp

  4. copies dll and dylib files into ConsoleApp folder placing them near csproj file:

    Copy-Item test.dylib -Destination ConsoleApp
    Copy-Item test.dll -Destination ConsoleApp
    
  5. sets csproj file content:

    $content = @"
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net5.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <None Update="test.dll">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
        <None Update="test.dylib">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
    </Project>
    "@
    
    Set-Content -Path ConsoleApp/ConsoleApp.csproj -Value $content
    

    so we have <CopyToOutputDirectory>Always</CopyToOutputDirectory> here.

  6. sets Program.cs content:

    $content = @"
    using System;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using System.Threading;
    
    namespace ConsoleNetFramework
    {
        class Program
        {
            [DllImport("test")]
            public static extern int Foo();
    
            static void Main(string[] args)
            {
                var result = Foo();
                Console.WriteLine($"Result = {result}");
            }
        }
    }
    "@
    
    Set-Content -Path ConsoleApp/Program.cs -Value $content
    
  7. runs app via .NET Core task (dotnet run --project ConsoleApp/ConsoleApp.csproj)

And here we get the error:

Unhandled exception. System.DllNotFoundException: Unable to load DLL 'test' or one of its dependencies: Access is denied. (0x80070005 (E_ACCESSDENIED))
   at ConsoleNetFramework.Program.Foo()
   at ConsoleNetFramework.Program.Main(String[] args) in D:\a\1\s\ConsoleApp\Program.cs:line 15

I found that this is due to files were not copied to output directory.

So I added additional step that prints all dll and dylib files:

----                -------------         ------ ----                                                                  
-a----        5/19/2021   2:42 PM           5120 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp\bin\Debug\net5.0


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----        5/19/2021   2:42 PM           4608 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp\obj\Debug\net5.0\ref


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----        5/19/2021   2:42 PM           5120 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp\obj\Debug\net5.0


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
-a----        5/19/2021   2:42 PM           4608 ConsoleApp.dll                                                        


    Directory: D:\a\1\s\ConsoleApp


Mode                LastWriteTime         Length Name                                                                  
----                -------------         ------ ----                                                                  
d-----        5/19/2021   2:41 PM                test.dll                                                              
d-----        5/19/2021   2:41 PM                test.dylib                                                            


Finishing: Print all dll and dylib files
Row 7. Clickable

Row 2. Clickable

Row 2. Clickable

Row 2. Clickable

Task disabled

Task enabled

Row 2. Clickable

For some reason I see only dll and dylib near csproj that I added on fourth step. So my question is why files were not copied to output directory (D:\a\1\s\ConsoleApp\obj\Debug\net5.0)?

When I do all these steps locally on my computer, all works fine.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
KirillOsenkovcommented, Jun 9, 2021

No, the way it works is None items are automatically created for files, not directories. This is the reason you didn’t see the None items and Update didn’t do anything. When you changed to Include, you forced including the directory, so the Copy task failed.

I’ll take a look at the online viewer failure.

1reaction
KirillOsenkovcommented, Jun 9, 2021

You can add this at the end of your .csproj:

  <ItemGroup>
    <AllFiles Include="**\*" />
  </ItemGroup>

and then in the binlog viewer, search for $additem AllFiles - it will snapshot the directory contents at evaluation time as MSBuild sees it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

visual studio - Copy always to output directory does not work
In the file properties in Visual Studio, set: Build action: None. Copy to output directory: Copy always.
Read more >
Build, test, and deploy .NET Core apps - Azure Pipelines
Set up your build environment with Microsoft-hosted or self-hosted agents. Restore dependencies, build your project, and test with the .NET Core ...
Read more >
Troubleshoot pipeline runs - Azure DevOps
This topic provides guidance on the common reasons that pipelines fail to trigger, get an agent and start, or complete. For instructions on ......
Read more >
DotNetCoreCLI@2 - .NET Core v2 task
Build, test, package, or publish a dotnet application, or run a custom dotnet command.
Read more >
CopyFiles@2 - Copy files v2 task
Copy files v2 # Copy files from a source folder to a target folder using patterns matching file paths (not folder paths).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found