Skip to content

fix: Avoid global.json when running all dotnet commands#614

Merged
bruno-garcia merged 2 commits into
masterfrom
fix/any-dotnet-version
Jul 28, 2025
Merged

fix: Avoid global.json when running all dotnet commands#614
bruno-garcia merged 2 commits into
masterfrom
fix/any-dotnet-version

Conversation

@Flash0ver

@Flash0ver Flash0ver commented Jul 28, 2025

Copy link
Copy Markdown
Member

Summary

The Sentry .NET SDK uses a global.json file to pin a specific version of the .NET SDK and .NET Workloads to ensure reproducible builds.
When pushing the Sentry NuGet packages and Symbol packages, we can use any version of the .NET SDK,
as long as we ensure that the actual dotnet build, dotnet test, and creating the packages via dotnet pack run with the pinned version of the .NET SDK.

In a previous PR, see #601,
we applied spawnOptions to the invocation of dotnet nuget push via spawnProcess,
with which we set the Working Directory outside of the sentry-dotnet repository,
so that the global.json does not get picked up / enforced.

However,
we forgot to apply the same spawnOptions to the other invocations of dotnet commands.

This manifests in failure when invoking dotnet --version:

...
[info] [[target/nuget]] .NET Version:
Error:  Process "dotnet" errored with code 145
...
dotnet: 8.0.412 [/usr/share/dotnet/sdk]
dotnet: 9.0.303 [/usr/share/dotnet/sdk]
...
dotnet: Requested SDK version: 9.0.301
dotnet: global.json file: global.json
...
Publish using Craft
[info] [[target/nuget]] .NET Version:
Error:  Process "dotnet" errored with code 145

STDOUT: dotnet: 8.0.412 [/usr/share/dotnet/sdk]
dotnet: 9.0.303 [/usr/share/dotnet/sdk]
dotnet: 


STDERR:dotnet: The command could not be loaded, possibly because:
dotnet:   * You intended to execute a .NET application:
dotnet:       The application '--version' does not exist.
dotnet:   * You intended to execute a .NET SDK command:
dotnet:       A compatible .NET SDK was not found.
dotnet: 
dotnet: Requested SDK version: 9.0.301
dotnet: global.json file: /github/workspace/__repo__/global.json
dotnet: 
dotnet: Installed SDKs:
dotnet: 
dotnet: Install the [9.0.301] .NET SDK or update [/github/workspace/__repo__/global.json] to match an installed SDK.
dotnet: 
dotnet: Learn about SDK resolution:
dotnet: https://aka.ms/dotnet/sdk-not-found
dotnet: 

  
  STDOUT: dotnet: 8.0.412 [/usr/share/dotnet/sdk]
  dotnet: 9.0.303 [/usr/share/dotnet/sdk]
  dotnet:
  
  
  STDERR:dotnet: The command could not be loaded, possibly because:
  dotnet:   * You intended to execute a .NET application:
  dotnet:       The application '--version' does not exist.
  dotnet:   * You intended to execute a .NET SDK command:
  dotnet:       A compatible .NET SDK was not found.
  dotnet:
  dotnet: Requested SDK version: 9.0.301
  dotnet: global.json file: global.json
  dotnet:
  dotnet: Installed SDKs:
  dotnet:
  dotnet: Install the [9.0.301] .NET SDK or update [global.json] to match an installed SDK.
  dotnet:
  dotnet: Learn about SDK resolution:
  dotnet: https://aka.ms/dotnet/sdk-not-found
  dotnet:
  
  at Bqe (/usr/local/bin/craft:357:15452)
  at p (/usr/local/bin/craft:361:467)
  at ChildProcess.<anonymous> (/usr/local/bin/craft:361:761)
  at ChildProcess.emit (node:events:518:28)
  at ChildProcess.emit (node:domain:489:12)
  at ChildProcess._handle.onexit (node:internal/child_process:293:12)

Changes

Applying the previously added spawnOptions to the invocations of all dotnet commands,
not just dotnet nuget push.

See also

@Flash0ver Flash0ver self-assigned this Jul 28, 2025
@Flash0ver Flash0ver added the bug Something isn't working label Jul 28, 2025

@bruno-garcia bruno-garcia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks Stefan.

Comment thread src/targets/nuget.ts Outdated
* Spawn options to run outside the repository folder to avoid global.json constraints
* (we don't need specific dotnet/workload versions just to upload to nuget)
*/
const SPAWN_OPTIONS = { cwd: '/' };

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Is this the right way to do it?

Alternative:
As private readonly field on class NugetTarget.

@asottile-sentry asottile-sentry left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bruno-garcia

Copy link
Copy Markdown
Member

Thanks @asottile-sentry

@bruno-garcia bruno-garcia merged commit 8d788a8 into master Jul 28, 2025
13 checks passed
@bruno-garcia bruno-garcia deleted the fix/any-dotnet-version branch July 28, 2025 20:11
BYK pushed a commit that referenced this pull request Oct 1, 2025
* fix: apply spawnOptions to other dotnet commands

* ref: more precise name and comment
BYK pushed a commit that referenced this pull request May 22, 2026
## Summary

Fixes #819. Refs getsentry/sentry-dotnet#5250.

The automatic version bumping added in #707 invokes `dotnet setversion`
with `cwd: rootDir`, so the dotnet host reads any `global.json` in the
consumer repo. The release runner won't always have that exact SDK
installed — `dotnet` then refuses to launch and `craft prepare` aborts.

Other `dotnet` invocations in this target dodge this by spawning with
`cwd: '/'` (see #601, #614). That isn't viable here because
`dotnet-setversion` operates on files in cwd.

Instead, this PR temporarily renames `global.json` to
`global.json.craft-bak` for the duration of the call and restores it in
a `finally`. The XML-fallback path is unchanged and unaffected.

## Notes for reviewers

- An alternative workaround would be to skip `dotnet-setversion`
altogether and rely on the xml based version update logic that is
currently a fallback. Hard to say which workaround is better.
- No existing test file for `nuget.ts` (`src/targets/__tests__/` has no
`nuget.test.ts`), so this PR doesn't add one. Happy to add one if you'd
like — let me know the preferred shape.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants