Skip to content

Commit 74d4566

Browse files
authored
fix(aws): Resolve all Sentry packages to local versions in layer build (#17106)
This fixes the AWS Layer release build which tried to fetch the new package versions from the NPM registry, which don't exist yet during the build. We now build a new `package.json` that uses `resolutions` to point all Sentry packages to their local version.
1 parent b9222cf commit 74d4566

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

packages/aws-serverless/package.json

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
},
8080
"scripts": {
8181
"build": "run-p build:transpile build:types",
82-
"build:bundle": "yarn build:layer",
8382
"build:layer": "yarn ts-node scripts/buildLambdaLayer.ts",
8483
"build:dev": "run-p build:transpile build:types",
8584
"build:transpile": "rollup -c rollup.npm.config.mjs && yarn build:layer",
@@ -103,18 +102,5 @@
103102
"volta": {
104103
"extends": "../../package.json"
105104
},
106-
"sideEffects": false,
107-
"nx": {
108-
"targets": {
109-
"build:bundle": {
110-
"dependsOn": [
111-
"build:transpile",
112-
"build:types"
113-
],
114-
"outputs": [
115-
"{projectRoot}/build/aws"
116-
]
117-
}
118-
}
119-
}
105+
"sideEffects": false
120106
}

packages/aws-serverless/scripts/buildLambdaLayer.ts

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ function run(cmd: string, options?: childProcess.ExecSyncOptions): string {
2121
*/
2222
async function buildLambdaLayer(): Promise<void> {
2323
console.log('Building Lambda layer.');
24+
buildPackageJson();
2425
console.log('Installing local @sentry/aws-serverless into build/aws/dist-serverless/nodejs.');
25-
26-
console.log('Creating target directory for npm install.');
27-
fsForceMkdirSync('./build/aws/dist-serverless/nodejs');
28-
29-
run('npm install . --prefix ./build/aws/dist-serverless/nodejs --install-links');
26+
run('yarn install --prod --cwd ./build/aws/dist-serverless/nodejs');
3027

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

3532
// The layer also includes `awslambda-auto.js`, a helper file which calls `Sentry.init()` and wraps the lambda
3633
// handler. It gets run when Node is launched inside the lambda, using the environment variable
@@ -142,3 +139,42 @@ function getAllFiles(dir: string): string[] {
142139
walkDirectory(dir);
143140
return files;
144141
}
142+
143+
function buildPackageJson(): void {
144+
console.log('Building package.json');
145+
const packagesDir = path.resolve(__dirname, '..');
146+
const packageDirs = fs
147+
.readdirSync(packagesDir, { withFileTypes: true })
148+
.filter(dirent => dirent.isDirectory())
149+
.map(dirent => dirent.name)
150+
.filter(name => !name.startsWith('.')) // Skip hidden directories
151+
.sort();
152+
153+
const resolutions: Record<string, string> = {};
154+
155+
for (const packageDir of packageDirs) {
156+
const packageJsonPath = path.join(packagesDir, packageDir, 'package.json');
157+
if (fs.existsSync(packageJsonPath)) {
158+
try {
159+
const packageContent = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as { name?: string };
160+
const packageName = packageContent.name;
161+
if (typeof packageName === 'string' && packageName) {
162+
resolutions[packageName] = `file:../../../../../../packages/${packageDir}`;
163+
}
164+
} catch {
165+
console.warn(`Warning: Could not read package.json for ${packageDir}`);
166+
}
167+
}
168+
}
169+
170+
const packageJson = {
171+
dependencies: {
172+
'@sentry/aws-serverless': version,
173+
},
174+
resolutions,
175+
};
176+
177+
fsForceMkdirSync('./build/aws/dist-serverless/nodejs');
178+
const packageJsonPath = './build/aws/dist-serverless/nodejs/package.json';
179+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
180+
}

0 commit comments

Comments
 (0)