Skip to content

Commit 32d31c8

Browse files
authored
Add check for duplicate SQL build items (#574)
* Add check for duplicate build items * Update link * Update link
1 parent baaf3b7 commit 32d31c8

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/Microsoft.Build.Sql/sdk/Sdk.props

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@
4848

4949
<ItemGroup>
5050
<!-- Build default globbing pattern includes all sql files in all subfolders, excluding bin and obj -->
51-
<Build Include="**/*.sql" />
52-
<Build Remove="bin/**/*.sql" />
53-
<Build Remove="obj/**/*.sql" />
51+
<Build Condition="'$(EnableDefaultSqlItems)' == 'true'" Include="**/*.sql" Exclude="$(DefaultSqlItemExcludes)" />
5452
</ItemGroup>
5553
</Project>

src/Microsoft.Build.Sql/sdk/Sdk.targets

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@
6868
<Target Name="_CheckForUnsupportedTargetFrameworkAndFeatureCombination" />
6969

7070
<UsingTask Condition="'$(NetCoreBuild)' == 'true'" TaskName="AllowEmptyTelemetry" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
71+
<UsingTask Condition="'$(NetCoreBuild)' == 'true'" TaskName="CheckForDuplicateItems" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
72+
73+
<Target Name="CheckForDuplicateSqlItems" BeforeTargets="_SetupSqlBuildInputs">
74+
75+
<PropertyGroup>
76+
<DefaultItemsMoreInformationLink>https://aka.ms/upgrade-sqlproject</DefaultItemsMoreInformationLink>
77+
</PropertyGroup>
78+
79+
<CheckForDuplicateItems
80+
Items="@(Build)"
81+
ItemName="Build"
82+
DefaultItemsEnabled="$(EnableDefaultSqlItems)"
83+
DefaultItemsOfThisTypeEnabled="$(EnableDefaultSqlItems)"
84+
PropertyNameToDisableDefaultItems="EnableDefaultSqlItems"
85+
MoreInformationLink="$(DefaultItemsMoreInformationLink)"
86+
ContinueOnError="$(ContinueOnError)">
87+
<Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedSqlItems" />
88+
</CheckForDuplicateItems>
89+
90+
<!-- Currently do nothing with DeduplicatedItems. There are some design time logic in Microsoft.NET.Sdk.DefaultItems.Shared.targets that we can implement in the future. -->
91+
92+
</Target>
7193

7294
<!-- Add dacpac file BuiltProjectOutputGroupOutput, so that it will get included in the NuGet package by the Pack target -->
7395
<Target Name="AddDacpacToBuiltProjectOutputGroupOutput" BeforeTargets="BuiltProjectOutputGroup">
@@ -138,4 +160,11 @@
138160
<ArtifactReference Include="@(_ResolvedDacpacPackageReference->'%(HintPath)')" Condition="Exists(%(_ResolvedDacpacPackageReference.HintPath))" />
139161
</ItemGroup>
140162
</Target>
163+
164+
<!-- Default properties to be evaluated last -->
165+
<PropertyGroup>
166+
<EnableDefaultSqlItems Condition=" '$(EnableDefaultSqlItems)' == '' ">true</EnableDefaultSqlItems>
167+
<DefaultSqlItemExcludes>$(DefaultSqlItemExcludes);$(BaseOutputPath)/**;$(BaseIntermediateOutputPath)/**</DefaultSqlItemExcludes>
168+
</PropertyGroup>
169+
141170
</Project>

test/Microsoft.Build.Sql.Tests/BuildTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,5 +405,38 @@ public void BuildWithExternalReference()
405405

406406
Directory.Delete(tempFolder, true);
407407
}
408+
409+
[Test]
410+
// https://github.com/microsoft/DacFx/issues/561
411+
public void FailBuildOnDuplicatedItems()
412+
{
413+
// Add a file that should be included in the default globbing pattern already
414+
this.AddBuildFiles("Table1.sql");
415+
416+
int exitCode = this.RunDotnetCommandOnProject("build", out _, out _);
417+
418+
// Verify failure
419+
Assert.AreEqual(1, exitCode, "Build is expected to fail.");
420+
}
421+
422+
[Test]
423+
public void BuildWithDefaultItemsDisabled()
424+
{
425+
// Add a file that should be included in the default globbing pattern already
426+
this.AddBuildFiles("Table1.sql");
427+
428+
// Disable default items
429+
ProjectUtils.AddProperties(this.GetProjectFilePath(), new Dictionary<string, string>()
430+
{
431+
{ "EnableDefaultSqlItems", "False" }
432+
});
433+
434+
int exitCode = this.RunDotnetCommandOnProject("build", out _, out string stdError);
435+
436+
// Verify success
437+
Assert.AreEqual(0, exitCode, "Build failed with error " + stdError);
438+
Assert.AreEqual(string.Empty, stdError);
439+
this.VerifyDacPackage();
440+
}
408441
}
409442
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE [dbo].[Table1]
2+
(
3+
c1 int NOT NULL PRIMARY KEY,
4+
c2 int NULL
5+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE [dbo].[Table1]
2+
(
3+
c1 int NOT NULL PRIMARY KEY,
4+
c2 int NULL
5+
)

0 commit comments

Comments
 (0)