Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit e3d8000

Browse files
author
Kirill Kornienko
committed
Fixed publish self-contained win-x64. Fixed publish with Trimmed option
1 parent 58fc5ce commit e3d8000

File tree

3 files changed

+115
-15
lines changed

3 files changed

+115
-15
lines changed

src/shared/DotNetClasses/CSharpCodeProvider.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ public class CSharpCodeProvider : CodeDomProvider
1919

2020
public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp, string code)
2121
{
22+
#if DEBUG
23+
Console.WriteLine("FR.Compat: " +
24+
#if NETSTANDARD
25+
"NETSTANDARD"
26+
#elif NETCOREAPP
27+
"NETCOREAPP"
28+
#endif
29+
);
30+
#endif
31+
2232
SyntaxTree codeTree = CSharpSyntaxTree.ParseText(code);
2333
CSharpCompilationOptions options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
2434
optimizationLevel: OptimizationLevel.Release,
@@ -27,11 +37,8 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,
2737

2838
List<MetadataReference> references = new List<MetadataReference>();
2939

40+
AddReferences(cp, references);
3041

31-
foreach (string reference in cp.ReferencedAssemblies)
32-
references.Add(GetReference(reference));
33-
34-
AddExtraAssemblies(cp.ReferencedAssemblies, references);
3542

3643
Compilation compilation = CSharpCompilation.Create(
3744
"_" + Guid.NewGuid().ToString("D"), new SyntaxTree[] { codeTree },

src/shared/DotNetClasses/CodeDomProvider.cs

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,62 @@ public abstract class CodeDomProvider : IDisposable
2222

2323
public event EventHandler<CompilationEventArgs> BeforeEmitCompilation;
2424

25+
#if NETCOREAPP
26+
/// <summary>
27+
/// If these assemblies were not found when 'trimmed', then skip them
28+
/// </summary>
29+
protected static readonly string[] SkippedAssemblies = new string[] {
30+
"System.dll",
31+
32+
"System.Drawing.dll",
33+
34+
//"System.Drawing.Primitives",
35+
36+
"System.Data.dll",
37+
38+
"System.Xml.dll",
39+
};
40+
#endif
41+
42+
protected void AddReferences(CompilerParameters cp, List<MetadataReference> references)
43+
{
44+
foreach (string reference in cp.ReferencedAssemblies)
45+
{
46+
#if DEBUG
47+
Console.WriteLine($"TRY ADD {reference}.");
48+
#endif
49+
#if NETCOREAPP
50+
try
51+
{
52+
#endif
53+
references.Add(GetReference(reference));
54+
#if NETCOREAPP
55+
}
56+
catch (FileNotFoundException e)
57+
{
58+
if (SkippedAssemblies.Contains(reference))
59+
{
60+
#if DEBUG
61+
Console.WriteLine($"{reference} FileNotFound. SKIPPED");
62+
#endif
63+
continue;
64+
}
65+
else
66+
throw e;
67+
}
68+
#endif
69+
70+
#if DEBUG
71+
Console.WriteLine($"{reference} ADDED");
72+
#endif
73+
}
74+
#if DEBUG
75+
Console.WriteLine("AFTER ADDING ReferencedAssemblies");
76+
#endif
77+
78+
AddExtraAssemblies(cp.ReferencedAssemblies, references);
79+
}
80+
2581

2682
protected void AddExtraAssemblies(StringCollection referencedAssemblies, List<MetadataReference> references)
2783
{
@@ -52,7 +108,25 @@ protected void AddExtraAssemblies(StringCollection referencedAssemblies, List<Me
52108
foreach(string assembly in assemblies)
53109
{
54110
if (!referencedAssemblies.Contains(assembly))
55-
references.Add(GetReference(assembly));
111+
{
112+
#if NETCOREAPP
113+
try
114+
{
115+
#endif
116+
references.Add(GetReference(assembly));
117+
#if NETCOREAPP
118+
}
119+
// If user run 'dotnet publish' with Trimmed - dotnet cut some extra assemblies.
120+
// We skip this error, because some assemblies in 'assemblies' array may not be needed
121+
catch (FileNotFoundException)
122+
{
123+
#if DEBUG
124+
Console.WriteLine($"{assembly} FILENOTFOUND. SKIPPED");
125+
#endif
126+
continue;
127+
}
128+
#endif
129+
}
56130
}
57131
}
58132

@@ -76,7 +150,7 @@ public MetadataReference GetReference(string refDll)
76150
return cache[refDll];
77151

78152
reference = refDll.EndsWith(".dll") || refDll.EndsWith(".exe") ?
79-
refDll.Substring(0, refDll.Length - 4) : refDll;
153+
refDll.Substring(0, refDll.Length - 4) : refDll;
80154
if (!refDll.Contains(Path.DirectorySeparatorChar))
81155
{
82156
#if NETCOREAPP
@@ -87,6 +161,9 @@ public MetadataReference GetReference(string refDll)
87161
{
88162
if (loadedAssembly.GetName().Name == reference)
89163
{
164+
#if DEBUG
165+
Console.WriteLine("FIND IN AssemblyLoadContext");
166+
#endif
90167
string location = loadedAssembly.Location;
91168
if (string.IsNullOrEmpty(location))
92169
{
@@ -107,6 +184,9 @@ public MetadataReference GetReference(string refDll)
107184
{
108185
if (string.Compare(currAssembly.GetName().Name, reference, true) == 0)
109186
{
187+
#if DEBUG
188+
Console.WriteLine("FIND IN AppDomain");
189+
#endif
110190
// Found it, return the location as the full reference.
111191
result = MetadataReference.CreateFromFile(currAssembly.Location);
112192
cache[refDll] = result;
@@ -119,6 +199,9 @@ public MetadataReference GetReference(string refDll)
119199
{
120200
if (name.Name == reference)
121201
{
202+
#if DEBUG
203+
Console.WriteLine("FIND IN ReferencedAssemblies");
204+
#endif
122205
#if NETCOREAPP
123206
// try load Assembly in runtime (for user script with custom assembly)
124207
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(name);
@@ -170,16 +253,29 @@ public MetadataReference GetReference(string refDll)
170253
catch
171254
{
172255
var assemblyName = new AssemblyName(reference);
256+
#if DEBUG
257+
Console.WriteLine("IN AssemblyName");
258+
#endif
259+
173260
#if NETCOREAPP
174261
// try load Assembly in runtime (for user script with custom assembly)
175262
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
176263
#else
177264
var assembly = Assembly.Load(assemblyName);
178265
#endif
179-
180266
string location = assembly.Location;
267+
#if DEBUG
268+
Console.WriteLine($"Location after LoadFromAssemblyName: {location}");
269+
#endif
181270

182-
result = MetadataReference.CreateFromFile(location);
271+
#if NETCOREAPP
272+
if(string.IsNullOrEmpty(location))
273+
{
274+
result = GetMetadataReferenceInSingleFileApp(assembly);
275+
}
276+
else
277+
#endif
278+
result = MetadataReference.CreateFromFile(location);
183279
cache[refDll] = result;
184280
return result;
185281
}
@@ -188,6 +284,9 @@ public MetadataReference GetReference(string refDll)
188284
#if NETCOREAPP
189285
private static unsafe MetadataReference GetMetadataReferenceInSingleFileApp(Assembly assembly)
190286
{
287+
#if DEBUG
288+
Console.WriteLine($"TRY IN UNSAFE METHOD {assembly.GetName().Name}");
289+
#endif
191290
assembly.TryGetRawMetadata(out byte* blob, out int length);
192291
var moduleMetadata = ModuleMetadata.CreateFromMetadata((IntPtr)blob, length);
193292
var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);

src/shared/DotNetClasses/VBCodeProvider.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,
2828
List<MetadataReference> references = new List<MetadataReference>();
2929

3030

31-
foreach (string reference in cp.ReferencedAssemblies)
32-
references.Add(GetReference(reference));
33-
34-
AddExtraAssemblies(cp.ReferencedAssemblies, references);
35-
36-
37-
31+
AddReferences(cp, references);
3832

3933
Compilation compilation = VisualBasicCompilation.Create(
4034
"_" + Guid.NewGuid().ToString("D"), new SyntaxTree[] { codeTree },

0 commit comments

Comments
 (0)