vercel-community / rust

🦀 Rust runtime for ▲ Vercel Serverless Functions
https://rust-runtime.vercel.app
MIT License
845 stars 49 forks source link

example provided on the root page won't deploy #79

Closed abcd-ca closed 1 year ago

abcd-ca commented 1 year ago

Given the example on the root page, I initially couldn't run vercel dev without a build error. I commented out the [lib] section in /api/Cargo.toml and then vercel dev did work and served up "hello world" at http://localhost:3000/api/user

Then I tried to deploy it using vercel. It failed in my terminal window with,

Error: ENOENT: no such file or directory, open '/vercel/path0/api/target/release/metadata'
Error: Check your logs at https://vercel-rust2-m10f8rxrm-my-org.vercel.app/_logs or run `vercel logs vercel-rust2-m10f8rxrm-my-org.vercel.app`

And in the Vercel logs, I see,

[19:15:29.757] Running build in Cleveland, USA (East) – cle1
[19:15:29.808] Retrieving list of deployment files...
[19:15:30.060] Previous build cache not available
[19:15:31.290] Downloading 2323 deployment files...
[19:15:46.219] Running "vercel build"
[19:15:46.654] Vercel CLI 28.15.2
[19:15:47.025] > Installing Builder: vercel-rust@3.1.2
[19:15:49.476] info: downloading installer
[19:15:49.640] error: $HOME differs from euid-obtained home directory: you may be using sudo
[19:15:49.641] error: $HOME directory: /vercel
[19:15:49.641] error: euid-obtained home directory: /root
[19:15:49.658] info: profile set to 'default'
[19:15:49.658] info: default host triple is x86_64-unknown-linux-gnu
[19:15:49.658] info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
[19:15:49.754] info: latest update on 2023-01-26, rust version 1.67.0 (fc594f156 2023-01-24)
[19:15:49.754] info: downloading component 'cargo'
[19:15:49.858] info: downloading component 'clippy'
[19:15:49.892] info: downloading component 'rust-docs'
[19:15:50.069] info: downloading component 'rust-std'
[19:15:50.345] info: downloading component 'rustc'
[19:15:50.910] info: downloading component 'rustfmt'
[19:15:50.956] info: installing component 'cargo'
[19:15:51.621] info: installing component 'clippy'
[19:15:51.947] info: installing component 'rust-docs'
[19:15:54.336] info: installing component 'rust-std'
[19:15:57.088] info: installing component 'rustc'
[19:16:02.795] info: installing component 'rustfmt'
[19:16:03.253] info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
[19:16:03.253] 
[19:16:03.266]   stable-x86_64-unknown-linux-gnu installed - rustc 1.67.0 (fc594f156 2023-01-24)
[19:16:03.266] 
[19:16:03.266] 
[19:16:03.266] Rust is installed now. Great!
[19:16:03.266] 
[19:16:03.266] To get started you may need to restart your current shell.
[19:16:03.266] This would reload your PATH environment variable to include
[19:16:03.266] Cargo's bin directory ($HOME/.cargo/bin).
[19:16:03.266] 
[19:16:03.266] To configure your current shell, run:
[19:16:03.266] source "$HOME/.cargo/env"
[19:17:32.301] error[E0601]: `main` function not found in crate `metadata`
[19:17:32.302]  --> target/debug/build/lambda_runtime_core-bc27958fb497a0f6/out/metadata.rs:5:2
[19:17:32.302]   |
[19:17:32.302] 5 | }
[19:17:32.302]   |  ^ consider adding a `main` function to `target/debug/build/lambda_runtime_core-bc27958fb497a0f6/out/metadata.rs`
[19:17:32.302] 
[19:17:32.302] For more information about this error, try `rustc --explain E0601`.
[19:17:32.304] error: could not compile `index` due to previous error
[19:17:32.328] failed to `cargo build`
[19:17:32.333] Error: ENOENT: no such file or directory, open '/vercel/path0/api/target/release/metadata'
abcd-ca commented 1 year ago

I've tried with and without NextJS but no luck. Here's a repo fully set up if anyone wants to pitch in and try to get it working.

https://github.com/abcd-ca/vercel-nextjs-rust

alexevladgabriel commented 1 year ago

I think you're required to publish this files on pages/api.

abcd-ca commented 1 year ago

@alexevladgabriel with a vanilla Vercel app the docs say /api. In a NextJS app it’s /pages/api. I have tried each and both concurrently. I’ve gotten to to work locally to some degree but it won’t deploy. With NextJS locally the rust endpoints interfere with it he js api endpoints though. Vercel is now helping me. I’ll update if I have more info

abcd-ca commented 1 year ago

I got it working, turns out I just needed to update my Vercel CLI 🤦 🎉

I'm now using Vercel CLI 28.16.7 and before I was on a lower 28.x.x. I forget the Xs but major number was definitely 28.

Here's a working repo with a NextJS project:

  1. Clone it: git clone https://github.com/abcd-ca/vercel-nextjs-rust
  2. Deploy it: vercel --prod - accept all the defaults
  3. View the endpoints at the resulting url:
    • Javascript lambda: <url from deployment>/api/hellojs
    • Rust lambda: <url from deployment>/api/hellorust

To get it to work locally, there's a trick, though I've been discussing this with Vercel and a ticket has been submitted with the Vercel CLI team which I expect will streamline this:

  1. vercel dev --debug Look for a line that contains Spawning dev command: next dev --port 50434. The port number differ from this one but it shows that the NextJS API endpoints are running on a different port than the Rust API endpoints which are on port 3000 (Thanks to Vercel for this tip)
  2. View the endpoints at the resulting url:
    • Javascript lambda: http://localhost:50434/api/hellojs
    • Rust lambda: http://localhost:3000/api/hellorust