Skip to content

Commit fd8cda3

Browse files
authored
Merge pull request github#16924 from tamasvajk/feature/winforms-linux
C#: Restore Windows dependencies when Windows Forms or WPF usage is detected
2 parents 57efb84 + 7387c56 commit fd8cda3

File tree

13 files changed

+188
-3
lines changed

13 files changed

+188
-3
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ private string GetRestoreArgs(RestoreSettings restoreSettings)
6969
args += " --force";
7070
}
7171

72+
if (restoreSettings.TargetWindows)
73+
{
74+
args += " /p:EnableWindowsTargeting=true";
75+
}
76+
7277
return args;
7378
}
7479

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IDotNet
1717
IList<string> GetNugetFeedsFromFolder(string folderPath);
1818
}
1919

20-
public record class RestoreSettings(string File, string PackageDirectory, bool ForceDotnetRefAssemblyFetching, string? PathToNugetConfig = null, bool ForceReevaluation = false);
20+
public record class RestoreSettings(string File, string PackageDirectory, bool ForceDotnetRefAssemblyFetching, string? PathToNugetConfig = null, bool ForceReevaluation = false, bool TargetWindows = false);
2121

2222
public partial record class RestoreResult(bool Success, IList<string> Output)
2323
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,13 @@ private IEnumerable<string> RestoreSolutions(out DependencyContainer dependencie
225225
var successCount = 0;
226226
var nugetSourceFailures = 0;
227227
var assets = new Assets(logger);
228+
229+
var isWindows = fileContent.UseWindowsForms || fileContent.UseWpf;
230+
228231
var projects = fileProvider.Solutions.SelectMany(solution =>
229232
{
230233
logger.LogInfo($"Restoring solution {solution}...");
231-
var res = dotnet.Restore(new(solution, PackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: true));
234+
var res = dotnet.Restore(new(solution, PackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: true, TargetWindows: isWindows));
232235
if (res.Success)
233236
{
234237
successCount++;
@@ -258,6 +261,9 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
258261
var successCount = 0;
259262
var nugetSourceFailures = 0;
260263
ConcurrentBag<DependencyContainer> collectedDependencies = [];
264+
265+
var isWindows = fileContent.UseWindowsForms || fileContent.UseWpf;
266+
261267
var sync = new object();
262268
var projectGroups = projects.GroupBy(Path.GetDirectoryName);
263269
Parallel.ForEach(projectGroups, new ParallelOptions { MaxDegreeOfParallelism = DependencyManager.Threads }, projectGroup =>
@@ -266,7 +272,7 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
266272
foreach (var project in projectGroup)
267273
{
268274
logger.LogInfo($"Restoring project {project}...");
269-
var res = dotnet.Restore(new(project, PackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: true));
275+
var res = dotnet.Restore(new(project, PackageDirectory.DirInfo.FullName, ForceDotnetRefAssemblyFetching: true, TargetWindows: isWindows));
270276
assets.AddDependenciesRange(res.AssetsFilePaths);
271277
lock (sync)
272278
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/Accessibility.dll |
2+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/Microsoft.VisualBasic.Forms.dll |
3+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/Microsoft.VisualBasic.dll |
4+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/Microsoft.Win32.Registry.AccessControl.dll |
5+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/Microsoft.Win32.SystemEvents.dll |
6+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationCore.dll |
7+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.Aero2.dll |
8+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.Aero.dll |
9+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.AeroLite.dll |
10+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.Classic.dll |
11+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.Luna.dll |
12+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.Royale.dll |
13+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationFramework.dll |
14+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/PresentationUI.dll |
15+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/ReachFramework.dll |
16+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.CodeDom.dll |
17+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Configuration.ConfigurationManager.dll |
18+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Design.dll |
19+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Diagnostics.EventLog.dll |
20+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Diagnostics.PerformanceCounter.dll |
21+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.DirectoryServices.dll |
22+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Drawing.Common.dll |
23+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Drawing.Design.dll |
24+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Drawing.dll |
25+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.IO.Packaging.dll |
26+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Printing.dll |
27+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Resources.Extensions.dll |
28+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Security.Cryptography.Pkcs.dll |
29+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Security.Cryptography.ProtectedData.dll |
30+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Security.Cryptography.Xml.dll |
31+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Security.Permissions.dll |
32+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Threading.AccessControl.dll |
33+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Controls.Ribbon.dll |
34+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Extensions.dll |
35+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Forms.Design.Editors.dll |
36+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Forms.Design.dll |
37+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Forms.Primitives.dll |
38+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Forms.dll |
39+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Input.Manipulations.dll |
40+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Windows.Presentation.dll |
41+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/System.Xaml.dll |
42+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/UIAutomationClient.dll |
43+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/UIAutomationClientSideProviders.dll |
44+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/UIAutomationProvider.dll |
45+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/UIAutomationTypes.dll |
46+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/WindowsBase.dll |
47+
| [...]/microsoft.windowsdesktop.app.ref/8.0.1/ref/net8.0/WindowsFormsIntegration.dll |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import csharp
2+
3+
private string getPath(Assembly a) {
4+
not a.getCompilation().getOutputAssembly() = a and
5+
exists(string s | s = a.getFile().getAbsolutePath() |
6+
result = "[...]" + s.substring(s.indexOf("microsoft.windowsdesktop.app.ref") - 1, s.length())
7+
)
8+
}
9+
10+
from Assembly a
11+
select getPath(a)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
| All Nuget feeds reachable | 1.0 |
2+
| Failed project restore with package source error | 0.0 |
3+
| Failed solution restore with package source error | 0.0 |
4+
| NuGet feed responsiveness checked | 1.0 |
5+
| Project files on filesystem | 1.0 |
6+
| Reachable fallback Nuget feed count | 1.0 |
7+
| Resource extraction enabled | 0.0 |
8+
| Restored .NET framework variants | 1.0 |
9+
| Restored projects through solution files | 0.0 |
10+
| Solution files on filesystem | 0.0 |
11+
| Source files generated | 1.0 |
12+
| Source files on filesystem | 3.0 |
13+
| Successfully restored project files | 1.0 |
14+
| Successfully restored solution files | 0.0 |
15+
| Unresolved references | 0.0 |
16+
| UseWPF set | 0.0 |
17+
| UseWindowsForms set | 1.0 |
18+
| WebView extraction enabled | 1.0 |
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import csharp
2+
import semmle.code.csharp.commons.Diagnostics
3+
4+
query predicate compilationInfo(string key, float value) {
5+
key != "Resolved references" and
6+
key != "Resolved assembly conflicts" and
7+
not key.matches("Compiler diagnostic count for%") and
8+
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
9+
key = infoKey and
10+
value = infoValue.toFloat()
11+
or
12+
not exists(infoValue.toFloat()) and
13+
key = infoKey + ": " + infoValue and
14+
value = 1
15+
)
16+
}

csharp/ql/integration-tests/all-platforms/standalone_winforms/Form1.Designer.cs

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace winforms;
2+
3+
public partial class Form1 : Form
4+
{
5+
public Form1()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace winforms;
2+
3+
static class Program
4+
{
5+
/// <summary>
6+
/// The main entry point for the application.
7+
/// </summary>
8+
[STAThread]
9+
static void Main()
10+
{
11+
// To customize application configuration such as set high DPI settings or default font,
12+
// see https://aka.ms/applicationconfiguration.
13+
ApplicationConfiguration.Initialize();
14+
Application.Run(new Form1());
15+
}
16+
}

0 commit comments

Comments
 (0)