runtime icon indicating copy to clipboard operation
runtime copied to clipboard

System.Text.Json does not support constructors with byref parameters.

Open mattchidley opened this issue 5 years ago • 25 comments

Description

I have a List of objects whose constructors take 'in' parameters of DateTime and TimeSpan.

public class TestClass
    {
        public TestClass(in DateTime dateTime, in TimeSpan timeSpan)
        {
            DateTime = dateTime;
            TimeSpan = timeSpan;
        }

        public DateTime DateTime { get; set; }

        public TimeSpan TimeSpan { get; set; }
    }

When serializing a list of these objects, I am seeing the exception in the title being thrown:

image

image

This is specifically being thrown at IEnumerableDefaultConverter.cs line 255:

state.Current.DeclaredJsonPropertyInfo = state.Current.JsonClassInfo.ElementClassInfo!.PropertyInfoForClassInfo;

Unfortunately I have absolutely no clue what the issue is here. I'm hoping you might be able to help.

mattchidley avatar Dec 15 '20 19:12 mattchidley

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

Dotnet-GitSync-Bot avatar Dec 15 '20 19:12 Dotnet-GitSync-Bot

Looks like System.Text.Json doesn't understand byrefs as ctor arguments? You can work around this for now by removing the in keyword from your ctor parameters.

GrabYourPitchforks avatar Dec 16 '20 01:12 GrabYourPitchforks

Hi @GrabYourPitchforks Im looking for an issue to take as my fist contribution and stumbled upon this issue. Do you think its ok if i take it on me?

ilanb1996 avatar Apr 18 '21 20:04 ilanb1996

@layomia - thoughts?

GrabYourPitchforks avatar Apr 18 '21 22:04 GrabYourPitchforks

@GrabYourPitchforks, @ilanb1996 sounds good, thanks! I'll assign the issue to you - please tag me with any questions.

layomia avatar Apr 19 '21 05:04 layomia

Hey @layomia , sorry for not updating recently, had been busy with work (just started new position). I'll start working on it this week, hope ill have a failing test by Friday afternoon and then ill submit a fix.

ilanb1996 avatar May 04 '21 20:05 ilanb1996

Congrats on your new position! 🥳 Please feel free to reach out if you need assistance with anything.

GrabYourPitchforks avatar May 04 '21 21:05 GrabYourPitchforks

@GrabYourPitchforks Im trying to run the tests in the solution (im working on System.Text.Json) but i cant build the project from visual studio. I tried using the build.sh script and it worked but i want to run only the tests relevant to the code im changing from visual studio. running the tests from build.sh takes very long time.

image

ilanb1996 avatar May 08 '21 16:05 ilanb1996

@GrabYourPitchforks Hey just bumping the issue...

ilanb1996 avatar May 21 '21 16:05 ilanb1996

Apologies, didn't see your earlier message.

I've seen the "Binary stream '101' does not contain a valid BinaryHeader." error before. Not sure what causes it, but git clean -xdf and rebuilding the repo has always resolved it for me.

Here's the workflow I use to run tests within a single project. The first two lines below build the runtime & libraries & unit tests (but does not run them!), and the last line runs the unit tests for the specific project I care about. (Replace the path to -proj below as appropriate.)

build -s clr -c Release
build -s libs+libs.tests -rc Release
build -proj src\libraries\System.Blah\tests\System.Blah.Tests.csproj -rc Release -test

I believe that VS's unit test runner will start working as long as you've run the first two lines above first, then you can use the IDE.

GrabYourPitchforks avatar May 21 '21 17:05 GrabYourPitchforks

@GrabYourPitchforks thanks for the reply :) When i run the second command i get the following error:

Build FAILED.

/Users/ilan.beloglovsky/Desktop/personal/runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs(252,21): Trim analysis error IL2062: Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor,Type,CallSiteChain,Int32): Value passed to parameter 'implementationType' of method 'Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache,Type,Type,CallSiteChain)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements. [/Users/ilan.beloglovsky/Desktop/personal/runtime/src/libraries/src.proj]
/Users/ilan.beloglovsky/Desktop/personal/runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteFactory.cs(293,17): Trim analysis error IL2062: Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateOpenGeneric(ServiceDescriptor,Type,CallSiteChain,Int32,Boolean): Value passed to parameter 'implementationType' of method 'Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache,Type,Type,CallSiteChain)' can not be statically determined and may not meet 'DynamicallyAccessedMembersAttribute' requirements. [/Users/ilan.beloglovsky/Desktop/personal/runtime/src/libraries/src.proj]

