Skip to content

Commit c14d33a

Browse files
Copilotkraenhansen
andcommitted
Refactor tier 3 target validation: move logic to ensureInstalledTargets and simplify getInstalledTargets
Co-authored-by: kraenhansen <1243959+kraenhansen@users.noreply.github.com>
1 parent abcbd29 commit c14d33a

File tree

2 files changed

+42
-61
lines changed

2 files changed

+42
-61
lines changed

packages/ferric/src/rustup.ts

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@ 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;
5+
export function getInstalledTargets() {
6+
try {
7+
return new Set(
8+
cp
9+
.execFileSync("rustup", ["target", "list", "--installed"], {
10+
encoding: "utf-8",
11+
})
12+
.split("\n")
13+
.filter((line) => line.trim() !== ""),
14+
);
15+
} catch (error) {
16+
throw new UsageError(
17+
"You need a Rust toolchain: https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo",
18+
{ cause: error },
19+
);
20+
}
21+
}
1222

1323
/**
14-
* Check if nightly Rust with rust-src component is available for build-std
24+
* Check if build-std prerequisites are available for tier 3 targets
1525
*/
16-
function isBuildStdAvailable(): boolean {
26+
export function isBuildStdAvailable(): boolean {
1727
try {
1828
// Check if nightly toolchain is available
1929
const toolchains = cp.execFileSync("rustup", ["toolchain", "list"], {
@@ -34,30 +44,3 @@ function isBuildStdAvailable(): boolean {
3444
return false;
3545
}
3646
}
37-
38-
export function getInstalledTargets() {
39-
try {
40-
const installedTargets = new Set(
41-
cp
42-
.execFileSync("rustup", ["target", "list", "--installed"], {
43-
encoding: "utf-8",
44-
})
45-
.split("\n")
46-
.filter((line) => line.trim() !== ""),
47-
);
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;
57-
} catch (error) {
58-
throw new UsageError(
59-
"You need a Rust toolchain: https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo",
60-
{ cause: error },
61-
);
62-
}
63-
}

packages/ferric/src/targets.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { chalk, UsageError, assertFixable } from "@react-native-node-api/cli-utils";
2-
import { getInstalledTargets } from "./rustup.js";
2+
import { getInstalledTargets, isBuildStdAvailable } from "./rustup.js";
33

44
export const ANDROID_TARGETS = [
55
"aarch64-linux-android",
@@ -69,7 +69,7 @@ export function isTier3Target(target: TargetName): boolean {
6969
}
7070

7171
/**
72-
* Ensure the targets are installed into the Rust toolchain
72+
* Ensure the targets are available for building
7373
* We do this up-front because the error message and fix is very unclear from the failure when missing.
7474
*/
7575
export function ensureInstalledTargets(expectedTargets: Set<TargetName>) {
@@ -93,29 +93,27 @@ export function ensureInstalledTargets(expectedTargets: Set<TargetName>) {
9393
);
9494

9595
// Handle tier 3 targets that require build-std setup
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:
106-
`To set up support for these targets:\n\n` +
107-
`1. Install nightly Rust with the rust-src component:\n` +
108-
` ${chalk.italic("rustup toolchain install nightly --component rust-src")}\n\n` +
109-
`2. Configure Cargo to use build-std by creating a .cargo/config.toml file:\n` +
110-
` ${chalk.italic("[unstable]")}\n` +
111-
` ${chalk.italic('build-std = ["std", "panic_abort"]')}\n\n` +
112-
`3. Set your default toolchain to nightly:\n` +
113-
` ${chalk.italic("rustup default nightly")}\n\n` +
114-
`For more information, see:\n` +
115-
`- Rust Platform Support: ${chalk.italic("https://doc.rust-lang.org/rustc/platform-support.html")}\n` +
116-
`- Cargo build-std: ${chalk.italic("https://doc.rust-lang.org/cargo/reference/unstable.html#build-std")}`,
117-
},
118-
);
96+
if (tier3Targets.size > 0) {
97+
// For tier 3 targets, check if build-std prerequisites are met
98+
assertFixable(
99+
isBuildStdAvailable(),
100+
`You're using tier 3 targets (${[...tier3Targets].join(", ")}) that require building the standard library from source`,
101+
{
102+
instructions:
103+
`To set up support for these targets:\n\n` +
104+
`1. Install nightly Rust with the rust-src component:\n` +
105+
` ${chalk.italic("rustup toolchain install nightly --component rust-src")}\n\n` +
106+
`2. Configure Cargo to use build-std by creating a .cargo/config.toml file:\n` +
107+
` ${chalk.italic("[unstable]")}\n` +
108+
` ${chalk.italic('build-std = ["std", "panic_abort"]')}\n\n` +
109+
`3. Set your default toolchain to nightly:\n` +
110+
` ${chalk.italic("rustup default nightly")}\n\n` +
111+
`For more information, see:\n` +
112+
`- Rust Platform Support: ${chalk.italic("https://doc.rust-lang.org/rustc/platform-support.html")}\n` +
113+
`- Cargo build-std: ${chalk.italic("https://doc.rust-lang.org/cargo/reference/unstable.html#build-std")}`,
114+
},
115+
);
116+
}
119117
}
120118

121119
export function isAndroidTarget(

0 commit comments

Comments
 (0)