@@ -12,6 +12,7 @@ const LAMBDA_FUNCTIONS_WITH_LAYER_DIR = './src/lambda-functions-layer';
1212const LAMBDA_FUNCTIONS_WITH_NPM_DIR = './src/lambda-functions-npm' ;
1313const LAMBDA_FUNCTION_TIMEOUT = 10 ;
1414const LAYER_DIR = './node_modules/@sentry/aws-serverless/' ;
15+ const WORKSPACE_AWS_SERVERLESS_DIR = path . resolve ( __dirname , '../../../../../packages/aws-serverless' ) ;
1516const DEFAULT_NODE_VERSION = '22' ;
1617export const SAM_PORT = 3001 ;
1718
@@ -67,10 +68,38 @@ export class LocalLambdaStack extends Stack {
6768 const functionName = `${ addLayer ? 'Layer' : 'Npm' } ${ lambdaDir } ` ;
6869
6970 if ( ! addLayer ) {
71+ const lambdaPath = path . resolve ( functionsDir , lambdaDir ) ;
72+ const packageLockPath = path . join ( lambdaPath , 'package-lock.json' ) ;
73+ const nodeModulesPath = path . join ( lambdaPath , 'node_modules' ) ;
74+ // Point the dependency at the locally built aws-serverless package so tests use the current workspace bits
75+ if ( ! fs . existsSync ( WORKSPACE_AWS_SERVERLESS_DIR ) ) {
76+ throw new Error (
77+ `[LocalLambdaStack] Workspace aws-serverless package not found at ${ WORKSPACE_AWS_SERVERLESS_DIR } . Did you run the build?` ,
78+ ) ;
79+ }
80+ const localAwsServerlessPath = path . relative ( lambdaPath , WORKSPACE_AWS_SERVERLESS_DIR ) ;
81+ const dependencyPath = localAwsServerlessPath . replace ( / \\ / g, '/' ) ;
82+
7083 console . log ( `[LocalLambdaStack] Install dependencies for ${ functionName } ` ) ;
71- const packageJson = { dependencies : { '@sentry/aws-serverless' : '* || latest' } } ;
72- fs . writeFileSync ( path . join ( functionsDir , lambdaDir , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
73- execFileSync ( 'npm' , [ 'install' , '--prefix' , path . join ( functionsDir , lambdaDir ) ] , { stdio : 'inherit' } ) ;
84+
85+ if ( fs . existsSync ( packageLockPath ) ) {
86+ // Prevent stale lock files from pinning the published package version
87+ fs . rmSync ( packageLockPath ) ;
88+ }
89+
90+ if ( fs . existsSync ( nodeModulesPath ) ) {
91+ // Ensure we reinstall from the workspace instead of reusing cached dependencies
92+ fs . rmSync ( nodeModulesPath , { recursive : true , force : true } ) ;
93+ }
94+
95+ const packageJson = {
96+ dependencies : {
97+ '@sentry/aws-serverless' : `file:${ dependencyPath } ` ,
98+ } ,
99+ } ;
100+
101+ fs . writeFileSync ( path . join ( lambdaPath , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
102+ execFileSync ( 'npm' , [ 'install' , '--prefix' , lambdaPath ] , { stdio : 'inherit' } ) ;
74103 }
75104
76105 new CfnResource ( this , functionName , {
0 commit comments