Skip to content

fix(build): guard BuildTarget.VisionOS with UNITY_2023_2_OR_NEWER#1063

Merged
Scriptwonder merged 1 commit intoCoplayDev:betafrom
sMartz1:fix/visionos-buildtarget-guard
Apr 15, 2026
Merged

fix(build): guard BuildTarget.VisionOS with UNITY_2023_2_OR_NEWER#1063
Scriptwonder merged 1 commit intoCoplayDev:betafrom
sMartz1:fix/visionos-buildtarget-guard

Conversation

@sMartz1
Copy link
Copy Markdown
Contributor

@sMartz1 sMartz1 commented Apr 15, 2026

BuildTarget.VisionOS does not exist in Unity 2022.3.0 - 2022.3.17 or 2023.1.x, so UNITY_2022_3_OR_NEWER causes CS0117 compile errors on those versions. Tighten the guard to UNITY_2023_2_OR_NEWER, where the enum is guaranteed to exist.

Description

Type of Change

Save your change type

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • Test update

Changes Made

Testing/Screenshots/Recordings

Documentation Updates

  • I have added/removed/modified tools or resources
  • If yes, I have updated all documentation files using:
    • The LLM prompt at tools/UPDATE_DOCS_PROMPT.md (recommended)
    • Manual updates following the guide at tools/UPDATE_DOCS.md

Related Issues

Additional Notes

Summary by Sourcery

Bug Fixes:

  • Prevent compilation errors on Unity 2022.3.x and 2023.1.x by guarding VisionOS mappings with UNITY_2023_2_OR_NEWER instead of UNITY_2022_3_OR_NEWER.

Summary by CodeRabbit

  • Chores
    • Updated build system compatibility: VisionOS support now requires Unity 2023.2 or newer (previously 2022.3 or newer).

BuildTarget.VisionOS does not exist in Unity 2022.3.0 - 2022.3.17 or
2023.1.x, so UNITY_2022_3_OR_NEWER causes CS0117 compile errors on
those versions. Tighten the guard to UNITY_2023_2_OR_NEWER, where the
enum is guaranteed to exist.
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 15, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Tightens the preprocessor guard around VisionOS build target handling so that BuildTarget.VisionOS and its BuildTargetGroup are only referenced on Unity 2023.2 or newer, preventing compile errors on earlier Unity versions.

Class diagram for BuildTargetMapping with VisionOS conditional mappings

classDiagram
    class BuildTargetMapping {
        +bool TryResolveBuildTarget(string name, out BuildTarget target)
        +BuildTargetGroup GetTargetGroup(BuildTarget target)
    }

    class BuildTarget {
        <<enumeration>>
        Android
        iOS
        WebGL
        WSAPlayer
        tvOS
        VisionOS
    }

    class BuildTargetGroup {
        <<enumeration>>
        Android
        iOS
        WebGL
        WSA
        tvOS
        VisionOS
        Unknown
    }

    class UNITY_2023_2_OR_NEWER {
        <<compile_symbol>>
    }

    BuildTargetMapping ..> BuildTarget : uses
    BuildTargetMapping ..> BuildTargetGroup : uses
    BuildTargetMapping ..> UNITY_2023_2_OR_NEWER : conditional_VisionOS_cases
Loading

Flow diagram for VisionOS build target resolution with UNITY_2023_2_OR_NEWER guard

flowchart TD
    A[Input build target name] --> B[TryResolveBuildTarget]
    B --> C{Normalize name}
    C --> D{name == android}
    D -->|yes| E[return BuildTarget.Android]
    D -->|no| F{name == ios}
    F -->|yes| G[return BuildTarget.iOS]
    F -->|no| H{name == webgl}
    H -->|yes| I[return BuildTarget.WebGL]
    H -->|no| J{name == uwp}
    J -->|yes| K[return BuildTarget.WSAPlayer]
    J -->|no| L{name == tvos}
    L -->|yes| M[return BuildTarget.tvOS]
    L -->|no| N{name == visionos}

    subgraph VisionOS_handling_compile_guard
        direction TB
        O[Compile time check UNITY_2023_2_OR_NEWER]
        O --> P{UNITY_2023_2_OR_NEWER defined?}
        P -->|yes| Q[Include case visionos mapping
visionos -> BuildTarget.VisionOS]
        P -->|no| R[Exclude visionos case
visionos name falls through to default]
    end

    N --> O

    Q --> S[return BuildTarget.VisionOS]
    R --> T[default: return false]

    M --> U[return true]
    K --> U
    I --> U
    G --> U
    E --> U
