Skip to content

Commit

Permalink
ProjectGraph.GetTargetLists throws on empty target names
Browse files Browse the repository at this point in the history
  • Loading branch information
cdmihai committed Aug 9, 2019
1 parent a3bce7d commit 3e0fe7e
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Build.UnitTests/Graph/ProjectGraph_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,42 @@ public void GetTargetListsReturnsEmptyTargetsForAllNodesWhenDefaultTargetsAreReq
}
}

[Fact]
public void GetTargetListsDoesNotPropagateEmptyTargets()
{
using (var env = TestEnvironment.Create())
{
// Target protocol produces empty target
TransientTestFile entryProject = CreateProjectFile(env: env, projectNumber: 1, projectReferences: new[] { 2 }, projectReferenceTargets: new Dictionary<string, string[]> { { "A", new[] { " ; ; " } }}, defaultTargets: string.Empty);

// Dependency has default targets. Even though it gets called with empty targets, B will not get called,
// because target propagation only equates empty targets to default targets for the root nodes.
CreateProjectFile(env: env, projectNumber: 2, defaultTargets: "B");

var projectGraph = new ProjectGraph(entryProject.Path);
projectGraph.ProjectNodes.Count.ShouldBe(2);

IReadOnlyDictionary<ProjectGraphNode, ImmutableList<string>> targetLists = projectGraph.GetTargetLists(new []{ "A" });
targetLists.Count.ShouldBe(projectGraph.ProjectNodes.Count);
targetLists[GetFirstNodeWithProjectNumber(projectGraph, 1)].ShouldBe(new []{ "A" });
targetLists[GetFirstNodeWithProjectNumber(projectGraph, 2)].ShouldBeEmpty();
}
}

[Fact]
public void GetTargetListsThrowsOnInvalidTargetNames()
{
using (var env = TestEnvironment.Create())
{
TransientTestFile entryProject = CreateProjectFile(env: env, projectNumber: 1);

var projectGraph = new ProjectGraph(entryProject.Path);
projectGraph.ProjectNodes.Count.ShouldBe(1);

Should.Throw<ArgumentException>(() => projectGraph.GetTargetLists(new []{ " " }));
}
}