I use mac with dotnet 6.0.100-preview.3.21202.5

ilanb1996 avatar May 21 '21 18:05 ilanb1996

@GrabYourPitchforks any suggestions with the above issue?

ilanb1996 avatar May 30 '21 17:05 ilanb1996

So i've decided to uninstall dotnet 6 and try to use dotnet 5 instead, i used this https://docs.microsoft.com/en-us/dotnet/core/install/remove-runtime-sdk-versions?pivots=os-macos article for it but when i list the version of dotnet i get the following message 6.0.100-preview.3.21202.5 (x64) [Cannot uninstall version 6.0.0 and above] Is it worth trying investigate further in this direction or the build scripts should work with dotnet 6 as well?

ilanb1996 avatar Jun 09 '21 19:06 ilanb1996

@layomia do you have any thoughts on the problem the contributor is experiencing?

GrabYourPitchforks avatar Jun 09 '21 19:06 GrabYourPitchforks

@ilanb1996 I'm surprised you were seeing trim analysis errors (https://github.com/dotnet/runtime/issues/46088#issuecomment-846144732) when running the build. @eerhardt / @joperezr is this expected?

Have you synced with the latest changes from the main branch? I'd start a clean build of the latest changes from main, stick with a dotnet6 SDK, then try the steps from https://github.com/dotnet/runtime/issues/46088#issuecomment-846115579.

cc @ViktorHofer in case there's further guidance on building for MacOS here.

layomia avatar Jun 09 '21 20:06 layomia

I'm surprised you were seeing trim analysis errors (#46088 (comment)) when running the build. @eerhardt / @joperezr is this expected?

No, you shouldn't be seeing those, assuming you have the right SDK installed.

eerhardt avatar Jun 09 '21 20:06 eerhardt

Is it worth trying investigate further in this direction or the build scripts should work with dotnet 6 as well?

The repository actually requires that the dotnet 6.0 Preview 3 SDK is installed if you want to use tools like Visual Studio or VSCode. There are helper files and switches in the repository to avoid that global install, i.e. the dotnet.cmd/sh scripts which use a local bootstrapped SDK or the build.cmd -vs switch which opens VS with a modified path so that it again uses the locally bootstrapped SDK. But usually I would recommend to install a matching SDK globally if possible.

I don't think you would be able to build individual libraries with an earlier SDK as the sdk version specified in the repo's global.json specifies the min required version that msbuild checks on.

@ilanb1996 what's the current commit you're on?

ViktorHofer avatar Jun 09 '21 20:06 ViktorHofer

@ViktorHofer Im on 43b1ce5d6b6

ilanb1996 avatar Jun 10 '21 07:06 ilanb1996

@ViktorHofer Im on 43b1ce5

Thanks for letting us know. Would it be possible for you to rebase onto the current main's HEAD? I assume that some of the issues that you were seeing will go away by doing so. After rebasing make sure that you do a clean build:

git clean -xdf
./build.sh clr+libs -rc Release

ViktorHofer avatar Jun 10 '21 07:06 ViktorHofer

@ViktorHofer Im on 43b1ce5

Thanks for letting us know. Would it be possible for you to rebase onto the current main's HEAD? I assume that some of the issues that you were seeing will go away by doing so. After rebasing make sure that you do a clean build:

git clean -xdf
./build.sh clr+libs -rc Release

I did all the steps you suggested but i still cant build the project from visual studio for mac... image I tried restoring the nuget packages but it failed, I think they want dotnet core 6 preview 4 (i have only preview 3) thats the error:

