22import { nodeFileTrace } from '@vercel/nft' ;
33import * as childProcess from 'child_process' ;
44import * as fs from 'fs' ;
5+ import * as os from 'os' ;
56import * as path from 'path' ;
67import { version } from '../package.json' ;
78
@@ -23,11 +24,19 @@ async function buildLambdaLayer(): Promise<void> {
2324 console . log ( 'Building Lambda layer.' ) ;
2425 buildPackageJson ( ) ;
2526 console . log ( 'Installing local @sentry/aws-serverless into build/aws/dist-serverless/nodejs.' ) ;
26- run ( 'yarn install --prod --cwd ./build/aws/dist-serverless/nodejs' ) ;
27+ // Use a temporary cache folder to avoid stale cache references to local file: packages.
28+ // Yarn's global cache can contain outdated references to build artifacts from other
29+ // @sentry /* packages (e.g., build/node_modules paths that no longer exist), causing
30+ // ENOENT errors during file copying.
31+ // The cache folder must be outside the monorepo to avoid recursive nesting when Yarn
32+ // follows file: links and copies package directories.
33+ const cacheFolder = path . join ( os . tmpdir ( ) , `sentry-lambda-build-cache-${ Date . now ( ) } ` ) ;
34+ run ( `yarn install --prod --cwd ./build/aws/dist-serverless/nodejs --cache-folder "${ cacheFolder } "` ) ;
2735
2836 await pruneNodeModules ( ) ;
2937 fs . rmSync ( './build/aws/dist-serverless/nodejs/package.json' , { force : true } ) ;
3038 fs . rmSync ( './build/aws/dist-serverless/nodejs/yarn.lock' , { force : true } ) ;
39+ fs . rmSync ( cacheFolder , { recursive : true , force : true } ) ;
3140
3241 // The layer also includes `awslambda-auto.js`, a helper file which calls `Sentry.init()` and wraps the lambda
3342 // handler. It gets run when Node is launched inside the lambda, using the environment variable
0 commit comments