Loading

Flow diagram for BuildTarget to BuildTargetGroup mapping with UNITY_2023_2_OR_NEWER guard

flowchart TD
    A[Input BuildTarget] --> B[GetTargetGroup]
    B --> C{target == BuildTarget.Android}
    C -->|yes| D[return BuildTargetGroup.Android]
    C -->|no| E{target == BuildTarget.iOS}
    E -->|yes| F[return BuildTargetGroup.iOS]
    E -->|no| G{target == BuildTarget.WebGL}
    G -->|yes| H[return BuildTargetGroup.WebGL]
    G -->|no| I{target == BuildTarget.WSAPlayer}
    I -->|yes| J[return BuildTargetGroup.WSA]
    I -->|no| K{target == BuildTarget.tvOS}
    K -->|yes| L[return BuildTargetGroup.tvOS]
    K -->|no| M{target == BuildTarget.VisionOS}

    subgraph VisionOS_group_compile_guard
        direction TB
        N[Compile time check UNITY_2023_2_OR_NEWER]
        N --> O{UNITY_2023_2_OR_NEWER defined?}
        O -->|yes| P[Include case BuildTarget.VisionOS
-> BuildTargetGroup.VisionOS]
        O -->|no| Q[Exclude VisionOS case
BuildTarget.VisionOS not referenced]
    end

    M --> N

    P --> R[return BuildTargetGroup.VisionOS]
    Q --> S[default: return BuildTargetGroup.Unknown]

    L --> T[return BuildTargetGroup.tvOS]
    J --> T
    H --> T
    F --> T
    D --> T
    M --> S
Loading

File-Level Changes

Change Details Files
Restrict VisionOS build target handling to Unity 2023.2+ to avoid CS0117 on earlier Unity versions.
  • Update the compile-time guard for mapping the "visionos" string to BuildTarget.VisionOS to use UNITY_2023_2_OR_NEWER.
  • Update the compile-time guard for mapping BuildTarget.VisionOS to BuildTargetGroup.VisionOS to use UNITY_2023_2_OR_NEWER.
  • Clarify the inline comment describing which Unity versions define BuildTarget.VisionOS.
MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 15, 2026

📝 Walkthrough

Walkthrough

BuildTargetMapping.cs was updated to conditionally compile VisionOS build-target mappings only when UNITY_2023_2_OR_NEWER is defined, replacing the prior UNITY_2022_3_OR_NEWER guard in two methods that handle build target resolution and grouping.

Changes

Cohort / File(s) Summary
VisionOS Build Target Compatibility
MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs
Updated preprocessor directives from UNITY_2022_3_OR_NEWER to UNITY_2023_2_OR_NEWER for VisionOS build target mapping in TryResolveBuildTarget and GetTargetGroup methods.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 VisionOS maps to twenty-twenty-three,
A version bump for compatibility!
From twenty-twenty-two we hop along,
Unity builds grow stronger, cleaner, strong.
Preprocessor guards keep the path so clear,
Building visions bright throughout the year! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: updating the compiler guard for BuildTarget.VisionOS from UNITY_2022_3_OR_NEWER to UNITY_2023_2_OR_NEWER.
Description check ✅ Passed PR description clearly explains the bug and rationale, but several template sections are incomplete (Changes Made, Testing, Related Issues are missing).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • The comment says BuildTarget.VisionOS exists in late 2022.3 patches but the guard is UNITY_2023_2_OR_NEWER, so either the comment or the conditional should be updated to match the actual version behavior (or use a more precise define for those 2022.3 patches if available).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The comment says BuildTarget.VisionOS exists in late 2022.3 patches but the guard is UNITY_2023_2_OR_NEWER, so either the comment or the conditional should be updated to match the actual version behavior (or use a more precise define for those 2022.3 patches if available).

## Individual Comments

