Skip to content

Commit 543c337

Browse files
authored
feat: add tests and docs for configurable toolchain overrides (#89)
* `4.0.0.beta.3` * clean up readme and `.gitignore` * fix center align * update readme * add docs and clean up logic for toolchain override * mv root toolchain file
1 parent f792abb commit 543c337

17 files changed

+1035
-40
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@ target/
1010
dist/
1111
node_modules/
1212
.vercel/
13-
14-
fixtures/**/vercel.json

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ exclude = [
1212
"test/fixtures/02-with-utility",
1313
"test/fixtures/03-with-function",
1414
"test/fixtures/04-with-parameter",
15-
"test/fixtures/05-with-similar-entrypaths"
15+
"test/fixtures/05-with-similar-entrypaths",
16+
"test/fixtures/06-with-toolchain-override"
1617
]

README.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
<p align="center">
2-
<a href="https://vercel.com">
2+
<a align="center" href="https://vercel.com">
33
<img src="https://assets.vercel.com/image/upload/v1588805858/repositories/vercel/logo.png" height="96">
44
<h3 align="center">Rust</h3>
55
</a>
6-
<p align="center">Rust runtime for Vercel Functions.</p>
6+
<p align="center">Rust Runtime for Vercel Functions.</p>
77
</p>
88

9+
<div align="center">
10+
11+
<a href="https://www.npmjs.com/package/vercel-rust">![npm version](https://img.shields.io/npm/v/vercel-rust.svg)</a>
12+
<a href="https://www.npmjs.com/package/vercel-rust">![npm downloads](https://img.shields.io/npm/dm/vercel-rust.svg?label=npm%20downloads)</a>
13+
<a href="https://crates.io/crates/vercel_runtime">![crates.io downloads](https://img.shields.io/crates/d/vercel_runtime?color=yellow&label=crates.io)</a>
14+
915
Community-maintained package to support using [Rust](https://www.rust-lang.org/) inside [Vercel Functions](https://vercel.com/docs/serverless-functions/introduction) as a [Runtime](https://vercel.com/docs/runtimes).
1016

17+
</div>
18+
1119
## Legacy Runtime
1220

1321
The below documentation is for the `vercel_runtime` crate (in beta). If you are looking for the legacy runtime instructions using `vercel_lambda` see [tree/a9495a0](https://github.com/vercel-community/rust/tree/a9495a0f0d882a36ea165f1629fcc79c30bc3108).
@@ -59,7 +67,8 @@ Finally we need a `Cargo.toml` file at the root of your repository.
5967
# --snip--
6068

6169
[dependencies]
62-
# --snip--
70+
tokio = { version = "1", features = ["macros"] }
71+
serde_json = { version = "1", features = ["raw_value"] }
6372
# Documentation: https://docs.rs/vercel_runtime/latest/vercel_runtime
6473
vercel_runtime = { version = "0.2.1" }
6574

@@ -72,7 +81,7 @@ vercel_runtime = { version = "0.2.1" }
7281
name = "handler"
7382
path = "api/handler.rs"
7483

75-
# Note that you need to provide unique names for dynamic paths
84+
# Note that you need to provide unique names for each binary
7685
[[bin]]
7786
name = "user-id"
7887
path = "api/user/[id].rs"
@@ -81,7 +90,6 @@ path = "api/user/[id].rs"
8190
name = "group-id"
8291
path = "api/group/[id].rs"
8392

84-
8593
# --snip--
8694
```
8795

@@ -99,13 +107,12 @@ During local development with `vercel dev`, ensure `rust` and `cargo` are alread
99107

100108
## Contributing
101109

102-
Since this project contains both Rust and Node.js code, you need to install the relevant dependencies. If you're only working on the JavaScript side, you only need to install those dependencies (and vice-versa).
110+
Since this project contains both Rust and Node.js code, you need to install the relevant dependencies. If you're only working on the TypeScript side, you only need to install those dependencies (and vice-versa).
103111

104112
```shell
105113
# install node dependencies
106114
pnpm install
107115

108-
109116
# install cargo dependencies
110117
cargo fetch
111118
```
@@ -121,6 +128,13 @@ graph TD
121128

122129
## FAQ
123130

131+
<details>
132+
<summary>How to specify toolchain overrides</summary>
133+
134+
An example on how this can be achieved is using a `rust-toolchain` file adjacent to your `Cargo.toml`. Please refer to [Rust Documentation](https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file) for more details.
135+
136+
</details>
137+
124138
<details>
125139
<summary>Can I use musl/static linking?</summary>
126140

rust-toolchain.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[toolchain]
2+
channel = "nightly"

src/lib/rust-toolchain.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { debug } from '@vercel/build-utils';
22
import execa from 'execa';
33

4-
async function downloadRustToolchain({
5-
version = 'stable',
6-
}: {
7-
version?: string;
8-
}): Promise<void> {
4+
async function downloadRustToolchain(): Promise<void> {
95
try {
106
await execa(
11-
`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${version}`,
7+
`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y`,
128
[],
139
{ shell: true, stdio: 'inherit' },
1410
);
@@ -21,11 +17,11 @@ async function downloadRustToolchain({
2117
}
2218
}
2319

24-
export const installRustToolchain = async (version?: string): Promise<void> => {
20+
export const installRustToolchain = async (): Promise<void> => {
2521
try {
2622
await execa(`rustup -V`, [], { shell: true, stdio: 'ignore' });
2723
debug('Rust Toolchain is already installed, skipping download');
2824
} catch (err) {
29-
await downloadRustToolchain({ version });
25+
await downloadRustToolchain();
3026
}
3127
};

test/fixtures.test.ts

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import { join } from 'node:path';
22
import ms from 'ms';
33
import execa from 'execa';
4-
import { writeJSON } from 'fs-extra';
54
import fetch from 'node-fetch';
65

7-
const vercelFileName = 'test.vercel.json';
8-
const pkgRootFile = `file:${process.cwd()}`;
96
const readyRegex = /Ready!\s+Available at (?<url>https?:\/\/\w+:\d+)/;
107

118
jest.setTimeout(ms('50m'));
129

13-
interface Config {
14-
builds: {
15-
use: string;
16-
}[];
17-
}
18-
1910
interface Probe {
2011
path: string;
2112
status?: number;
@@ -30,19 +21,6 @@ async function importJSON<T>(path: string): Promise<T> {
3021
return (await import(path)) as unknown as Promise<T>;
3122
}
3223

33-
async function injectConf(confPath: string): Promise<Config> {
34-
const conf = await importJSON<Config>(join(confPath, vercelFileName));
35-
36-
conf.builds[0].use = pkgRootFile;
37-
38-
await writeJSON(join(confPath, 'vercel.json'), conf, {
39-
spaces: 2,
40-
EOL: '\n',
41-
});
42-
43-
return conf;
44-
}
45-
4624
async function checkProbes(baseUrl: string, probes: Probe[]): Promise<void> {
4725
for (const probe of probes) {
4826
// eslint-disable-next-line no-await-in-loop
@@ -121,4 +99,7 @@ describe('vercel-rust', () => {
12199
it('deploy 05-with-similar-entrypaths', async () => {
122100
await expect(testFixture('05-with-similar-entrypaths')).resolves.toBe('ok');
123101
});
102+
it('deploy 06-with-toolchain-override', async () => {
103+
await expect(testFixture('06-with-toolchain-override')).resolves.toBe('ok');
104+
});
124105
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vercel
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

0 commit comments

Comments
 (0)