Skip to content

Commit ec4b622

Browse files
authored
Amend behaviour for selecting path prefix in proc macro (#103)
We encountered an issue deploying the `examples/route-merge` directory, insofar as it worked locally, and worked with `vc --prebuilt`, but deploying via the Vercel build system by running `vercel deploy` resulted in all the API routes panicing. This happens because: - The routes are properly formed and specified from the rust runtime package (typescript side). Meaning we create the correct Vercel functions. - However, no routes are picked up by the proc macro. The `vercel deploy` command only uploads the current directory into Vercel. Meaning when built via the Vercel build system, the `examples/route-merge` directory does not appear to be part of a cargo workspace. The path prefix specified then does not point to `examples/route-merge/api/**.rs`, but instead to `examples/route-merge/examples/route-merge/api/**.rs`, meaning no routing are created. The fix for this is to fall back to acting as if no `path` is specified in the case where `{path}/api/main.rs` cannot be found.
1 parent 48e09a6 commit ec4b622

File tree

1 file changed

+19
-4
lines changed
  • crates/vercel_runtime_macro/src

1 file changed

+19
-4
lines changed

crates/vercel_runtime_macro/src/lib.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use glob::glob;
22
use proc_macro::TokenStream;
33
use quote::quote;
44
use std::collections::HashMap;
5+
use std::fs;
56
use syn::parse_macro_input;
67
use syn::AttributeArgs;
78
use vercel_runtime_router::{Route, Router};
@@ -26,14 +27,28 @@ pub fn bundled_api(args: TokenStream, stream: TokenStream) -> TokenStream {
2627

2728
let prefix = args_map
2829
.get("path")
29-
.map(|s| {
30-
if s.ends_with('/') {
31-
s.to_owned()
30+
.map(|p| {
31+
if p.ends_with('/') {
32+
p.to_owned()
3233
} else {
33-
format!("{}/", s)
34+
format!("{}/", p)
35+
}
36+
})
37+
.and_then(|p| {
38+
if fs::metadata(format!("{}api/main.rs", p)).is_ok() {
39+
Some(p)
40+
} else {
41+
// there is a `path` specified, but it doesn't appear to be in a cargo workspace,
42+
// so default to acting as if it is not in a cargo workspace.
43+
// This protects us from both
44+
// - using `vercel` to deploy via build container, but only uploading part of a
45+
// workspace
46+
// - path being specified incorrectly
47+
None
3448
}
3549
})
3650
.unwrap_or("".to_string());
51+
3752
let glob_pattern = format!("{}api/**/[!main]*.rs", prefix);
3853

3954
let input: syn::ItemFn = syn::parse(stream).unwrap();

0 commit comments

Comments
 (0)