fix(build): guard BuildTarget.VisionOS with UNITY_2023_2_OR_NEWER#1063
Conversation
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.
Reviewer's guide (collapsed on small PRs)Reviewer's GuideTightens 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 mappingsclassDiagram
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
Flow diagram for VisionOS build target resolution with UNITY_2023_2_OR_NEWER guardflowchart 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
Flow diagram for BuildTarget to BuildTargetGroup mapping with UNITY_2023_2_OR_NEWER guardflowchart 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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
📝 WalkthroughWalkthroughBuildTargetMapping.cs was updated to conditionally compile VisionOS build-target mappings only when Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| // BuildTarget.VisionOS exists only in Unity 2023.2+ and late 2022.3 patches | ||
| #if UNITY_2023_2_OR_NEWER |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 | 🟡 MinorMake the error message version-aware to match the conditional visionOS support.
Line 70's error message unconditionally lists
visionosas 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
📒 Files selected for processing (1)
MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs
| // 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; |
There was a problem hiding this comment.
🧩 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:
- 1: https://docs.unity3d.com/2022.3/Documentation/ScriptReference/BuildTarget.VisionOS.html
- 2: https://docs.unity3d.com/2022.3/Documentation/ScriptReference/BuildTargetGroup.VisionOS.html
- 3: https://docs.unity3d.com/2022.3/Documentation/Manual/visionOS.html
- 4: https://docs.unity3d.com/Manual/scripting-symbol-reference.html
- 5: https://docs.unity3d.com/Packages/com.unity.polyspatial.visionos@2.0/manual/Requirements.html
- 6: https://docs.unity3d.com/Packages/com.unity.polyspatial.visionos@1.1/manual/Requirements.html
- 7: https://docs.unity3d.com/Packages/com.unity.polyspatial.visionos%401.1/manual/Requirements.html
- 8: https://docs.unity3d.com/Packages/com.unity.polyspatial.visionos@1.2/manual/Requirements.html
🏁 Script executed:
# First, confirm file exists and check its content
cat -n MCPForUnity/Editor/Tools/Build/BuildTargetMapping.cs | head -70Repository: 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).
|
I use unity 2023..2.20 but this issue is not fixed. I must comment this line: case "visionos": target = BuildTarget.VisionOS; return true; |
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
Changes Made
Testing/Screenshots/Recordings
Documentation Updates
tools/UPDATE_DOCS_PROMPT.md(recommended)tools/UPDATE_DOCS.mdRelated Issues
Additional Notes
Summary by Sourcery
Bug Fixes:
Summary by CodeRabbit