Skip to content

Commit

Permalink
Merge pull request #674 from Cysharp/hotfix/FixSharedProjectRoot
Browse files Browse the repository at this point in the history
Fix an incorrect root when a project references a shared project.
  • Loading branch information
mayuki committed Aug 16, 2023
2 parents 12ab140 + d1cf978 commit daffe58
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
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"));
}
}

0 comments on commit daffe58

Please sign in to comment.