Skip to content

Commit 3c8bac6

Browse files
authored
fix: process.env members could be undefined, abstract into RustEnv type (#49)
1 parent c1980f0 commit 3c8bac6

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs-extra';
22
import path from 'path';
3-
import execa from 'execa';
3+
import execa, { ExecaError } from 'execa';
44
import toml from '@iarna/toml';
55
import {
66
glob,
@@ -16,6 +16,9 @@ import {
1616
} from '@vercel/build-utils';
1717
import { installRustAndFriends } from './install-rust';
1818

19+
type RustEnv = Record<'RUSTFLAGS' | 'PATH', string> &
20+
Record<string, string | undefined>;
21+
1922
interface CargoConfig {
2023
env: Record<string, any>;
2124
cwd: string;
@@ -67,6 +70,10 @@ async function runUserScripts(entrypoint: string) {
6770
}
6871
}
6972

73+
function isExecaError(err: Error): err is ExecaError {
74+
return 'stderr' in err;
75+
}
76+
7077
async function cargoLocateProject(config: CargoConfig) {
7178
try {
7279
const { stdout: projectDescriptionStr } = await execa(
@@ -79,10 +86,12 @@ async function cargoLocateProject(config: CargoConfig) {
7986
return projectDescription.root;
8087
}
8188
} catch (e) {
82-
if (!/could not find/g.test(e.stderr)) {
83-
console.error("Couldn't run `cargo locate-project`");
84-
throw e;
89+
if (e instanceof Error && isExecaError(e)) {
90+
if (!/could not find/g.test(e.stderr)) {
91+
console.error("Couldn't run `cargo locate-project`");
92+
}
8593
}
94+
throw e;
8695
}
8796

8897
return null;
@@ -164,7 +173,7 @@ async function buildSingleFile(
164173
{ entrypoint, workPath, meta = {} }: BuildOptions,
165174
downloadedFiles: DownloadedFiles,
166175
extraFiles: DownloadedFiles,
167-
rustEnv: Record<string, string>
176+
rustEnv: RustEnv
168177
) {
169178
debug('Building single file');
170179
const entrypointPath = downloadedFiles[entrypoint].fsPath;
@@ -250,7 +259,7 @@ export async function build(opts: BuildOptions) {
250259
const entryPath = downloadedFiles[entrypoint].fsPath;
251260

252261
const { PATH, HOME } = process.env;
253-
const rustEnv: Record<string, string> = {
262+
const rustEnv: RustEnv = {
254263
...process.env,
255264
PATH: `${path.join(HOME!, '.cargo/bin')}:${PATH}`,
256265
RUSTFLAGS: [process.env.RUSTFLAGS, ...codegenFlags]
@@ -277,7 +286,7 @@ export async function prepareCache({
277286
targetFolderDir = path.dirname(path.join(workPath, entrypoint));
278287
} else {
279288
const { PATH, HOME } = process.env;
280-
const rustEnv: Record<string, string> = {
289+
const rustEnv: RustEnv = {
281290
...process.env,
282291
PATH: `${path.join(HOME!, '.cargo/bin')}:${PATH}`,
283292
RUSTFLAGS: [process.env.RUSTFLAGS, ...codegenFlags]

0 commit comments

Comments
 (0)