Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Embedded Resources during compilation task #10928

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/Build.UnitTests/ProjectCache/ProjectCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.CommandLine;
using Microsoft.Build.Construction;
using Microsoft.Build.Execution;
using Microsoft.Build.Experimental.ProjectCache;
Expand All @@ -18,10 +19,12 @@
using Microsoft.Build.Shared;
using Microsoft.Build.Unittest;
using Microsoft.Build.UnitTests;
using Microsoft.Build.UnitTests.Shared;
using Microsoft.Build.Utilities;
using Shouldly;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using Task = System.Threading.Tasks.Task;

namespace Microsoft.Build.Engine.UnitTests.ProjectCache
Expand Down Expand Up @@ -1656,5 +1659,73 @@ private void SetEnvironmentForErrorLocations(ErrorLocations errorLocations, Erro
}
}
}

[Fact]
/// <summary>
/// https://github.com/dotnet/msbuild/issues/5334
/// </summary>
public void EmbeddedResourcesFileCompileCache()
{
var directory = _env.CreateFolder();
string content = ObjectModelHelpers.CleanupFileContents(
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<OutputPath>bin/</OutputPath>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="*.txt"/>
</ItemGroup>
</Project>
""");
var projectPath = directory.CreateFile("app.csproj", content).Path;
directory.CreateFile("Program.cs",
"""
using System;
using System.IO;
using System.Reflection;

class Program
{
static void Main()
{
var assembly = Assembly.GetExecutingAssembly();
var resourceNames = assembly.GetManifestResourceNames();

foreach (var resourceName in resourceNames)
{
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
var content = reader.ReadToEnd();
Console.WriteLine($"Content of {resourceName}:");
Console.WriteLine(content);
}
}
}
}
""");

// Create EmbeddedResources file
var file1 = directory.CreateFile("File1.txt", "A=1");
var file2 = directory.CreateFile("File2.txt", "B=1");

// Build and run the project
string output = RunnerUtilities.ExecBootstrapedMSBuild($"{projectPath} -restore", out bool success);
success.ShouldBeTrue(output);
output = RunnerUtilities.RunProcessAndGetOutput(Path.Combine(directory.Path, "bin/net8.0/app"), "", out success, false, _output);
output.ShouldContain("A=1");
output.ShouldContain("B=1");

// Delete a file and build
FileUtilities.DeleteNoThrow(file1.Path);
output = RunnerUtilities.ExecBootstrapedMSBuild($"{projectPath}", out success);
success.ShouldBeTrue(output);
output = RunnerUtilities.RunProcessAndGetOutput(Path.Combine(directory.Path, "bin/net8.0/app"), "", out success, false, _output);
output.ShouldNotContain("A=1");
output.ShouldContain("B=1");
}
}
}
3 changes: 2 additions & 1 deletion src/Tasks/Microsoft.Common.CurrentVersion.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3810,7 +3810,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
contribute to incremental build inconsistencies.
============================================================
-->
<Target Name="_GenerateCompileDependencyCache" Condition="'$(DesignTimeBuild)' != 'true' and '$(BuildingProject)' == 'true'" DependsOnTargets="ResolveAssemblyReferences">
<Target Name="_GenerateCompileDependencyCache" Condition="'$(DesignTimeBuild)' != 'true' and '$(BuildingProject)' == 'true'" DependsOnTargets="ResolveAssemblyReferences;_GenerateCompileInputs">
<ItemGroup>
<CustomAdditionalCompileInputs Include="$(IntermediateOutputPath)$(MSBuildProjectFile).CoreCompileInputs.cache" />
<CoreCompileCache Include="@(Compile)" />
Expand All @@ -3819,6 +3819,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<CoreCompileCache Include="$(LangVersion)" />
<CoreCompileCache Include="$(Deterministic)" />
<CoreCompileCache Include="$(PathMap)" />
<CoreCompileCache Include="@(_CoreCompileResourceInputs)"/>
</ItemGroup>

<Hash
Expand Down
Loading