Skip to content

.NET MAUI projects supported? #18481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dhindrik opened this issue Jan 13, 2025 · 11 comments
Closed

.NET MAUI projects supported? #18481

dhindrik opened this issue Jan 13, 2025 · 11 comments
Labels
question Further information is requested

Comments

@dhindrik
Copy link

We have used CodeQL for our .NET MAUI app for a while. But last time it was running successful was 13th November. Since than it failed, even if we still was on .NET 8.

We get a lot of errors like this:
{APP_PATH}\obj\Release\net9.0-windows10.0.19041.0\win10-x64\generated\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\App.xaml.sg.cs'.'

I guess it is because of code generation of XAML.

So is .NET MAUI projects supported? If I look at the docs I cannot see .NET MAUI there. I a look back and I can also see that we never had any issues reported even if the run succeed.

Image
Image from, https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/

We have an requirement to run CodeQL on all our repositories, but if it is not supported for .NET MAUI projects we need to explain it for the managers and looking for options.

@dhindrik dhindrik added the question Further information is requested label Jan 13, 2025
@tamasvajk
Copy link
Contributor

I've tried the following to reproduce your issue:

dotnet workload install maui
dotnet new maui --name maui1   
cd maui1
dotnet build -f net9.0-maccatalyst /t:rebuild

The above built the application successfully, so then I tried the below, which created a database:

codeql database create DB1 --language=csharp --command="dotnet build -f net9.0-maccatalyst /t:rebuild"

Could you give a bit more context on the errors that you see? Would you be able to provide a minimal repro of your issue?

@dhindrik
Copy link
Author

Here is an workflow that are more or less the same as we have for the real app, https://github.com/dhindrik/MauiCodeQL/actions/runs/12794061568

@tamasvajk
Copy link
Contributor

Thank you for the repro. This is just a progress report, not a solution yet.

I looked into the debug logs of the job, and found that an exception is thrown here:

 Unhandled exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\a\MauiCodeQL\MauiCodeQL\MauiCodeQL\obj\Release\net9.0-windows10.0.19041.0\win10-x64\generated'.

The path comes from the msbuild property CompilerGeneratedFilesOutputPath, which is actually set to: obj\Release\net9.0-windows10.0.19041.0\win10-x64\/generated, note the odd \/.

This could be handled by wrapping the call with a try-catch. The generated files make analysis more precise, but could be omitted.

At the same time, the compiler is reporting these kind of errors:

CSC : error CS0016: Could not write to output file 'D:\a\MauiCodeQL\MauiCodeQL\MauiCodeQL\obj\Release\net9.0-windows10.0.19041.0\win10-x64\generated\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\App.xaml.sg.cs'

This only happens when the build is executed within a codeql context. The reason for this is that codeql silently modifies the build command, see here, and injects the /p:EmitCompilerGeneratedFiles=true into the call. If this is added to the build command manually, then the build fails even without CodeQL, see here.

@tamasvajk
Copy link
Contributor

@dhindrik I think we can't do much with the compilation failure caused by /p:EmitCompilerGeneratedFiles=true, this seems to be a bug somewhere in the toolchain.

CodeQL also supports creating databases from msbuild binary log files. In this mode, the extractor doesn't rely on the /p:EmitCompilerGeneratedFiles=true property, so this is a viable workaround for the issue that you're facing. Here's a run with this option.

You'd need to change your build command to generate the binary log file, and CodeQL needs to be configured to use build-mode: none with the binlog option, which can be achieved as follows (see the workflow here):

      - name: Initialize CodeQL
        uses: github/codeql-action/init@v3
        with:
          languages: csharp
          build-mode: none
          # ...

      - name: Build app for release
        run: |
          dotnet build -f net9.0-windows10.0.19041.0 -c Release -bl:log.binlog
          echo "CODEQL_EXTRACTOR_CSHARP_OPTION_BINLOG=log.binlog" >> $env:GITHUB_ENV

      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v3

@dhindrik
Copy link
Author

When I am running the real project I now got this error in "Perform CodeQL Analysis"

 Error: 1-21 10:25:14] [ERROR] Spawned process exited abnormally (code 2; tried to run: [C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\tools\win64\runner.exe, cmd.exe, /C, type, NUL, &&, C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\csharp\tools\autobuild.cmd])
  A fatal error occurred: Exit status 2 from command: [C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\tools\win64\runner.exe, cmd.exe, /C, type, NUL, &&, C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\csharp\tools\autobuild.cmd]
  Error: Encountered a fatal error while running "C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\codeql.exe database trace-command --use-build-mode --working-dir D:\a\{AppPath}  D:\a\_temp\codeql_databases\csharp". Exit code was 2 and error was: A fatal error occurred: Exit status 2 from command: [C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\tools\win64\runner.exe, cmd.exe, /C, type, NUL, &&, C:\hostedtoolcache\windows\CodeQL\2.20.1\x64\codeql\csharp\tools\autobuild.cmd]. See the logs for more details.

@tamasvajk
Copy link
Contributor

Could you rerun the job with debug logging enabled? Then you can check the workflow artifact for more details of the error.

@dhindrik
Copy link
Author

This is more or less what I can find in the logs that seems to be error related.

[2476] [001] [INFO]  Running binary log analysis.
[2476] [001] [INFO] Reading compiler calls from binary log log.binlog
[2476] [001] [ERROR] Failed to open binary log: Could not find file 'D:\a\{AppName}\{AppName}\log.binlog'.

Some screenshots about what the zip contains:
Image

Image

@tamasvajk
Copy link
Contributor

Could not find file 'D:\a\{AppName}\{AppName}\log.binlog'. suggests that the binlog file was not created by the build command. Can you double check if you added -bl:log.binlog to your build command?

@dhindrik
Copy link
Author

Yes, I added it:. Here is a screenshot from the build log:

Image

@tamasvajk
Copy link
Contributor

Ahh, I think the file is at D:\a\{AppName}\{AppName}\src\log.binlog and not D:\a\{AppName}\{AppName}\log.binlog. Could you adjust the CODEQL_EXTRACTOR_CSHARP_OPTION_BINLOG to point to that location?

@dhindrik
Copy link
Author

That worked! Thank you for your help! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants