Skip to content

Commit fcedb50

Browse files
authored
[mono] Don't call Assembly.CodeBase directly in RuntimeAssembly.GetName (#54895)
* [mono] Don't call Assembly.CodeBase directly in RuntimeAssembly.GetName It's marked as not available in single file apps. Call the underlying get_code_base icall. Fixes #54835 * [icall] Use MonoImage:filename for RuntimeAssembly.get_code_base For bundled asssemblies in single file scenarios, RuntimeAssembly.CodeBase will be null, matching CoreCLR. * disable codebase test on wasm
1 parent 121bcd1 commit fcedb50

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/libraries/System.Reflection/tests/AssemblyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public void Location_ExecutingAssembly_IsNotNull()
510510
}
511511

512512
#pragma warning disable SYSLIB0012
513-
[Fact]
513+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] // single file
514514
public void CodeBase()
515515
{
516516
Assert.NotEmpty(Helpers.ExecutingAssembly.CodeBase);

src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,7 @@ private static void AddPublicNestedTypes(Type type, List<Type> types, List<Excep
253253

254254
public override AssemblyName GetName(bool copiedName)
255255
{
256-
#pragma warning disable IL3002 // Suppressing for now. See https://github.com/dotnet/runtime/issues/54835
257-
return AssemblyName.Create(_mono_assembly, CodeBase);
258-
#pragma warning restore IL3002
256+
return AssemblyName.Create(_mono_assembly, get_code_base (this));
259257
}
260258

261259
[RequiresUnreferencedCode("Types might be removed")]

src/mono/mono/metadata/icall.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4462,12 +4462,18 @@ MonoStringHandle
44624462
ves_icall_System_Reflection_RuntimeAssembly_get_code_base (MonoReflectionAssemblyHandle assembly, MonoError *error)
44634463
{
44644464
MonoAssembly *mass = MONO_HANDLE_GETVAL (assembly, assembly);
4465-
gchar *absolute;
44664465

4467-
if (g_path_is_absolute (mass->image->name)) {
4468-
absolute = g_strdup (mass->image->name);
4466+
/* return NULL for bundled assemblies in single-file scenarios */
4467+
const char* filename = m_image_get_filename (mass->image);
4468+
4469+
if (!filename)
4470+
return NULL_HANDLE_STRING;
4471+
4472+
gchar *absolute;
4473+
if (g_path_is_absolute (filename)) {
4474+
absolute = g_strdup (filename);
44694475
} else {
4470-
absolute = g_build_filename (mass->basedir, mass->image->name, (const char*)NULL);
4476+
absolute = g_build_filename (mass->basedir, filename, (const char*)NULL);
44714477
}
44724478

44734479
mono_icall_make_platform_path (absolute);

0 commit comments

Comments
 (0)