Skip to content

Commit d5a0ca4

Browse files
authored
fix windows dev server bin resolve (#84)
1 parent e56eb6f commit d5a0ca4

File tree

13 files changed

+75
-64
lines changed

13 files changed

+75
-64
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ First, you'll need a `vercel.json` file in your project:
2020
{
2121
"functions": {
2222
"api/**/*.rs": {
23-
"runtime": "[email protected].1"
23+
"runtime": "[email protected].2"
2424
}
2525
}
2626
}

examples/cron/vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/**/*.rs": {
4-
"runtime": "[email protected].1"
4+
"runtime": "[email protected].2"
55
}
66
},
77
"crons": [

examples/nextjs/vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/**/*.rs": {
4-
"runtime": "[email protected].1"
4+
"runtime": "[email protected].2"
55
}
66
}
77
}

examples/simple/vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/**/*.rs": {
4-
"runtime": "[email protected].1"
4+
"runtime": "[email protected].2"
55
}
66
}
77
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vercel-rust",
3-
"version": "4.0.0-beta.1",
3+
"version": "4.0.0-beta.2",
44
"description": "Rust runtime for Vercel Functions.",
55
"homepage": "https://github.com/vercel-community/rust",
66
"repository": {

src/index.ts

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,29 @@
1-
import fs from 'node:fs';
21
import path from 'node:path';
32
import type { BuildOptions, BuildResultV3 } from '@vercel/build-utils';
43
import {
54
FileFsRef,
65
debug,
76
download,
87
glob,
9-
runShellScript,
108
createLambda,
119
} from '@vercel/build-utils';
1210
import execa from 'execa';
1311
import { installRustToolchain } from './lib/rust-toolchain';
1412
import type { Runtime } from './lib/runtime';
15-
import { getCargoMetadata, findBinaryName, findCargoWorkspace } from './lib/cargo';
13+
import {
14+
getCargoMetadata,
15+
findBinaryName,
16+
findCargoWorkspace,
17+
} from './lib/cargo';
18+
import {
19+
assertEnv,
20+
getExecutableName,
21+
gatherExtraFiles,
22+
runUserScripts,
23+
} from './lib/utils';
1624

1725
type RustEnv = Record<'RUSTFLAGS' | 'PATH', string>;
1826

19-
function assertEnv(name: string): string {
20-
if (!process.env[name]) {
21-
throw new Error(`Missing ENV variable process.env.${name}`);
22-
}
23-
24-
return process.env[name] as unknown as string;
25-
}
26-
27-
async function runUserScripts(dir: string): Promise<void> {
28-
const buildScriptPath = path.join(dir, 'build.sh');
29-
const buildScriptExists = fs.existsSync(buildScriptPath);
30-
31-
if (buildScriptExists) {
32-
debug('Running `build.sh`');
33-
await runShellScript(buildScriptPath);
34-
}
35-
}
36-
37-
async function gatherExtraFiles(
38-
globMatcher: string | string[] | undefined,
39-
workPath: string,
40-
): Promise<Record<string, FileFsRef>> {
41-
if (!globMatcher) return {};
42-
43-
debug(
44-
`Gathering extra files for glob \`${JSON.stringify(
45-
globMatcher,
46-
)}\` in ${workPath}`,
47-
);
48-
49-
if (Array.isArray(globMatcher)) {
50-
const allMatches = await Promise.all(
51-
globMatcher.map((pattern) => glob(pattern, workPath)),
52-
);
53-
54-
return allMatches.reduce((acc, matches) => ({ ...acc, ...matches }), {});
55-
}
56-
57-
return glob(globMatcher, workPath);
58-
}
59-
6027
async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
6128
const BUILDER_DEBUG = Boolean(process.env.VERCEL_BUILDER_DEBUG ?? false);
6229
const { files, entrypoint, workPath, config, meta } = options;
@@ -103,11 +70,7 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
10370
throw err;
10471
}
10572

106-
debug(`Building \`${binaryName}\` completed`);
107-
108-
// The compiled binary in Windows has the `.exe` extension
109-
const binExtension = process.platform === 'win32' ? '.exe' : '';
110-
const bootstrap = `bootstrap${binExtension}`;
73+
debug(`Building \`${binaryName}\` for \`${process.platform}\` completed`);
11174

11275
const { target_directory: targetDirectory } = await getCargoMetadata({
11376
cwd: process.cwd(),
@@ -117,9 +80,10 @@ async function buildHandler(options: BuildOptions): Promise<BuildResultV3> {
11780
const bin = path.join(
11881
targetDirectory,
11982
BUILDER_DEBUG ? 'debug' : 'release',
120-
binaryName,
83+
getExecutableName(binaryName),
12184
);
12285

86+
const bootstrap = getExecutableName('bootstrap');
12387
const lambda = await createLambda({
12488
files: {
12589
...extraFiles,
@@ -141,7 +105,6 @@ const runtime: Runtime = {
141105
prepareCache: async ({ workPath }) => {
142106
debug(`Caching \`${workPath}\``);
143107
const cacheFiles = await glob('target/**', workPath);
144-
145108
// Convert this into a reduce
146109
for (const f of Object.keys(cacheFiles)) {
147110
const accept =
@@ -155,7 +118,6 @@ const runtime: Runtime = {
155118
delete cacheFiles[f];
156119
}
157120
}
158-
159121
return cacheFiles;
160122
},
161123
shouldServe: async (options): Promise<boolean> => {

src/lib/rust-toolchain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function downloadRustToolchain({
55
version = 'stable',
66
}: {
77
version?: string;
8-
}) {
8+
}): Promise<void> {
99
try {
1010
await execa(
1111
`curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${version}`,
@@ -21,7 +21,7 @@ async function downloadRustToolchain({
2121
}
2222
}
2323

24-
export const installRustToolchain = async (version?: string) => {
24+
export const installRustToolchain = async (version?: string): Promise<void> => {
2525
try {
2626
await execa(`rustup -V`, [], { shell: true, stdio: 'ignore' });
2727
debug('Rust Toolchain is already installed, skipping download');

src/lib/utils.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import type { FileFsRef } from '@vercel/build-utils';
4+
import { debug, glob, runShellScript } from '@vercel/build-utils';
5+
6+
export function getExecutableName(binName: string): string {
7+
// The compiled binary in Windows has the `.exe` extension
8+
return process.platform === 'win32' ? `${binName}.exe` : binName;
9+
}
10+
11+
export function assertEnv(name: string): string {
12+
if (!process.env[name]) {
13+
throw new Error(`Missing ENV variable process.env.${name}`);
14+
}
15+
return process.env[name] as unknown as string;
16+
}
17+
18+
export async function runUserScripts(dir: string): Promise<void> {
19+
const buildScriptPath = path.join(dir, 'build.sh');
20+
const buildScriptExists = fs.existsSync(buildScriptPath);
21+
22+
if (buildScriptExists) {
23+
debug('Running `build.sh`');
24+
await runShellScript(buildScriptPath);
25+
}
26+
}
27+
28+
export async function gatherExtraFiles(
29+
globMatcher: string | string[] | undefined,
30+
workPath: string,
31+
): Promise<Record<string, FileFsRef>> {
32+
if (!globMatcher) return {};
33+
34+
debug(
35+
`Gathering extra files for glob \`${JSON.stringify(
36+
globMatcher,
37+
)}\` in ${workPath}`,
38+
);
39+
40+
if (Array.isArray(globMatcher)) {
41+
const allMatches = await Promise.all(
42+
globMatcher.map((pattern) => glob(pattern, workPath)),
43+
);
44+
45+
return allMatches.reduce((acc, matches) => ({ ...acc, ...matches }), {});
46+
}
47+
48+
return glob(globMatcher, workPath);
49+
}

test/fixtures/01-include-files/vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/**/*.rs": {
4-
"runtime": "[email protected].1",
4+
"runtime": "[email protected].2",
55
"includeFiles": "static/**/*.{txt,svg}"
66
}
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"functions": {
33
"api/**/*.rs": {
4-
"runtime": "[email protected].1"
4+
"runtime": "[email protected].2"
55
}
66
}
77
}

0 commit comments

Comments
 (0)