Skip to content
Closed
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: 2 additions & 0 deletions eng/Subsets.props
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@
</ItemGroup>

<ItemGroup Condition="$(_subset.Contains('+clr.toolstests+'))">
<ProjectToBuild Include="$(CoreClrProjectRoot)tools\AssemblyChecker.Tests\AssemblyChecker.Tests.csproj"
Test="true" Category="clr" Condition="'$(DotNetBuildSourceOnly)' != 'true'"/>
<ProjectToBuild Include="$(CoreClrProjectRoot)tools\aot\ILCompiler.DependencyAnalysisFramework.Tests\ILCompiler.DependencyAnalysisFramework.Tests.csproj"
Test="true" Category="clr" Condition="'$(DotNetBuildSourceOnly)' != 'true'"/>
<ProjectToBuild Include="$(CoreClrProjectRoot)tools\aot\ILCompiler.TypeSystem.Tests\ILCompiler.TypeSystem.Tests.csproj"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>AssemblyChecker.Tests</AssemblyName>
<Configurations>Debug;Release;Checked</Configurations>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../AssemblyChecker/AssemblyChecker.csproj" />
<ProjectReference Include="TestAssets/AssemblyWithExternalAttribute/AssemblyWithExternalAttribute.csproj" />
</ItemGroup>
Comment on lines +9 to +12

<ItemGroup>
<Compile Include="AssemblyInspectorTests.cs" />
</ItemGroup>
</Project>
16 changes: 16 additions & 0 deletions src/coreclr/tools/AssemblyChecker.Tests/AssemblyInspectorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using AssemblyChecker.Tests.AssemblyWithExternalAttribute;
using Xunit;

namespace AssemblyChecker.Tests;

public class AssemblyInspectorTests
{
[Fact]
public void IsDebugResolvesAssemblyLevelAttributeFromAdjacentAssembly()
{
Assert.False(AssemblyInspector.IsDebug(typeof(Marker).Assembly.Location));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<DebugSymbols>false</DebugSymbols>
<DebugType>none</DebugType>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Optimize>true</Optimize>
<OutputType>Library</OutputType>
<SkipTestRun>true</SkipTestRun>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../ExternalAttribute/ExternalAttribute.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="Marker.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using AssemblyChecker.Tests.ExternalAttribute;

[assembly: ReferencedAssembly]

namespace AssemblyChecker.Tests.AssemblyWithExternalAttribute;

public sealed class Marker
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<OutputType>Library</OutputType>
<SkipTestRun>true</SkipTestRun>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="ReferencedAssemblyAttribute.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace AssemblyChecker.Tests.ExternalAttribute;

[AttributeUsage(AttributeTargets.Assembly)]
public sealed class ReferencedAssemblyAttribute : Attribute
{
}
1 change: 1 addition & 0 deletions src/coreclr/tools/AssemblyChecker/AssemblyChecker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="AssemblyChecker.Tests" />
<PackageReference Include="System.Reflection.MetadataLoadContext" Version="$(SystemReflectionMetadataLoadContextPackageVersion)" />
</ItemGroup>
</Project>
22 changes: 15 additions & 7 deletions src/coreclr/tools/AssemblyChecker/AssemblyInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;

namespace AssemblyChecker
{
internal static class AssemblyInspector
{
private static readonly RuntimeAssemblyResolver s_resolver = new();

internal static bool IsDebug(string path)
{
string assemblyPath = Path.GetFullPath(path);
using MetadataLoadContext loadContext = new(s_resolver);
string? assemblyDirectory = Path.GetDirectoryName(assemblyPath);
Debug.Assert(assemblyDirectory is not null);

using MetadataLoadContext loadContext = new(new AssemblyResolver(assemblyDirectory));
Assembly assembly = loadContext.LoadFromAssemblyPath(assemblyPath);
foreach (CustomAttributeData attribute in assembly.GetCustomAttributesData())
{
Expand Down Expand Up @@ -56,10 +58,10 @@ internal static bool IsDebug(string path)
return false;
}

private sealed class RuntimeAssemblyResolver : MetadataAssemblyResolver
private sealed class AssemblyResolver(string assemblyDirectory) : MetadataAssemblyResolver
{
private readonly string _runtimeDirectory = Path.GetDirectoryName(typeof(object).Assembly.Location)
?? throw new InvalidOperationException("The runtime assembly directory is not available.");
private readonly string _assemblyDirectory = assemblyDirectory;
private readonly string _runtimeDirectory = RuntimeEnvironment.GetRuntimeDirectory();

public override Assembly? Resolve(MetadataLoadContext context, AssemblyName assemblyName)
{
Expand All @@ -68,7 +70,13 @@ private sealed class RuntimeAssemblyResolver : MetadataAssemblyResolver
return null;
}

string assemblyPath = Path.Combine(_runtimeDirectory, name + ".dll");
string fileName = name + ".dll";
string assemblyPath = Path.Combine(_assemblyDirectory, fileName);
if (!File.Exists(assemblyPath))
{
assemblyPath = Path.Combine(_runtimeDirectory, fileName);
}

return File.Exists(assemblyPath) ? context.LoadFromAssemblyPath(assemblyPath) : null;
}
}
Expand Down
Loading