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 an incorrect root when a project references a shared project. #674

Merged
merged 1 commit into from
Aug 16, 2023
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
2 changes: 1 addition & 1 deletion src/MagicOnion.GeneratorCore/Utils/PseudoCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private static void CollectDocument(string csproj, HashSet<string> source, List<
{
var sharedRoot = Path.GetDirectoryName(Path.Combine(csProjRoot, item.Attribute("Project").Value));
logger.Trace($"[{nameof(PseudoCompilation)}] Import project '{sharedRoot}'");
foreach (var file in IterateCsFileWithoutBinObj(Path.GetDirectoryName(sharedRoot)))
foreach (var file in IterateCsFileWithoutBinObj(sharedRoot))
{
source.Add(file);
}
Expand Down
66 changes: 66 additions & 0 deletions tests/MagicOnion.Generator.Tests/GenerateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,70 @@ public interface IMyService : IService<IMyService>
// Assert
sourceProjectCompilation.GetDiagnostics().Should().Contain(x => x.Severity == DiagnosticSeverity.Error); // the compilation should have some errors.
}

[Fact]
public async Task SharedProject()
{
var options = TemporaryProjectWorkareaOptions.Default with
{
AdditionalCsProjectContent = @"
<Import Project=""..\Shared\SubDir\MyShared.projitems"" Label=""Shared"" />
",
};
using var tempWorkspace = TemporaryProjectWorkarea.Create(options);
var sharedDir = Path.GetFullPath(Path.Combine(tempWorkspace.ProjectDirectory, "..", "Shared", "SubDir"));
Directory.CreateDirectory(sharedDir);
// <tempdir>/Shared/SubDir/MyShared.projeitems
File.WriteAllText(Path.Combine(sharedDir, "MyShared.projeitems"), "<Project></Project>");
// <tempdir>/Shared/SubDir/IMyService.cs
File.WriteAllText(Path.Combine(sharedDir, "IMyService.cs"), """
using System;
using System.Threading.Tasks;
using MessagePack;
using MagicOnion;

namespace TempProject
{
public interface IMyService : IService<IMyService>
{
UnaryResult<int> A();
}
}
""");
// <tempdir>/Shared/IYetAnotherMyService.cs (This file should be ignored.)
File.WriteAllText(Path.Combine(Path.GetDirectoryName(sharedDir), "IYetAnotherMyService.cs"), """
using System;
using System.Threading.Tasks;
using MessagePack;
using MagicOnion;

namespace TempProject
{
public interface IYetAnotherMyService : IService<IYetAnotherMyService>
{
UnaryResult<int> A();
}
}
""");

var compiler = new MagicOnionCompiler(new MagicOnionGeneratorTestOutputLogger(testOutputHelper), CancellationToken.None);
await compiler.GenerateFileAsync(
tempWorkspace.CsProjectPath,
Path.Combine(tempWorkspace.OutputDirectory, "Generated.cs"),
true,
"TempProject.Generated",
"",
"MessagePack.Formatters",
SerializerType.MessagePack
);

// NOTE: GetOutputCompilation only refers under the ProjectDirectory and OutputDirectory.
Directory.Move(sharedDir, Path.Combine(tempWorkspace.ProjectDirectory, "Shared"));

var outputCompilation = tempWorkspace.GetOutputCompilation();
outputCompilation.GetCompilationErrors().Should().BeEmpty();

var symbols = outputCompilation.GetNamedTypeSymbolsFromGenerated();
symbols.Should().NotContain(x => x.Name.Contains("YetAnotherMyService"));
}
}