Skip to content

fix(aws): Resolve all Sentry packages to local versions in layer build #17106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions packages/aws-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
},
"scripts": {
"build": "run-p build:transpile build:types",
"build:bundle": "yarn build:layer",
"build:layer": "yarn ts-node scripts/buildLambdaLayer.ts",
"build:dev": "run-p build:transpile build:types",
"build:transpile": "rollup -c rollup.npm.config.mjs && yarn build:layer",
Expand All @@ -103,18 +102,5 @@
"volta": {
"extends": "../../package.json"
},
"sideEffects": false,
"nx": {
"targets": {
"build:bundle": {
"dependsOn": [
"build:transpile",
"build:types"
],
"outputs": [
"{projectRoot}/build/aws"
]
}
}
}
"sideEffects": false
}
48 changes: 42 additions & 6 deletions packages/aws-serverless/scripts/buildLambdaLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ function run(cmd: string, options?: childProcess.ExecSyncOptions): string {
*/
async function buildLambdaLayer(): Promise<void> {
console.log('Building Lambda layer.');
buildPackageJson();
console.log('Installing local @sentry/aws-serverless into build/aws/dist-serverless/nodejs.');

console.log('Creating target directory for npm install.');
fsForceMkdirSync('./build/aws/dist-serverless/nodejs');

run('npm install . --prefix ./build/aws/dist-serverless/nodejs --install-links');
run('yarn install --prod --cwd ./build/aws/dist-serverless/nodejs');

await pruneNodeModules();
fs.rmSync('./build/aws/dist-serverless/nodejs/package.json', { force: true });
fs.rmSync('./build/aws/dist-serverless/nodejs/package-lock.json', { force: true });
fs.rmSync('./build/aws/dist-serverless/nodejs/yarn.lock', { force: true });

// The layer also includes `awslambda-auto.js`, a helper file which calls `Sentry.init()` and wraps the lambda
// handler. It gets run when Node is launched inside the lambda, using the environment variable
Expand Down Expand Up @@ -142,3 +139,42 @@ function getAllFiles(dir: string): string[] {
walkDirectory(dir);
return files;
}

function buildPackageJson(): void {
console.log('Building package.json');
const packagesDir = path.resolve(__dirname, '..');
const packageDirs = fs
.readdirSync(packagesDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
.filter(name => !name.startsWith('.')) // Skip hidden directories
.sort();

const resolutions: Record<string, string> = {};

for (const packageDir of packageDirs) {
const packageJsonPath = path.join(packagesDir, packageDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
try {
const packageContent = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as { name?: string };
const packageName = packageContent.name;
if (typeof packageName === 'string' && packageName) {
resolutions[packageName] = `file:../../../../../../packages/${packageDir}`;
}
} catch {
console.warn(`Warning: Could not read package.json for ${packageDir}`);
}
}
}

const packageJson = {
dependencies: {
'@sentry/aws-serverless': version,
},
resolutions,
};

fsForceMkdirSync('./build/aws/dist-serverless/nodejs');
const packageJsonPath = './build/aws/dist-serverless/nodejs/package.json';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
Loading