### Comment 1
<location path="MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs" line_range="27-28" />
<code_context>
                 case "tvos": target = BuildTarget.tvOS; return true;
-                // VisionOS requires a late 2022.3 patch or Unity 6+; guard broadly
-#if UNITY_2022_3_OR_NEWER
+                // BuildTarget.VisionOS exists only in Unity 2023.2+ and late 2022.3 patches
+#if UNITY_2023_2_OR_NEWER
                 case "visionos": target = BuildTarget.VisionOS; return true;
 #endif
</code_context>
<issue_to_address>
**issue (bug_risk):** Preprocessor guard no longer matches the comment or late 2022.3 VisionOS availability.

The comment and guard now disagree: the code says VisionOS exists in 2023.2+ and late 2022.3, but `#if UNITY_2023_2_OR_NEWER` excludes all 2022.3.x, including late patches that define `BuildTarget.VisionOS`, breaking VisionOS builds there. Either expand the guard to include the appropriate 2022.3 patch symbols (e.g. `UNITY_2022_3_14_OR_NEWER`) or change the comment to state that only 2023.2+ is supported.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +27 to +28
// BuildTarget.VisionOS exists only in Unity 2023.2+ and late 2022.3 patches
#if UNITY_2023_2_OR_NEWER
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.

issue (bug_risk): Preprocessor guard no longer matches the comment or late 2022.3 VisionOS availability.

The comment and guard now disagree: the code says VisionOS exists in 2023.2+ and late 2022.3, but #if UNITY_2023_2_OR_NEWER excludes all 2022.3.x, including late patches that define BuildTarget.VisionOS, breaking VisionOS builds there. Either expand the guard to include the appropriate 2022.3 patch symbols (e.g. UNITY_2022_3_14_OR_NEWER) or change the comment to state that only 2023.2+ is supported.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs (1)

70-70: ⚠️ Potential issue | 🟡 Minor

Make the error message version-aware to match the conditional visionOS support.

Line 70's error message unconditionally lists visionos as a valid target, but the code only recognizes it on Unity 2023.2 and newer (lines 28–30 gate the handler with #if UNITY_2023_2_OR_NEWER). This creates a misleading error for users on earlier versions where the string will not resolve.

The proposed fix correctly gates the error message message to match the available targets:

Proposed fix
-                return $"Unknown build target: '{name}'. Valid targets: windows64, osx, linux64, android, ios, webgl, uwp, tvos, visionos";
+                return $"Unknown build target: '{name}'. Valid targets: windows64, osx, linux64, android, ios, webgl, uwp, tvos"
+#if UNITY_2023_2_OR_NEWER
+                    + ", visionos"
+#endif
+                    + ".";
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs` at line 70, The error
string in BuildTargetMapping (the return on the Unknown build target path)
unconditionally lists "visionos" even though support is gated by `#if`
UNITY_2023_2_OR_NEWER; update the message to be version-aware by emitting two
variants: one that includes "visionos" inside a UNITY_2023_2_OR_NEWER
conditional and a fallback without it otherwise. Modify the same return
statement in the method in class BuildTargetMapping so the valid-targets list
matches the compile-time UNITY_2023_2_OR_NEWER guard that surrounds the visionOS
handler.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs`:
- Around line 27-29: Update the VisionOS preprocessor guards and related
messaging: change the comment near TryResolveBuildTarget to reflect that
VisionOS was introduced in Unity 2022.3, and replace both occurrences of `#if`
UNITY_2023_2_OR_NEWER (the one in TryResolveBuildTarget that sets
BuildTarget.VisionOS and the one in GetTargetGroup that maps
BuildTargetGroup.VisionOS) with `#if` UNITY_2022_3_OR_NEWER so resolution and
mapping are symmetric on 2022.3.x; additionally make the error/log message
emitted near the BuildTarget/target-group mismatch conditional so it doesn't
reference compilation-only symbols on older Unity versions (wrap or adjust the
message so it only runs when the corresponding preprocessor symbol is defined).

---