[Fact]
public void GetTargetListsUsesAllTargetsForNonMultitargetingNodes()
Expand Down
20 changes: 20 additions & 0 deletions src/Build/Graph/ProjectGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,8 @@ private static IReadOnlyCollection<ProjectGraphNode> TopologicalSort(
/// </returns>
public IReadOnlyDictionary<ProjectGraphNode, ImmutableList<string>> GetTargetLists(ICollection<string> entryProjectTargets)
{
ThrowOnEmptyTargetNames(entryProjectTargets);

// Seed the dictionary with empty lists for every node. In this particular case though an empty list means "build nothing" rather than "default targets".
var targetLists = ProjectNodes.ToDictionary(node => node, node => ImmutableList<string>.Empty);

Expand Down Expand Up @@ -549,6 +551,11 @@ public IReadOnlyDictionary<ProjectGraphNode, ImmutableList<string>> GetTargetLis
{
var applicableTargets = targetsToPropagate.GetApplicableTargetsForReference(referenceNode.ProjectInstance);

if (applicableTargets.IsEmpty)
{
continue;
}

var expandedTargets = ExpandDefaultTargets(
applicableTargets,
referenceNode.ProjectInstance.DefaultTargets,
Expand Down Expand Up @@ -599,6 +606,19 @@ public IReadOnlyDictionary<ProjectGraphNode, ImmutableList<string>> GetTargetLis
}

return targetLists;

void ThrowOnEmptyTargetNames(ICollection<string> targetNames)
{
if (targetNames == null || targetNames.Count == 0)
{
return;
}

if (targetNames.Any(targetName => string.IsNullOrWhiteSpace(targetName)))
{
throw new ArgumentException(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("OM_TargetNameNullOrEmpty"));
}
}
}

private static ImmutableList<string> ExpandDefaultTargets(ImmutableList<string> targets, List<string> defaultTargets, ProjectItemInstance graphEdge)
Expand Down
3 changes: 3 additions & 0 deletions src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,9 @@ Utilization: {0} Average Utilization: {1:###.0}</value>
<data name="OM_NameInvalid">
<value>The name "{0}" contains an invalid character "{1}".</value>
</data>
<data name="OM_TargetNameNullOrEmpty">
<value>Target names cannot be null or empty.</value>
</data>
<data name="OM_NoOtherwiseBeforeWhenOrOtherwise">
<value>An &lt;Otherwise&gt; element cannot be located before a &lt;When&gt; or &lt;Otherwise&gt; element.</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph nepodporuje položky ProjectReference s nastavenými metadaty ToolsVersion. V souboru {1} byla nalezena položka ProjectReference {0} s metadaty ToolsVersion.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph bietet keine Unterstützung für ProjectReference-Elemente mit dem ToolsVersion-Metadatensatz. In der Datei "{1}" wurde ProjectReference "{0}" mit "ToolsVersion" gefunden.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="new">MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph no admite elementos de ProjectReference con los metadatos de ToolsVersion establecidos. Se encontró ProjectReference "{0}" con ToolsVersion en el archivo "{1}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph ne prend pas en charge les éléments ProjectReference avec l'ensemble de métadonnées ToolsVersion. ProjectReference "{0}" trouvé avec ToolsVersion dans le fichier "{1}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph non supporta elementi ProjectReference con metadati ToolsVersion impostati. L'elemento ProjectReference "{0}" con ToolsVersion è stato trovato nel file "{1}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph では、ToolsVersion メタデータが設定された ProjectReference 項目はサポートしていません。ToolsVersion が含まれる ProjectReference "{0}" がファイル "{1}" で見つかりました</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph는 ToolsVersion 메타데이터가 설정된 ProjectReference 항목을 지원하지 않습니다. "{1}" 파일에 ToolsVersion이 포함된 ProjectReference "{0}"이(가) 있습니다.</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: Element ProjectGraph nie obsługuje elementów ProjectReference z ustawionymi metadanymi atrybutu ToolsVersion. W pliku „{1}” odnaleziono element ProjectReference „{0}” z atrybutem ToolsVersion</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: O ProjectGraph não tem suporte para os itens ProjectReference com o conjunto de metadados ToolsVersion. O ProjectReference "{0}" foi encontrado com ToolsVersion no arquivo "{1}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph не поддерживает элементы ProjectReference с набором метаданных ToolsVersion. Обнаружен ProjectReference "{0}" с ToolsVersion в файле "{1}"</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph, ToolsVersion meta veri kümesine sahip ProjectReference öğelerini desteklemez. "{1}" dosyasında ToolsVersion içeren ProjectReference "{0}" bulundu</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph 不支持具有 ToolsVersion 元数据集的 ProjectReference 项。在“{1}”文件中发现了带有 ToolsVersion 的 ProjectReference“{0}”</target>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
LOCALIZATION: Do not localize the following words: ProjectInstanceFactoryFunc.
</note>
</trans-unit>
<trans-unit id="OM_TargetNameNullOrEmpty">
<source>Target names cannot be null or empty.</source>
<target state="new">Target names cannot be null or empty.</target>
<note />
</trans-unit>
<trans-unit id="ProjectGraphDoesNotSupportProjectReferenceWithToolset">
<source>MSB4250: ProjectGraph does not support ProjectReference items with the ToolsVersion metadata set. Found ProjectReference "{0}" with ToolsVersion in file "{1}"</source>
<target state="translated">MSB4250: ProjectGraph 不支援設有 ToolsVersion 中繼資料的 ProjectReference 項目。在檔案 "{1}" 中找到具有 ToolsVersion 的 ProjectReference "{0}"</target>
Expand Down

0 comments on commit 3e0fe7e

Please sign in to comment.