Skip to content
Merged
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
6 changes: 6 additions & 0 deletions src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,12 @@ private bool NeedsToBuild(out CacheInfo cache)
{
cache = ComputeCacheEntry();

if (Directives.Any(static d => d is CSharpDirective.Project))
{
Reporter.Verbose.WriteLine("Building because there are project directives.");
return true;
}

// Check cache files.

string artifactsDirectory = ArtifactsPath;
Expand Down
58 changes: 58 additions & 0 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3403,6 +3403,64 @@ public void UpToDate_InvalidOptions()
.And.HaveStdErrContaining(string.Format(CliCommandStrings.CannotCombineOptions, RunCommandParser.NoCacheOption.Name, RunCommandParser.NoBuildOption.Name));
}

/// <summary>
/// <see cref="UpToDate"/> optimization currently does not support <c>#:project</c> references and hence is disabled if those are present.
/// See <see href="https://github.com/dotnet/sdk/issues/52057"/>.
/// </summary>
[Fact]
public void UpToDate_ProjectReferences()
{
var testInstance = _testAssetsManager.CreateTestDirectory();

var libDir = Path.Join(testInstance.Path, "Lib");
Directory.CreateDirectory(libDir);

File.WriteAllText(Path.Join(libDir, "Lib.csproj"), $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>{ToolsetInfo.CurrentTargetFramework}</TargetFramework>
</PropertyGroup>
</Project>
""");

var libPath = Path.Join(libDir, "Lib.cs");
var libCode = """
namespace Lib;
public class LibClass
{
public static string GetMessage() => "Hello from Lib v1";
}
""";
File.WriteAllText(libPath, libCode);

var appDir = Path.Join(testInstance.Path, "App");
Directory.CreateDirectory(appDir);

var code = """
#:project ../Lib
Console.WriteLine("v1 " + Lib.LibClass.GetMessage());
""";

var programPath = Path.Join(appDir, "Program.cs");
File.WriteAllText(programPath, code);

// Remove artifacts from possible previous runs of this test.
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programPath);
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true);

var programFileName = "App/Program.cs";

Build(testInstance, BuildLevel.All, expectedOutput: "v1 Hello from Lib v1", programFileName: programFileName);

// We cannot detect changes in referenced projects, so we always rebuild.
Build(testInstance, BuildLevel.All, expectedOutput: "v1 Hello from Lib v1", programFileName: programFileName);

libCode = libCode.Replace("v1", "v2");
File.WriteAllText(libPath, libCode);

Build(testInstance, BuildLevel.All, expectedOutput: "v1 Hello from Lib v2", programFileName: programFileName);
}

/// <summary>
/// Up-to-date checks and optimizations currently don't support other included files.
/// </summary>
Expand Down
Loading