Skip to content

Conversation

@angyanmark
Copy link

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 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.

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
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.

1 participant