From dc5fbc1a9187f17317112b65a0fdde887f703212 Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Mon, 6 Jun 2016 16:23:23 -0700 Subject: [PATCH] Adding basic cycle detection in the dependency walker --- .../.noautobuild | 0 .../Program.cs | 12 ++++++++++++ .../project.json | 16 ++++++++++++++++ .../dotnet-build/ProjectGraphCollector.cs | 2 +- .../Commands/RestoreCommand.cs | 6 ++++++ test/dotnet-build.Tests/BuildOutputTests.cs | 12 ++++++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/.noautobuild create mode 100644 TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/Program.cs create mode 100644 TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/project.json diff --git a/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/.noautobuild b/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/.noautobuild new file mode 100644 index 000000000..e69de29bb diff --git a/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/Program.cs b/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/Program.cs new file mode 100644 index 000000000..24b0cd5f1 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/project.json b/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/project.json new file mode 100644 index 000000000..cd24d0e88 --- /dev/null +++ b/TestAssets/NonRestoredTestProjects/TestProjectWithSelfReferencingDependency/project.json @@ -0,0 +1,16 @@ +{ + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true + }, + "dependencies": { + "TestProjectWithSelfReferencingDependency": { + "target": "project" + } + }, + "frameworks": { + "netcoreapp1.0": { + "imports": "dnxcore50" + } + } +} diff --git a/src/dotnet/commands/dotnet-build/ProjectGraphCollector.cs b/src/dotnet/commands/dotnet-build/ProjectGraphCollector.cs index 7fe7d88d2..cc2a261fe 100644 --- a/src/dotnet/commands/dotnet-build/ProjectGraphCollector.cs +++ b/src/dotnet/commands/dotnet-build/ProjectGraphCollector.cs @@ -43,7 +43,7 @@ namespace Microsoft.DotNet.Tools.Build foreach (var dependency in project.Dependencies) { LibraryDescription libraryDescription; - if (lookup.TryGetValue(dependency.Name, out libraryDescription)) + if ((lookup.TryGetValue(dependency.Name, out libraryDescription)) && (!libraryDescription.Identity.Name.Equals(project.Identity.Name))) { if (libraryDescription.Resolved && libraryDescription.Identity.Type.Equals(LibraryType.Project)) { diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RestoreCommand.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RestoreCommand.cs index a533c1a91..6c88478e2 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RestoreCommand.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RestoreCommand.cs @@ -19,5 +19,11 @@ namespace Microsoft.DotNet.Tools.Test.Utilities return base.Execute(args); } + + public override CommandResult ExecuteWithCapturedOutput(string args = "") + { + args = $"restore {args}"; + return base.ExecuteWithCapturedOutput(args); + } } } diff --git a/test/dotnet-build.Tests/BuildOutputTests.cs b/test/dotnet-build.Tests/BuildOutputTests.cs index 852f563fe..ebf5bc522 100644 --- a/test/dotnet-build.Tests/BuildOutputTests.cs +++ b/test/dotnet-build.Tests/BuildOutputTests.cs @@ -351,6 +351,18 @@ namespace Microsoft.DotNet.Tools.Builder.Tests buildResult.StdErr.Should().Contain("The project has not been restored or restore failed - run `dotnet restore`"); } + [Fact] + private void App_WithSelfReferencingDependency_FailsBuild() + { + var testAssetsManager = GetTestGroupTestAssetsManager("NonRestoredTestProjects"); + var testInstance = testAssetsManager.CreateTestInstance("TestProjectWithSelfReferencingDependency") + .WithLockFiles(); + + var restoreResult = new RestoreCommand() { WorkingDirectory = testInstance.TestRoot }.ExecuteWithCapturedOutput(); + restoreResult.Should().Fail(); + restoreResult.StdOut.Should().Contain("error: Cycle detected"); + } + private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir) { // copy all the files to temp dir