Skip to content

Fix locating files in GetCommonSourceFiles#125123

Open
mrvoorhe wants to merge 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-locating-source-files
Open

Fix locating files in GetCommonSourceFiles#125123
mrvoorhe wants to merge 1 commit intodotnet:mainfrom
Unity-Technologies:linker-fix-locating-source-files

Conversation

@mrvoorhe
Copy link
Contributor

@mrvoorhe mrvoorhe commented Mar 3, 2026

_testCase.RootCasesDirectory is meant to be configurable. For example, Unity uses the same test framework to run tests that live in Unity.Linker.Tests.Cases. When we run tests, _testCase.RootCasesDirectory is going to be the path to our Unity.Linker.Tests.Cases directory. When that happens GetCommonSourceFiles cannot locate the files relative to _testCase.RootCasesDirectory.

Since GetCommonSourceFiles is trying to explicitly locate files relative to the ILLink test case directory structure the code should be using a known directory to start from rather than a path that is configurable.

I added the new GetMonoLinkerTestsDirectory directory to TestCaseCompilationMetadataProvider.cs instead of adding it to PathUtilities because it makes life a little easier. Unity has to provide it's own copy of PathUtilities in order to add the bin directory in the path returned by GetTestAssemblyRoot. Which means having this path location in PathUtilities leads to more wire up work for us. That said, if you'd prefer GetMonoLinkerTestsDirectory be in PathUtilities I can move it there and deal with the extra wiring on our end.

While I was here I thought I'd make GetCommonSourceFiles virtual just in case Unity needs to override it in the future.

`_testCase.RootCasesDirectory` is meant to be configurable.  For example, Unity uses the same test framework to run tests that live in `Unity.Linker.Tests.Cases`.  When we run tests, `_testCase.RootCasesDirectory` is going to be the path to our `Unity.Linker.Tests.Cases` directory.  When that happens `GetCommonSourceFiles` cannot locate the files relative to `_testCase.RootCasesDirectory`.

Since `GetCommonSourceFiles` is trying to explicitly locate files relative to the ILLink test case directory structure the code should be using a known directory to start from rather than a path that is configurable.

I added the new `GetMonoLinkerTestsDirectory` directory to `TestCaseCompilationMetadataProvider.cs` instead of adding it to `PathUtilities` because it makes life a little easier.  Unity has to provide it's own copy of `PathUtilities` in order to add the `bin` directory in the path returned by `GetTestAssemblyRoot`.  Which means having this path location in `PathUtilities` leads to more wire up work for us.  That said, if you'd prefer `GetMonoLinkerTestsDirectory` be in `PathUtilities` I can move it there and deal with the extra wiring on our end.

While I was here I thought I'd make `GetCommonSourceFiles` virtual just in case Unity needs to override it in the future.
Copilot AI review requested due to automatic review settings March 3, 2026 14:50
@mrvoorhe mrvoorhe requested a review from sbomer as a code owner March 3, 2026 14:50
@mrvoorhe
Copy link
Contributor Author

mrvoorhe commented Mar 3, 2026

@sbomer please take a look

@github-actions github-actions bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Mar 3, 2026
@dotnet-policy-service dotnet-policy-service bot added linkable-framework Issues associated with delivering a linker friendly framework community-contribution Indicates that the PR has been added by a community member labels Mar 3, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the ILLink test framework’s GetCommonSourceFiles path resolution so it doesn’t depend on the configurable _testCase.RootCasesDirectory, which can point at non-ILLink test case trees (e.g., Unity’s).

Changes:

  • Switch GetCommonSourceFiles to compute paths from a “known” test framework directory rather than _testCase.RootCasesDirectory.
  • Add GetMonoLinkerTestsDirectory helper (via CallerFilePath) to find the Mono.Linker.Tests directory.
  • Make GetCommonSourceFiles virtual to allow downstream override.


// Currently this isn't needed anywhere else. Putting it in PathUtilities would require Unity to do some extra wiring up work in order
// to get this working. It seems simpler to just leave this here.
static NPath GetMonoLinkerTestsDirectory([CallerFilePath] string thisFile = null) => thisFile.ToNPath().Parent.Parent;
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetMonoLinkerTestsDirectory() relies on [CallerFilePath], which bakes the build-time source path into the assembly. This bypasses the existing runtime-configured test root (PathUtilities.GetTestsSourceRootDirectory via AppContext) and can break when the binaries are executed from a different checkout/path than they were built in. Consider resolving these paths from PathUtilities.GetTestsSourceRootDirectory() (the known root used elsewhere in the test framework) instead of CallerFilePath, and drop GetMonoLinkerTestsDirectory entirely (or implement it in terms of PathUtilities).

Suggested change
static NPath GetMonoLinkerTestsDirectory([CallerFilePath] string thisFile = null) => thisFile.ToNPath().Parent.Parent;
static NPath GetMonoLinkerTestsDirectory() => PathUtilities.GetTestsSourceRootDirectory();

Copilot uses AI. Check for mistakes.
Comment on lines +155 to 163
public virtual IEnumerable<NPath> GetCommonSourceFiles()
{
yield return _testCase.RootCasesDirectory.Parent
yield return GetMonoLinkerTestsDirectory().Parent
.Combine("Mono.Linker.Tests.Cases.Expectations")
.Combine("Support")
.Combine("DynamicallyAccessedMembersAttribute.cs");

var sharedDir = _testCase.RootCasesDirectory.Parent.Parent
var sharedDir = GetMonoLinkerTestsDirectory().Parent.Parent
.Combine("src")
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetMonoLinkerTestsDirectory() is called twice in this method; consider storing it in a local variable (e.g., testsDir) to avoid repeated path conversion and to make the subsequent path construction easier to read and maintain.

Copilot uses AI. Check for mistakes.
@mrvoorhe
Copy link
Contributor Author

mrvoorhe commented Mar 3, 2026

I'm looking into the failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers community-contribution Indicates that the PR has been added by a community member linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants