From 4772215c3eff8d0356face65922602d6839307ed Mon Sep 17 00:00:00 2001 From: Zi Chen Date: Fri, 6 Dec 2024 18:20:01 -0800 Subject: [PATCH] Fix issue 520 --- src/Microsoft.Build.Sql/sdk/Sdk.props | 4 ++-- src/Microsoft.Build.Sql/sdk/Sdk.targets | 11 +++++++--- test/Microsoft.Build.Sql.Tests/BuildTests.cs | 20 +++++++++++++++++++ .../DotnetTestBase.cs | 11 +++++++--- .../BuildWithExternalReference/Synonym1.sql | 1 + .../BuildWithExternalReference/View1.sql | 2 ++ 6 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/Synonym1.sql create mode 100644 test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/View1.sql diff --git a/src/Microsoft.Build.Sql/sdk/Sdk.props b/src/Microsoft.Build.Sql/sdk/Sdk.props index 4b94526..f972831 100644 --- a/src/Microsoft.Build.Sql/sdk/Sdk.props +++ b/src/Microsoft.Build.Sql/sdk/Sdk.props @@ -6,7 +6,6 @@ - true @@ -14,8 +13,9 @@ $(MSBuildThisFileDirectory)..\tools\netstandard2.1 netstandard2.1 - @(SupportedTargetFramework->'%(Alias)') + @(SupportedTargetFramework->'%(Alias)') publish + false diff --git a/src/Microsoft.Build.Sql/sdk/Sdk.targets b/src/Microsoft.Build.Sql/sdk/Sdk.targets index abb5a46..31420fc 100644 --- a/src/Microsoft.Build.Sql/sdk/Sdk.targets +++ b/src/Microsoft.Build.Sql/sdk/Sdk.targets @@ -34,8 +34,8 @@ true $(AllowedOutputExtensionsInPackageBuildOutputFolder);.dacpac - - $(NoWarn),NU5128 + + $(NoWarn),NU1701,NU5128 @@ -51,6 +51,8 @@ --> + + @@ -62,6 +64,9 @@ + + + @@ -86,7 +91,7 @@ - + diff --git a/test/Microsoft.Build.Sql.Tests/BuildTests.cs b/test/Microsoft.Build.Sql.Tests/BuildTests.cs index ff4c154..07437c6 100644 --- a/test/Microsoft.Build.Sql.Tests/BuildTests.cs +++ b/test/Microsoft.Build.Sql.Tests/BuildTests.cs @@ -389,5 +389,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); + } } } \ No newline at end of file diff --git a/test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs b/test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs index 1b57491..8d19a47 100644 --- a/test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs +++ b/test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs @@ -286,11 +286,16 @@ protected void AddNoneScripts(params string[] files) } /// - /// Add references to another project(s). paths are relative. + /// Add reference to another project. path is relative. /// - 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); + } + }); } /// diff --git a/test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/Synonym1.sql b/test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/Synonym1.sql new file mode 100644 index 0000000..f8909e1 --- /dev/null +++ b/test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/Synonym1.sql @@ -0,0 +1 @@ +CREATE SYNONYM Synonym1 FOR [$(ReferenceDb)].[dbo].[Table1] \ No newline at end of file diff --git a/test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/View1.sql b/test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/View1.sql new file mode 100644 index 0000000..8484ab4 --- /dev/null +++ b/test/Microsoft.Build.Sql.Tests/TestData/BuildWithExternalReference/View1.sql @@ -0,0 +1,2 @@ +CREATE VIEW [dbo].[View1] + AS SELECT * FROM Synonym1 \ No newline at end of file