11import fs from 'fs-extra' ;
22import path from 'path' ;
3- import execa from 'execa' ;
3+ import execa , { ExecaError } from 'execa' ;
44import toml from '@iarna/toml' ;
55import {
66 glob ,
@@ -16,6 +16,9 @@ import {
1616} from '@vercel/build-utils' ;
1717import { installRustAndFriends } from './install-rust' ;
1818
19+ type RustEnv = Record < 'RUSTFLAGS' | 'PATH' , string > &
20+ Record < string , string | undefined > ;
21+
1922interface 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+
7077async 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 ( ! / c o u l d n o t f i n d / 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 ( ! / c o u l d n o t f i n d / 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