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

Fix issue 520 #538

Merged
merged 2 commits into from
Dec 19, 2024
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
4 changes: 2 additions & 2 deletions src/Microsoft.Build.Sql/sdk/Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildSdksPath)\Microsoft.NET.Sdk\targets\Microsoft.NET.SupportedTargetFrameworks.props" Condition="Exists('$(MSBuildSdksPath)\Microsoft.NET.Sdk\targets\Microsoft.NET.SupportedTargetFrameworks.props')" />

<PropertyGroup>
<NetCoreBuild Condition="'$(NetCoreBuild)' == '' And '$(MSBuildRuntimeType)' == 'Core'">true</NetCoreBuild>
<NetCoreBuild Condition="'$(NetCoreBuild)' == '' And '$(MSBuildRuntimeType)' == 'Full'">false</NetCoreBuild>
<NETCoreTargetsPath Condition="$(NETCoreTargetsPath) == ''">$(MSBuildThisFileDirectory)..\tools\net8.0</NETCoreTargetsPath>
<TargetFramework Condition="'$(TargetFramework)' == '' AND '$(NetCoreBuild)' == 'true'">netstandard2.1</TargetFramework>
<!-- Allow packages of all target frameworks to be referenced by the sqlproj -->
<PackageTargetFallback Condition="'$(PackageTargetFallback)' == ''">@(SupportedTargetFramework->'%(Alias)')</PackageTargetFallback>
<AssetTargetFallback Condition="'$(AssetTargetFallback)' == ''">@(SupportedTargetFramework->'%(Alias)')</AssetTargetFallback>
<PublishDirName Condition="'$(PublishDirName)' == ''">publish</PublishDirName>
<ImplicitlyExpandNETStandardFacades Condition="'$(ImplicitlyExpandNETStandardFacades)' == ''">false</ImplicitlyExpandNETStandardFacades>
</PropertyGroup>

<!-- building in Visual Studio requires some sort of TargetFrameworkVersion. So we condition to NetCoreBuild as false to avoid failures -->
Expand Down
11 changes: 8 additions & 3 deletions src/Microsoft.Build.Sql/sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<IsTool Condition="'$(IsTool)' == ''">true</IsTool>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.dacpac</AllowedOutputExtensionsInPackageBuildOutputFolder>

<!-- Suppress NU5128 since SQL NuGet Packages have no target framework dependencies -->
<NoWarn>$(NoWarn),NU5128</NoWarn>
<!-- Suppress NuGet warnings since SQL NuGet Packages have no target framework dependencies -->
<NoWarn>$(NoWarn),NU1701,NU5128</NoWarn>
</PropertyGroup>

<!-- Publish target properties -->
Expand All @@ -51,6 +51,8 @@
-->
<Target Name="CoreCompile" />

<Import Condition="'$(NetCoreBuild)' == 'true'" Project="$(MSBuildSdksPath)/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.BeforeCommon.targets" />

<Import Condition="'$(NetCoreBuild)' == 'true'" Project="$(MSBuildThisFileDirectory)../tools/net8.0/Microsoft.Data.Tools.Schema.SqlTasks.targets"/>
<Import Condition="'$(NetCoreBuild)' != 'true' AND '$(SQLDBExtensionsRefPath)' != ''" Project="$(SQLDBExtensionsRefPath)\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
<Import Condition="'$(NetCoreBuild)' != 'true' AND '$(SQLDBExtensionsRefPath)' == ''" Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets" />
Expand All @@ -62,6 +64,9 @@
<Import Condition="'$(NetCoreBuild)' != 'true' AND Exists('$(MSBuildExtensionsPath)\Sdks\Microsoft.SqlProject.Sdk\ssdtprojectsystem.targets')"
Project="$(MSBuildExtensionsPath)\Sdks\Microsoft.SqlProject.Sdk\ssdtprojectsystem.targets" />

<!-- Override target to skip invalid .NET Framework check -->
<Target Name="_CheckForUnsupportedTargetFrameworkAndFeatureCombination" />

<UsingTask Condition="'$(NetCoreBuild)' == 'true'" TaskName="AllowEmptyTelemetry" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />

<!-- Add dacpac file BuiltProjectOutputGroupOutput, so that it will get included in the NuGet package by the Pack target -->
Expand All @@ -86,7 +91,7 @@

<ItemGroup>
<!-- This is necessary for building on non-Windows platforms. -->
<PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" IsImplicitlyDefined="true" />
<PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" IsImplicitlyDefined="true" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions test/Microsoft.Build.Sql.Tests/BuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,25 @@ public void VerifyBuildWithIncludeFiles()
}
}
}

[Test]
// https://github.com/microsoft/DacFx/issues/520
public void BuildWithExternalReference()
{
// Build a ReferenceProj with a table
string tempFolder = TestUtils.CreateTempDirectory();
TestUtils.CopyDirectoryRecursive(Path.Combine(this.CommonTestDataDirectory, "ReferenceProj"), tempFolder);

// Add project reference and build with a synonym created from the external table, and a view on the synonym
this.AddProjectReference(Path.Combine(tempFolder, "ReferenceProj.sqlproj"), databaseSqlcmdVariable: "ReferenceDb");
int exitCode = this.RunDotnetCommandOnProject("build", out string stdOutput, out string stdError);

// Verify success
Assert.AreEqual(0, exitCode, "Build failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();

Directory.Delete(tempFolder, true);
}
}
}
11 changes: 8 additions & 3 deletions test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,16 @@ protected void AddNoneScripts(params string[] files)
}

/// <summary>
/// Add references to another project(s). <paramref name="projects"/> paths are relative.
/// Add reference to another project. <paramref name="project"/> path is relative.
/// </summary>
protected void AddProjectReference(params string[] projects)
protected void AddProjectReference(string project, string databaseSqlcmdVariable = "")
{
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "ProjectReference", projects);
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "ProjectReference", new string[] { project }, (ProjectItemElement item) => {
if (!string.IsNullOrEmpty(databaseSqlcmdVariable))
{
item.AddMetadata("DatabaseSqlCmdVariable", databaseSqlcmdVariable);
}
});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SYNONYM Synonym1 FOR [$(ReferenceDb)].[dbo].[Table1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE VIEW [dbo].[View1]
AS SELECT * FROM Synonym1
Loading