sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Support UTF-16 BOM for global.json

Open schellap opened this issue 10 years ago • 7 comments

The deps.json, runtimeconfig.json files are build generated, but global.json is user generated. So this can be encoded in UTF-16.

See https://github.com/dotnet/cli/issues/2159

@gkhanna79 I just ran into this issue with a global.json that has a UTF-16 BOM.

I'm testing .NET on a Windows Nano Server container, so I used PowerShell's Out-File cmdlet to create the global.json file, which appears to use UTF-16 by default.

I was able to work around the issue by removing the BOM by using [IO.File]::WriteAllText. I also verified that using a UTF-8 BOM is another workaround, e.g. by using Out-File -Encoding utf8.

I'm wondering if it would be worth fixing this issue for UTF-16 in addition to UTF-8, because I think people might run into it, especially because the error message does not make it obvious what the problem is.

cc: @dleeapho @MichaelSimons

Actual Behavior

Supports UTF-8 BOM

Expected Behavior

Support UTF-16 BOM

schellap avatar Jun 17 '16 02:06 schellap

Cc @naamunds

@blackdwarf could you please call this out in 1.0.0 Release Notes that the global.json file should be in UTF-8. This is the encoding VS uses, but powershell could write a text file in UTF-16.

schellap avatar Jun 17 '16 02:06 schellap

Cc @piotrpMSFT

schellap avatar Jun 17 '16 02:06 schellap

/cc @leecow @joshfree for visibility.

Sure, I will add this to the release notes. Just to confirm, the issue is that if global.json has UTF16 BOM, all of the CLI commands will error out?

thesmithee avatar Jun 22 '16 13:06 thesmithee

@schellap it looks like @pakrym added a test asset to dotnet/cli which exhibited this scenario. However, I don't see an associated test being created so it did not get moved when @brthor moved host tests to dotnet/core-setup. I'm going to be removing the test asset from CLI since we don't need it anymore. However, here is the commit so you can grab it as needed.

https://github.com/dotnet/cli/commit/8d1ddcdf2dfbbee7907f9c65e58cdef892c0b488

TheRealPiotrP avatar Jun 29 '16 00:06 TheRealPiotrP

Isn't it that the Json format used here, specifically states, that Json files must be UTF-8 encoded? In such case adding support for other encodings would be a deviation from this format's standard and this would need to be documented (as opposite to having only UTF-8 support, which is per Json specification).

hilari0n avatar Nov 21 '23 18:11 hilari0n

it should at least give a better error

this error in no way directs people that they have a file with incorrect encoding

A JSON parsing exception occurred in [C:\Code\LegislationManager\src\global.json], offset 0 (line 1, column 1): Invalid value.
C:\Program Files\dotnet\sdk\10.0.100-preview.3.25160.17\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.ImportWorkloads.props(14,38): error MSB4242:
  SDK Resolver Failure: "The SDK resolver "Microsoft.DotNet.MSBuildWorkloadSdkResolver" failed while attempting to resol
  ve the SDK "Microsoft.NET.SDK.WorkloadAutoImportPropsLocator". Exception: "System.Text.Json.JsonReaderException: '0xFF
  ' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0.

SimonCropp avatar Apr 15 '25 00:04 SimonCropp

This might be cauesd by [https://github.com/baronfel/sdk/blob/3fe1e18d05b0b40e644a00c83eef4b97833c83b3/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/SdkDirectoryWorkloadManifestProvider.GlobalJsonReader.cs#L29] where one of our common global.json readers assumes UTF8-ed-ness. We can't do that, so we should make this pathway able to read files of many encodings.

baronfel avatar Feb 24 '26 22:02 baronfel