Repository navigation
test(rust): derive toolchain homes from the rustup proxy and report them on failure #8458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { existsSync, readFileSync, realpathSync } from "node:fs"; | ||
| import { userInfo } from "node:os"; | ||
| import { basename, dirname, join } from "node:path"; | ||
|
|
||
| /** | ||
| * Rust toolchain environment for tests that build or run native fixtures. | ||
| * | ||
| * The preload sandboxes HOME/USERPROFILE, and rustup finds its toolchains from RUSTUP_HOME, which | ||
| * hosted runners leave unset. Guessing the home from OCX_REAL_HOME or userInfo() has failed on | ||
| * Windows ("rustup could not choose a version of cargo ... no default is configured"; batches | ||
| * 42499262970, 42580170093, 42672304851). The rustup proxy's own location names the installation | ||
| * it belongs to, so each home that is not set explicitly is derived from it; the real-home guess | ||
| * remains only for a binary that is not inside a .cargo/bin. | ||
| */ | ||
| export function rustToolchainEnv(tool: "cargo" | "rustc"): { bin: string | null; env: Record<string, string | undefined>; describe: () => string } { | ||
| const exeSuffix = process.platform === "win32" ? ".exe" : ""; | ||
| const realHome = process.env.OCX_REAL_HOME?.trim() || userInfo().homedir; | ||
| const found = Bun.which(tool) ?? join(realHome, ".cargo", "bin", `${tool}${exeSuffix}`); | ||
| const bin = existsSync(found) ? found : null; | ||
| let cargoHome = process.env.CARGO_HOME || join(realHome, ".cargo"); | ||
| let rustupHome = process.env.RUSTUP_HOME || join(realHome, ".rustup"); | ||
| if (bin) { | ||
| let resolved = bin; | ||
| try { resolved = realpathSync(bin); } catch { /* keep the PATH spelling */ } | ||
| const binDir = dirname(resolved); | ||
| // rustup installs lowercase .cargo/bin; Windows paths compare case-insensitively. | ||
| const name = (path: string) => process.platform === "win32" ? basename(path).toLowerCase() : basename(path); | ||
| if (name(binDir) === "bin" && name(dirname(binDir)) === ".cargo") { | ||
| const derivedCargoHome = dirname(binDir); | ||
| if (!process.env.CARGO_HOME) cargoHome = derivedCargoHome; | ||
| const sibling = join(dirname(derivedCargoHome), ".rustup"); | ||
| if (!process.env.RUSTUP_HOME && existsSync(sibling)) rustupHome = sibling; | ||
| } | ||
| } | ||
| const env = { ...process.env, CARGO_HOME: cargoHome, RUSTUP_HOME: rustupHome }; | ||
| const describe = () => { | ||
| const settings = join(rustupHome, "settings.toml"); | ||
| let defaultToolchain: string | null = null; | ||
| try { defaultToolchain = /^default_toolchain\s*=\s*"([^"]*)"/m.exec(readFileSync(settings, "utf8"))?.[1] ?? null; } catch { /* absent */ } | ||
| return JSON.stringify({ | ||
| bin, CARGO_HOME: cargoHome, RUSTUP_HOME: rustupHome, rustupSettings: existsSync(settings), defaultToolchain, | ||
| OCX_REAL_HOME: process.env.OCX_REAL_HOME ?? null, USERPROFILE: process.env.USERPROFILE ?? null, HOME: process.env.HOME ?? null, | ||
| }); | ||
| }; | ||
| return { bin, env, describe }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the proxy path before resolving its symlink.
If
.cargo/bin/rustcor.cargo/bin/cargois a symlink to a binary outside.cargo/bin,realpathSyncat Line 24 removes the proxy’s directory fromresolved. This condition then rejects the proxy path. When the proxy’s sibling.rustupis the installed home andOCX_REAL_HOMEpoints elsewhere, the spawned Rust tool receives the wrongRUSTUP_HOMEand can fail to select its toolchain. Checkbinfor the.cargo/binlayout first. Use the resolved path only when the proxy path does not match. Rustup supports symlinked proxies. (rust-lang.github.io)