dotnet / dotnet/roslyn

ServiceHub.RoslynCodeAnalysisService.exe ignores TargetPathWithTargetPlatformMoniker?

Open
#79,868 9 comments 0 reactions 1 assignee Claimed by @jasonmalinowski View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

I have a generator that uses some Nuget packages, so I add `TargetPathWithTargetPlatformMoniker` items for all dlls that need to be loaded:

```

$(GetTargetPathDependsOn);GetDependencyTargetPaths




... other dependencies ...


```

This works great when the analyzer is called from `csc.exe` or `VBCSCompiler.exe`, but doesn't work from `ServiceHub.RoslynCodeAnalysisService.exe`. The issue is that there is a `MissingMethodException` thrown, which seems to be due to incorrect versions of some dependencies being loaded.

I attached the debugger to each of these processes and found that the loading behavior is different for each of them:

- csc.exe
- Loads the assembly from the Nuget folder path, exactly as specified in `TargetPathWithTargetPlatformMoniker`, which works great. For example: `%USERPROFILE%\.nuget\packages\system.text.encodings.web\9.0.0\lib\netstandard2.0\System.Text.Encodings.Web.dll`
- VBCSCompiler.exe
- Loads the assembly from some temporary location, which also works fine, since the temporary file seems to be a copy of the file from the package. For example: `%TEMP%\VBCSCompiler\AnalyzerAssemblyLoader\6a0c4919d01f499ab357ef804bdbc838\13\System.Text.Encodings.Web.dll`
- ServiceHub.RoslynCodeAnalysisService.exe
- This is where the issue starts, since the assembly is loaded from some shared .NET location, for example `C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\ServiceHub\Hosts\ServiceHub.Host.dotnet.x64\..\..\SharedAssemblies\System.Text.Encodings.Web.dll`. Some other assemblies are loaded from `C:\Program Files\Microsoft Visual Studio\2022\Enterprise\dotnet\net8.0\runtime\shared\Microsoft.AspNetCore.App\8.0.18` and maybe other common .NET folders. This causes exceptions since incompatible package versions from different places are being used, and it seems `TargetPathWithTargetPlatformMoniker` does not have any effect at all in this process.

Is this expected behavior? Is there any way to make `ServiceHub.RoslynCodeAnalysisService.exe` use `TargetPathWithTargetPlatformMoniker` just like other processes (`csc.exe` and `VBCSCompiler.exe`) do?

I realize this might be because these assemblies are already loaded by the service process, so it just tries to use them, without even having to look at `TargetPathWithTargetPlatformMoniker`.

**Version Used**: Visual Studio 17.14.9

**Expected Behavior**: `ServiceHub.RoslynCodeAnalysisService.exe` loads dlls from paths that are specified in `TargetPathWithTargetPlatformMoniker`.

**Actual Behavior**: `ServiceHub.RoslynCodeAnalysisService.exe` loads dlls from common .NET locations.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.