@@ -15,6 +15,17 @@ const LAYER_DIR = './node_modules/@sentry/aws-serverless/';
1515const DEFAULT_NODE_VERSION = '22' ;
1616export const SAM_PORT = 3001 ;
1717
18+ function resolvePackagesDir ( ) : string {
19+ // When running via the e2e test runner, tests are copied to a temp directory
20+ // so we need the workspace root passed via env var
21+ const workspaceRoot = process . env . SENTRY_E2E_WORKSPACE_ROOT ;
22+ if ( workspaceRoot ) {
23+ return path . join ( workspaceRoot , 'packages' ) ;
24+ }
25+ // Fallback for local development when running from the original location
26+ return path . resolve ( __dirname , '../../../../../packages' ) ;
27+ }
28+
1829export class LocalLambdaStack extends Stack {
1930 sentryLayer : CfnResource ;
2031
@@ -67,10 +78,46 @@ export class LocalLambdaStack extends Stack {
6778 const functionName = `${ addLayer ? 'Layer' : 'Npm' } ${ lambdaDir } ` ;
6879
6980 if ( ! addLayer ) {
81+ const lambdaPath = path . resolve ( functionsDir , lambdaDir ) ;
82+ const packageLockPath = path . join ( lambdaPath , 'package-lock.json' ) ;
83+ const nodeModulesPath = path . join ( lambdaPath , 'node_modules' ) ;
84+
85+ // Point the dependency at the locally built packages so tests use the current workspace bits
86+ // We need to link all @sentry /* packages that are dependencies of aws-serverless
87+ // because otherwise npm will try to install them from the registry, where the current version is not yet published
88+ const packagesToLink = [ 'aws-serverless' , 'node' , 'core' , 'node-core' , 'opentelemetry' ] ;
89+ const dependencies : Record < string , string > = { } ;
90+
91+ const packagesDir = resolvePackagesDir ( ) ;
92+ for ( const pkgName of packagesToLink ) {
93+ const pkgDir = path . join ( packagesDir , pkgName ) ;
94+ if ( ! fs . existsSync ( pkgDir ) ) {
95+ throw new Error (
96+ `[LocalLambdaStack] Workspace package ${ pkgName } not found at ${ pkgDir } . Did you run the build?` ,
97+ ) ;
98+ }
99+ const relativePath = path . relative ( lambdaPath , pkgDir ) ;
100+ dependencies [ `@sentry/${ pkgName } ` ] = `file:${ relativePath . replace ( / \\ / g, '/' ) } ` ;
101+ }
102+
70103 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' } ) ;
104+
105+ if ( fs . existsSync ( packageLockPath ) ) {
106+ // Prevent stale lock files from pinning the published package version
107+ fs . rmSync ( packageLockPath ) ;
108+ }
109+
110+ if ( fs . existsSync ( nodeModulesPath ) ) {
111+ // Ensure we reinstall from the workspace instead of reusing cached dependencies
112+ fs . rmSync ( nodeModulesPath , { recursive : true , force : true } ) ;
113+ }
114+
115+ const packageJson = {
116+ dependencies,
117+ } ;
118+
119+ fs . writeFileSync ( path . join ( lambdaPath , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
120+ execFileSync ( 'npm' , [ 'install' , '--prefix' , lambdaPath ] , { stdio : 'inherit' } ) ;
74121 }
75122
76123 new CfnResource ( this , functionName , {
0 commit comments