|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.IO; |
| 7 | +using System.Xml; |
| 8 | +using Microsoft.Build.Construction; |
7 | 9 | using Microsoft.Build.Evaluation; |
8 | 10 | using Microsoft.Build.Execution; |
9 | 11 | using Microsoft.Build.Framework; |
@@ -1997,5 +1999,142 @@ public void NonExistentProject(bool? buildNonexistentProjectsByDefault) |
1997 | 1999 | ? "MSB4025" // error MSB4025: The project file could not be loaded. |
1998 | 2000 | : "MSB3202"); // error MSB3202: The project file was not found. |
1999 | 2001 | } |
| 2002 | + |
| 2003 | + /// <summary> |
| 2004 | + /// Verifies that a virtual (in-memory) project can be resolved via <c><ProjectReference></c> |
| 2005 | + /// through the real <c>_SplitProjectReferencesByFileExistence</c> target from |
| 2006 | + /// <c>Microsoft.Common.CurrentVersion.targets</c> when <c>_BuildNonexistentProjectsByDefault</c> is set. |
| 2007 | + /// This is used by file-based apps (<c>dotnet run file.cs</c>) to support |
| 2008 | + /// <c>#:ref</c> directives that create virtual project references. |
| 2009 | + /// </summary> |
| 2010 | + [Theory] |
| 2011 | + [InlineData(false)] |
| 2012 | + [InlineData(true)] |
| 2013 | + public void VirtualProjectReference_SplitByFileExistence(bool buildNonexistentProjectsByDefault) |
| 2014 | + { |
| 2015 | + using TestEnvironment env = TestEnvironment.Create(_testOutput); |
| 2016 | + string projectDir = env.CreateFolder().Path; |
| 2017 | + |
| 2018 | + using var collection = new ProjectCollection(); |
| 2019 | + |
| 2020 | + // Create the referenced virtual project (NOT on disk). |
| 2021 | + string referencedProjectPath = Path.Combine(projectDir, "referenced.csproj"); |
| 2022 | + using var referencedReader = XmlReader.Create(new StringReader(""" |
| 2023 | + <Project> |
| 2024 | + <Target Name="GetTargetPath" Returns="@(TargetPathItem)"> |
| 2025 | + <ItemGroup> |
| 2026 | + <TargetPathItem Include="referenced_output.dll" /> |
| 2027 | + </ItemGroup> |
| 2028 | + </Target> |
| 2029 | + </Project> |
| 2030 | + """)); |
| 2031 | + var referencedRoot = ProjectRootElement.Create(referencedReader, collection); |
| 2032 | + referencedRoot.FullPath = referencedProjectPath; |
| 2033 | + |
| 2034 | + // Create the main project ON DISK with <ProjectReference> and import of real targets. |
| 2035 | + string mainProjectPath = Path.Combine(projectDir, "main.csproj"); |
| 2036 | + File.WriteAllText(mainProjectPath, """ |
| 2037 | + <Project> |
| 2038 | + <Import Project="$(MSBuildBinPath)\Microsoft.Common.CurrentVersion.targets" /> |
| 2039 | + <ItemGroup> |
| 2040 | + <ProjectReference Include="referenced.csproj" /> |
| 2041 | + </ItemGroup> |
| 2042 | + <Target Name="CheckSplit" DependsOnTargets="AssignProjectConfiguration;_SplitProjectReferencesByFileExistence"> |
| 2043 | + <Message Text="Existent: @(_MSBuildProjectReferenceExistent)" Importance="High" /> |
| 2044 | + <Message Text="Nonexistent: @(_MSBuildProjectReferenceNonexistent)" Importance="High" /> |
| 2045 | + </Target> |
| 2046 | + </Project> |
| 2047 | + """); |
| 2048 | + |
| 2049 | + var globalProperties = new Dictionary<string, string>(); |
| 2050 | + if (buildNonexistentProjectsByDefault) |
| 2051 | + { |
| 2052 | + globalProperties[PropertyNames.BuildNonexistentProjectsByDefault] = bool.TrueString; |
| 2053 | + } |
| 2054 | + |
| 2055 | + var project = new Project(mainProjectPath, globalProperties, null, collection); |
| 2056 | + var logger = new MockLogger(_testOutput); |
| 2057 | + bool result = project.Build("CheckSplit", [logger]); |
| 2058 | + _testOutput.WriteLine(logger.FullLog); |
| 2059 | + Assert.True(result); |
| 2060 | + |
| 2061 | + if (buildNonexistentProjectsByDefault) |
| 2062 | + { |
| 2063 | + logger.AssertLogContains("Existent: referenced.csproj"); |
| 2064 | + logger.AssertLogDoesntContain("Nonexistent: referenced.csproj"); |
| 2065 | + } |
| 2066 | + else |
| 2067 | + { |
| 2068 | + logger.AssertLogDoesntContain("Existent: referenced.csproj"); |
| 2069 | + logger.AssertLogContains("Nonexistent: referenced.csproj"); |
| 2070 | + } |
| 2071 | + } |
| 2072 | + |
| 2073 | + /// <summary> |
| 2074 | + /// End-to-end: a virtual project referenced via <c><ProjectReference></c> is actually |
| 2075 | + /// built through the real <c>ResolveProjectReferences</c> target when |
| 2076 | + /// <c>_BuildNonexistentProjectsByDefault</c> is set. Parameterized to verify |
| 2077 | + /// that the main project can also be virtual (not on disk). |
| 2078 | + /// </summary> |
| 2079 | + [Theory] |
| 2080 | + [InlineData(false)] |
| 2081 | + [InlineData(true)] |
| 2082 | + public void VirtualProjectReference_EndToEnd(bool mainProjectVirtual) |
| 2083 | + { |
| 2084 | + using TestEnvironment env = TestEnvironment.Create(_testOutput); |
| 2085 | + string projectDir = env.CreateFolder().Path; |
| 2086 | + |
| 2087 | + using var collection = new ProjectCollection( |
| 2088 | + globalProperties: new Dictionary<string, string> |
| 2089 | + { |
| 2090 | + { PropertyNames.BuildNonexistentProjectsByDefault, bool.TrueString }, |
| 2091 | + }); |
| 2092 | + |
| 2093 | + // Create the referenced virtual project (NOT on disk). |
| 2094 | + string referencedProjectPath = Path.Combine(projectDir, "referenced.csproj"); |
| 2095 | + using var referencedReader = XmlReader.Create(new StringReader(""" |
| 2096 | + <Project> |
| 2097 | + <Target Name="GetTargetPath" Returns="@(TargetPathItem)"> |
| 2098 | + <ItemGroup> |
| 2099 | + <TargetPathItem Include="referenced_output.dll" /> |
| 2100 | + </ItemGroup> |
| 2101 | + <Message Text="message from referenced project" Importance="High" /> |
| 2102 | + </Target> |
| 2103 | + </Project> |
| 2104 | + """)); |
| 2105 | + var referencedRoot = ProjectRootElement.Create(referencedReader, collection); |
| 2106 | + referencedRoot.FullPath = referencedProjectPath; |
| 2107 | + |
| 2108 | + // Create the main project with <ProjectReference> and import of real targets. |
| 2109 | + string mainProjectPath = Path.Combine(projectDir, "main.csproj"); |
| 2110 | + string mainProjectXml = """ |
| 2111 | + <Project> |
| 2112 | + <Import Project="$(MSBuildBinPath)\Microsoft.Common.CurrentVersion.targets" /> |
| 2113 | + <ItemGroup> |
| 2114 | + <ProjectReference Include="referenced.csproj" SkipGetTargetFrameworkProperties="true" /> |
| 2115 | + </ItemGroup> |
| 2116 | + </Project> |
| 2117 | + """; |
| 2118 | + |
| 2119 | + Project project; |
| 2120 | + if (mainProjectVirtual) |
| 2121 | + { |
| 2122 | + using var mainReader = XmlReader.Create(new StringReader(mainProjectXml)); |
| 2123 | + var mainRoot = ProjectRootElement.Create(mainReader, collection); |
| 2124 | + mainRoot.FullPath = mainProjectPath; |
| 2125 | + project = new Project(mainRoot, null, null, collection); |
| 2126 | + } |
| 2127 | + else |
| 2128 | + { |
| 2129 | + File.WriteAllText(mainProjectPath, mainProjectXml); |
| 2130 | + project = new Project(mainProjectPath, null, null, collection); |
| 2131 | + } |
| 2132 | + |
| 2133 | + var logger = new MockLogger(_testOutput); |
| 2134 | + bool result = project.Build("ResolveProjectReferences", [logger]); |
| 2135 | + _testOutput.WriteLine(logger.FullLog); |
| 2136 | + Assert.True(result); |
| 2137 | + logger.AssertLogContains("message from referenced project"); |
| 2138 | + } |
2000 | 2139 | } |
2001 | 2140 | } |
0 commit comments