Skip to content

Fix updating globaljsonworkloadsets.json - #50372

Merged
baronfel merged 16 commits into
dotnet:mainfrom
angyanmark:fix-globaljsonworkloadsets-json
Oct 6, 2026
Merged

baronfel merged 16 commits into
dotnet:mainfrom
angyanmark:fix-globaljsonworkloadsets-json

Conversation

@angyanmark

@angyanmark angyanmark commented Aug 19, 2025 •

Copy link
Copy Markdown
Contributor

Current logic sets the file stream position to 0, and overwrites the content of the globaljsonworkloadsets.json file.
If the new content is shorter, then some characters remain from the old content, thus making the JSON malformed.

Example

  1. Setting workloadVersion in global.json to 9.0.303.1.
  2. Running dotnet workload restore makes the content of globaljsonworkloadsets.json the following:
{
  "<path-to-global-json>": "9.0.303.1"
}
  1. Changing workloadVersion in global.json to 9.0.304.
  2. Running dotnet workload restore outputs this warning:
Warning: Workload garbage collection failed with error: '}' is invalid after a single JSON value. Expected end of data. Path: $ | LineNumber: 3 | BytePositionInLine: 0.

and makes the content of globaljsonworkloadsets.json the following:

{
  "<path-to-global-json>": "9.0.304"
}
}

The new version number is 2 characters shorter, so 2 characters from the old content of the file remains.
This is not a valid JSON and causes any subsequent dotnet workload restore command to fail with error:

Workload update failed: '}' is invalid after a single JSON value. Expected end of data. Path: $ | LineNumber: 3 | BytePositionInLine: 0.

Fix

This fix sets the stream length to 0, truncating the stream, so there will be no remainder of the old content.
Also, as per SetLength documentation: if the current position is greater than the new length, the current position is moved to the last byte of the stream, so Seek is unnecessary.

@angyanmark angyanmark changed the title Fix updating 'globaljsonworkloadsets.json' Fix updating globaljsonworkloadsets.json Aug 19, 2025
@beeradmoore

Copy link
Copy Markdown

I had just discovered this issue with my MAUI workloads and thought I was bumping the MAUI issue when I discovered the broken file here. Turns out it was @Redth repo (tagging to see if we can get some eyes on this) and no the MAUI one. Looking for a MAUI issue I came across this one.

It is funny that this is what the bug is, because that is a thing that came up recently with image croppers across different platforms writing new data but not truncating the file leaving the old data recoverable.

@beeradmoore

Copy link
Copy Markdown

Hey @baronfel, unsure if you are the right person to tag of not. Would it be possible to get some eyes on this issue?

I think this has mostly flown under the radar because string versions are currently xx.x.xxx. Sometimes MAUI is dropping larger workset string lengths like 10.0.301.1. This bug is regularly breaking our CI/CD and dev machines whenever we build from a branch that is on a shorter workset length. It will also appear when MAUI updates to a workset to 10.0.302. I believe that change will hit all MAUI users and I recommend this can be resolved before MAUI workload updates and prevents every user updating. (cc. @davidortinau)

I am unsure if the PR is the exact fix or if it needs to be fixed elsewhere as I am unsure how to test the SDK. Fun fact: this is the same type of bug that led to Android (and other platforms) leaking data from cropped images because they were not truncating the file before writing.

@github-actions

Copy link
Copy Markdown
Contributor

Due to lack of recent activity, this PR has been labeled as 'Stale'. It will be closed if no further activity occurs within 7 more days. Any new comment will remove the label.

@github-actions github-actions Bot added the stale label Sep 11, 2026
@beeradmoore

Copy link
Copy Markdown

This is still broken and bringing down CI/CD every now and then. I don't know if the above fix has been tested.

@github-actions github-actions Bot removed the stale label Sep 11, 2026
@baronfel
baronfel merged commit d9fb8db into dotnet:main Oct 6, 2026
24 checks passed
@baronfel

baronfel commented Oct 6, 2026

Copy link
Copy Markdown
Member

/backport to release/11.0.2xx

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0.2xx (link to workflow run)

@baronfel

baronfel commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thank you for this fix and analysis!

@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

@baronfel backporting to release/11.0.2xx failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Fix updating 'globaljsonworkloadsets.json'
Using index info to reconstruct a base tree...
M	src/Cli/dotnet/Commands/Workload/GlobalJsonWorkloadSetFile.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Cli/dotnet/Commands/Workload/GlobalJsonWorkloadSetFile.cs
Applying: Remove unnecessary Seek
Using index info to reconstruct a base tree...
M	src/Cli/dotnet/Commands/Workload/GlobalJsonWorkloadSetFile.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Cli/dotnet/Commands/Workload/GlobalJsonWorkloadSetFile.cs
CONFLICT (content): Merge conflict in src/Cli/dotnet/Commands/Workload/GlobalJsonWorkloadSetFile.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Remove unnecessary Seek
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@UniQP

UniQP commented Oct 19, 2026

Copy link
Copy Markdown

@baronfel Would it be possible to backport this to the SDK 10.0.x as well?

@beeradmoore

Copy link
Copy Markdown

This issue is killing me this week. Trying to update MAUI projects to Xcode 27.0/27.1 on a different branch but:

  • Android is on workload version 10.0.402
  • iOS is on workload 10.0.401.1 (Xcode 27.0) or 10.0.402.1 (Xcode 27.1)

@beeradmoore

Copy link
Copy Markdown

For anyone else finding this thread I'll leave this extra bit of info I have been fighting with.

If you are on macOS and you are trying out dotnetup then you should not touch /usr/local/share/dotnet/metadata/workloads/{arch}/{version}/InstallState/globaljsonworkloadsets.json as the broken file is now at /Users/{username}/Library/Application Support/dotnetup/dotnet/metadata/workloads/{arch}/{version}/InstallState/globaljsonworkloadsets.json.

@beeradmoore

Copy link
Copy Markdown

Another nugget for those who have CI/CD. I added this to my pipelines/githubactions file. For us this is where our globaljsonworkloadsets.json is. Your milage may vary.

    - pwsh: |
        # Find globaljsonworkloadsets.json 
        $globalJsonWorkloadSets = "$env:DOTNET_ROOT/metadata/workloads/Arm64/10.0.400/InstallState/globaljsonworkloadsets.json"
        
        # If it exists load the json content 
        if (Test-Path -LiteralPath $globalJsonWorkloadSets -PathType Leaf) {
          $globalJsonWorkloadSetsContent = Get-Content -LiteralPath $globalJsonWorkloadSets -Raw

          # Count the number of } 
          $closingBraceCount = [regex]::Matches($globalJsonWorkloadSetsContent, '\}').Count

          # If there is more than 1 delete everything after that 1 and save the file.
          if ($closingBraceCount -gt 1) {
              $firstClosingBrace = $globalJsonWorkloadSetsContent.IndexOf('}')
              [System.IO.File]::WriteAllText($globalJsonWorkloadSets, $globalJsonWorkloadSetsContent.Substring(0, $firstClosingBrace + 1))
          }
        }
      displayName: 'Repair globaljsonworkloadsets.json'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants