vercel-community / rust

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

Rewrites Not Working as Expected #121

Open rajput-hemant opened 10 months ago

rajput-hemant commented 10 months ago

It appears that rewrites are not functioning as expected. Here's my vercel.json configuration:

{
  "rewrites": [
    {
      "source": "/(.*)",
      "destination": "/api"
    }
  ],
  "functions": {
    "api/**/*.rs": {
      "runtime": "vercel-rust@4.0.6"
    }
  }
}

Additionally, when I navigate to an API route, I receive the following error when running vc dev:

❯ vc dev
Vercel CLI 32.2.0
> Ready! Available at http://localhost:3000
> Building vercel-rust@4.0.6:api/simple.rs
> Built vercel-rust@4.0.6:api/simple.rs [2s]
Error: spawn /home/rajput-hemant/.cache/com.vercel.fun/runtimes/provided/bootstrap EACCES
    at Process.ChildProcess._handle.onexit (node:internal/child_process:286:19)
    at onErrorNT (node:internal/child_process:484:16)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
Error: An unexpected error occurred!
Error: spawn /home/rajput-hemant/.cache/com.vercel.fun/runtimes/provided/bootstrap EACCES
    at Process.ChildProcess._handle.onexit (node:internal/child_process:286:19)
    at onErrorNT (node:internal/child_process:484:16)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)
rajput-hemant commented 10 months ago

I managed to rewrite the path with the following vercel.json configuration for example/simple:

{
    "rewrites": [
        { "source": "/simple", "destination": "/api/simple.rs" },
        { "source": "/complex", "destination": "/api/complex.rs" },
        { "source": "/user/:id", "destination": "/api/user/[id].rs" }
    ],
    "functions": {
        "api/**/*.rs": {
            "runtime": "vercel-rust@4.0.6"
        }
    }
}

However, this doesn't seem to work for the Experimental bundling API. Here is my vercel.json configuration, using the bundled_api macro for example/route-merge:

{
    "rewrites": [{ "source": "/(.*)", "destination": "/api/main.rs" }],
    "functions": {
        "api/main.rs": {
            "runtime": "vercel-rust@4.0.6"
        }
    }
}

With this configuration, when I navigate to the route /foo, i.e., without /api as the base path, I get the usual 404: NOT_FOUND, and when I navigate to the route /api/foo, I get 502: BAD_GATEWAY and see this panic log in the terminal:

❯ vc dev
Vercel CLI 32.2.0
> Ready! Available at http://localhost:3000
> Building vercel-rust@4.0.6:api/main.rs
> Built vercel-rust@4.0.6:api/main.rs [4s]
thread 'main' panicked at 'internal error: entered unreachable code', examples/route-merge/api/main.rs:8:1
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
TypeError: Object prototype may only be an Object or null: undefined
    at Function.setPrototypeOf (<anonymous>)
    at new LambdaError (/home/rajput-hemant/.bun/install/global/node_modules/vercel/dist/index.js:13136:16)
    at Lambda.<anonymous> (/home/rajput-hemant/.bun/install/global/node_modules/vercel/dist/index.js:13269:27)
    at Generator.next (<anonymous>)
    at fulfilled (/home/rajput-hemant/.bun/install/global/node_modules/vercel/dist/index.js:13183:58)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
dglsparsons commented 10 months ago

Hi @rajput-hemant - thanks for raising this. From your examples it looks like we don't support both rewrites and function bundling at the same time. I'll have a look into this though 🙏

rajput-hemant commented 10 months ago

@dglsparsons Thanks for your quick response and for looking into this. I'm hopeful that this issue can be fixed, as having both rewrites and function bundling would be a valuable addition.