Skip to content

Commit abcbd29

Browse files
Copilotkraenhansen
andcommitted
Improve tier 3 target handling: use assertFixable and smart getInstalledTargets
Co-authored-by: kraenhansen <[email protected]>
1 parent 0bf31fb commit abcbd29

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

packages/ferric/src/rustup.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,58 @@ import cp from "node:child_process";
22

33
import { UsageError } from "@react-native-node-api/cli-utils";
44

5+
/**
6+
* Tier 3 targets that require build-std (defined here to avoid circular imports)
7+
*/
8+
const TIER_3_TARGETS = [
9+
"aarch64-apple-visionos",
10+
"aarch64-apple-visionos-sim",
11+
] as const;
12+
13+
/**
14+
* Check if nightly Rust with rust-src component is available for build-std
15+
*/
16+
function isBuildStdAvailable(): boolean {
17+
try {
18+
// Check if nightly toolchain is available
19+
const toolchains = cp.execFileSync("rustup", ["toolchain", "list"], {
20+
encoding: "utf-8",
21+
});
22+
23+
if (!toolchains.includes("nightly")) {
24+
return false;
25+
}
26+
27+
// Check if rust-src component is installed for nightly
28+
const components = cp.execFileSync("rustup", ["component", "list", "--toolchain", "nightly"], {
29+
encoding: "utf-8",
30+
});
31+
32+
return components.includes("rust-src (installed)");
33+
} catch {
34+
return false;
35+
}
36+
}
37+
538
export function getInstalledTargets() {
639
try {
7-
return new Set(
40+
const installedTargets = new Set(
841
cp
942
.execFileSync("rustup", ["target", "list", "--installed"], {
1043
encoding: "utf-8",
1144
})
12-
.split("\n"),
45+
.split("\n")
46+
.filter((line) => line.trim() !== ""),
1347
);
48+
49+
// Add tier 3 targets if build-std is properly configured
50+
if (isBuildStdAvailable()) {
51+
for (const target of TIER_3_TARGETS) {
52+
installedTargets.add(target);
53+
}
54+
}
55+
56+
return installedTargets;
1457
} catch (error) {
1558
throw new UsageError(
1659
"You need a Rust toolchain: https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo",

packages/ferric/src/targets.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chalk, UsageError } from "@react-native-node-api/cli-utils";
1+
import { chalk, UsageError, assertFixable } from "@react-native-node-api/cli-utils";
22
import { getInstalledTargets } from "./rustup.js";
33

44
export const ANDROID_TARGETS = [
@@ -65,7 +65,7 @@ export const TIER_3_TARGETS: readonly TargetName[] = [
6565
* Check if a target is a tier 3 target that requires build-std
6666
*/
6767
export function isTier3Target(target: TargetName): boolean {
68-
return TIER_3_TARGETS.includes(target);
68+
return (TIER_3_TARGETS as readonly string[]).includes(target);
6969
}
7070

7171
/**
@@ -84,20 +84,25 @@ export function ensureInstalledTargets(expectedTargets: Set<TargetName>) {
8484
]);
8585

8686
// Handle standard targets that can be installed via rustup
87-
if (missingStandardTargets.size > 0) {
88-
throw new UsageError(
89-
`You're missing ${
90-
missingStandardTargets.size
91-
} targets - to fix this, run:\n\n${chalk.italic(
92-
`rustup target add ${[...missingStandardTargets].join(" ")}`,
93-
)}`,
94-
);
95-
}
87+
assertFixable(
88+
missingStandardTargets.size === 0,
89+
`You're missing ${missingStandardTargets.size} targets`,
90+
{
91+
command: `rustup target add ${[...missingStandardTargets].join(" ")}`,
92+
},
93+
);
9694

9795
// Handle tier 3 targets that require build-std setup
98-
if (tier3Targets.size > 0) {
99-
throw new UsageError(
100-
`You're using tier 3 targets (${[...tier3Targets].join(", ")}) that require building the standard library from source.\n\n` +
96+
// Check if tier 3 targets are properly configured (included in installedTargets means they're available)
97+
const missingTier3Targets = new Set([
98+
...[...tier3Targets].filter((target) => !installedTargets.has(target)),
99+
]);
100+
101+
assertFixable(
102+
missingTier3Targets.size === 0,
103+
`You're using tier 3 targets (${[...missingTier3Targets].join(", ")}) that require building the standard library from source`,
104+
{
105+
instructions:
101106
`To set up support for these targets:\n\n` +
102107
`1. Install nightly Rust with the rust-src component:\n` +
103108
` ${chalk.italic("rustup toolchain install nightly --component rust-src")}\n\n` +
@@ -109,8 +114,8 @@ export function ensureInstalledTargets(expectedTargets: Set<TargetName>) {
109114
`For more information, see:\n` +
110115
`- Rust Platform Support: ${chalk.italic("https://doc.rust-lang.org/rustc/platform-support.html")}\n` +
111116
`- Cargo build-std: ${chalk.italic("https://doc.rust-lang.org/cargo/reference/unstable.html#build-std")}`,
112-
);
113-
}
117+
},
118+
);
114119
}
115120

116121
export function isAndroidTarget(

0 commit comments

Comments
 (0)