Getting restore information for solution /Users/ilan.beloglovsky/Desktop/personal/runtime/src/libraries/System.Text.Json/System.Text.Json.sln
A compatible installed .NET Core SDK for global.json version [6.0.100-preview.4.21255.9] from [/Users/ilan.beloglovsky/Desktop/personal/runtime/global.json] was not found
Install the [6.0.100-preview.4.21255.9] .NET Core SDK or update [/Users/ilan.beloglovsky/Desktop/personal/runtime/global.json] with an installed .NET Core SDK:

A compatible installed .NET Core SDK for global.json version [6.0.100-preview.4.21255.9] from [/Users/ilan.beloglovsky/Desktop/personal/runtime/global.json] was not found
Install the [6.0.100-preview.4.21255.9] .NET Core SDK or update [/Users/ilan.beloglovsky/Desktop/personal/runtime/global.json] with an installed .NET Core SDK:

/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018: The "WriteRestoreGraphTask" task failed unexpectedly. [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018: NuGet.Frameworks.FrameworkException: Invalid framework identifier ''. [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.Frameworks.NuGetFramework.GetShortFolderName (NuGet.Frameworks.IFrameworkNameProvider mappings) [0x0006f] in <6320f402f7944f8eb51291e628b0cb3f>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/
bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.Frameworks.NuGetFramework.GetShortFolderName () [0x00006] in <6320f402f7944f8eb51291e628b0cb3f>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.ProjectModel.PackageSpecWriter.WriteMetadataTargetFrameworks (NuGet.RuntimeModel.IObjectWriter writer, NuGet.ProjectModel.ProjectRestoreMetadata msbuildMetadata) [0x00074] in <32c305740b2d40cfb589237fb6cd3658>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.ProjectModel.PackageSpecWriter.SetMSBuildMetadata (NuGet.RuntimeModel.IObjectWriter writer, NuGet.ProjectModel.PackageSpec packageSpec) [0x00119] in <32c305740b2d40cfb5
89237fb6cd3658>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.ProjectModel.PackageSpecWriter.Write (NuGet.ProjectModel.PackageSpec packageSpec, NuGet.RuntimeModel.IObjectWriter writer) [0x000bf] in <32c305740b2d40cfb589237fb6cd3658>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.ProjectModel.DependencyGraphSpec.Write (NuGet.RuntimeModel.IObjectWriter writer, System.Action`2[T1,T2] writeAction) [0x0009b] in <32c305740b2d40cfb589237fb6cd3658>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuG
et.targets(163,5): error MSB4018:   at NuGet.ProjectModel.DependencyGraphSpec.Save (System.String path) [0x00024] in <32c305740b2d40cfb589237fb6cd3658>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at NuGet.Build.Tasks.WriteRestoreGraphTask.Execute () [0x000e8] in <cf2b79aeaf2349769d5a80e504e6400c>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00029] in <e45f684672594c219e19c34e591a0726>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]
/Applications/Visual Studio.app/Contents/Resources/lib/monodeve
lop/bin/MSBuild/Current/bin/NuGet.targets(163,5): error MSB4018:   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x002b9] in <e45f684672594c219e19c34e591a0726>:0  [/var/folders/lb/7y8pln9x32b6rdv9hdz9d2cmdczs2t/T/NuGetScratch/rbxapid8.ufa.nugetinputs.targets]

  3.1.408 [/usr/local/share/dotnet/sdk]
  3.1.409 [/usr/local/share/dotnet/sdk]
  5.0.202 [/usr/local/share/dotnet/sdk]
  5.0.203 [/usr/local/share/dotnet/sdk]
  5.0.301 [/usr/local/share/dotnet/sdk]
  6.0.100-preview.3.21202.5 [/usr/local/share/dotnet/sdk]
  3.1.408 [/usr/local/share/dotnet/sdk]
  3.1.409 [/usr/local/share/dotnet/sdk]
  5.0.202 [/usr/local/share/dotnet/sdk]
  5.0.203 [/usr/local/share/dotnet/sdk]
  5.0.301 [/usr/local/share/dotnet/sdk]
  6.0.100-preview.3.21202.5 [/usr/local/share/dotnet/sdk]

MSBuild exited with code 1

I'll try install preview 4 and update if that helps resolve the issue

ilanb1996 avatar Jun 12 '21 14:06 ilanb1996