Outside diff comments:
In `@MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs`:
- Line 70: The error string in BuildTargetMapping (the return on the Unknown
build target path) unconditionally lists "visionos" even though support is gated
by `#if` UNITY_2023_2_OR_NEWER; update the message to be version-aware by emitting
two variants: one that includes "visionos" inside a UNITY_2023_2_OR_NEWER
conditional and a fallback without it otherwise. Modify the same return
statement in the method in class BuildTargetMapping so the valid-targets list
matches the compile-time UNITY_2023_2_OR_NEWER guard that surrounds the visionOS
handler.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 15feb138-e99b-4a26-afe1-601590013a88

📥 Commits

Reviewing files that changed from the base of the PR and between 8123820 and dcd4963.

📒 Files selected for processing (1)
  • MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs

Comment on lines +27 to 29
// BuildTarget.VisionOS exists only in Unity 2023.2+ and late 2022.3 patches
#if UNITY_2023_2_OR_NEWER
case "visionos": target = BuildTarget.VisionOS; return true;
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.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

Which exact Unity editor versions introduced BuildTarget.VisionOS and BuildTargetGroup.VisionOS, and do patch-level symbols like UNITY_2022_3_18_OR_NEWER exist?

💡 Result:

BuildTarget.VisionOS and BuildTargetGroup.VisionOS were introduced in Unity 2022.3, with full visionOS support and the required visionOS Build Support module available starting from Unity 2022.3.5f1. Official PolySpatial packages for visionOS require Unity 2022.3.18f1 or newer. These enums are documented in the 2022.3 Scripting API reference, confirming their presence there, and also in Unity 6 (6000.0). Earlier versions like 2022.3.12 had partial or experimental support with build issues. Yes, Unity defines patch-level preprocessor symbols in the format UNITY_X_Y_Z for exact versions (e.g., UNITY_2022_3_18 for 2022.3.18) and UNITY_X_Y_OR_NEWER (e.g., UNITY_2022_3_OR_NEWER, UNITY_2022_3_18_OR_NEWER) for version checks. Additionally, UNITY_VISIONOS is defined when building for visionOS.

Citations:


🏁 Script executed:

# First, confirm file exists and check its content
cat -n MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs | head -70

Repository: CoplayDev/unity-mcp

Length of output: 3674


VisionOS guard is overly restrictive, causing asymmetry between resolution and mapping.

TryResolveBuildTarget uses #if UNITY_2023_2_OR_NEWER (line 28), while the unguarded fallback System.Enum.TryParse at line 32 will succeed on Unity 2022.3.5+ where BuildTarget.VisionOS actually exists. Meanwhile, GetTargetGroup (line 53) only maps VisionOS behind the same 2023.2 guard. On Unity 2022.3.5–2022.3.x, this causes TryResolveBuildTarget("visionos") to return a valid target that GetTargetGroup maps to Unknown.

The web search confirms BuildTarget.VisionOS and BuildTargetGroup.VisionOS were introduced in Unity 2022.3, not 2023.2. The guards should be #if UNITY_2022_3_OR_NEWER to match the actual enum availability.

Update both guards to UNITY_2022_3_OR_NEWER (lines 28 and 53), and correct the comment at line 27. Additionally, the error message at line 70 should be conditional if supporting versions < 2023.2 where the preprocessor guard blocks compilation.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs` around lines 27 - 29,
Update the VisionOS preprocessor guards and related messaging: change the
comment near TryResolveBuildTarget to reflect that VisionOS was introduced in
Unity 2022.3, and replace both occurrences of `#if` UNITY_2023_2_OR_NEWER (the one
in TryResolveBuildTarget that sets BuildTarget.VisionOS and the one in
GetTargetGroup that maps BuildTargetGroup.VisionOS) with `#if`
UNITY_2022_3_OR_NEWER so resolution and mapping are symmetric on 2022.3.x;
additionally make the error/log message emitted near the
BuildTarget/target-group mismatch conditional so it doesn't reference
compilation-only symbols on older Unity versions (wrap or adjust the message so
it only runs when the corresponding preprocessor symbol is defined).

@Scriptwonder Scriptwonder merged commit b51ad49 into CoplayDev:beta Apr 15, 2026
2 checks passed
@thangvu207
Copy link
Copy Markdown

I use unity 2023..2.20 but this issue is not fixed. I must comment this line: case "visionos": target = BuildTarget.VisionOS; return true;

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants