Skip to content

Commit d79b6a7

Browse files
authored
fix(nextjs): Use local copies for all sentry packages when testing on vercel (getsentry#3812)
Previously, when testing a project on vercel against a particular branch of the SDK repo, only the `@sentry/nextjs` package was reflecting the branches changes (by dint of the fact that it was listed as a file dependency pointing to the built SDK). All other `@sentry/*` packages were still getting downloaded from `npm`. This fixes the install script so that it will force all interdependencies between `@sentry/*` packages to be specified by absolute file paths. That way, only the branch versions of all packages will be used. There are also some small logging improvements and some generalized cleanup.
1 parent 4bec90f commit d79b6a7

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed

packages/nextjs/vercel/install-sentry-from-branch.sh

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
# SCRIPT TO INCLUDE AS PART OF A VERCEL-DEPLOYED PROJECT, SO THAT IT USES A BRANCH FROM THE SDK REPO
22
# USE `yarn vercel:project <path-to-project>` TO HAVE IT AUTOMATICALLY ADDED TO YOUR PROJECT
33

4-
# CUSTOM INSTALL COMMAND FOR PROJECT ON VERCEL: `source .sentry/install-sentry-from-branch.sh`
4+
# CUSTOM INSTALL COMMAND FOR PROJECT ON VERCEL: `bash .sentry/install-sentry-from-branch.sh`
55

66
PROJECT_DIR=$(pwd)
7+
REPO_DIR="${PROJECT_DIR}/sentry-javascript"
78

89
# Set BRANCH_NAME as an environment variable
910
source .sentry/set-branch-name.sh
1011

1112
echo " "
1213
echo "CLONING SDK REPO"
1314
git clone https://github.com/getsentry/sentry-javascript.git
14-
cd sentry-javascript
15+
16+
echo " "
17+
echo "MOVING INTO REPO DIRECTORY AND CHECKING OUT BRANCH"
18+
cd $REPO_DIR
1519
git checkout $BRANCH_NAME
16-
echo "Latest commit: $(git log --format="%C(auto) %h - %s" | head -n 1)"
20+
21+
echo "LATEST COMMIT: $(git log --format="%C(auto) %h - %s" | head -n 1)"
1722

1823
echo " "
1924
echo "INSTALLING SDK DEPENDENCIES"
@@ -22,13 +27,65 @@ yarn --prod false
2227

2328
echo " "
2429
echo "BUILDING SDK"
25-
# we need to build es5 versions because `next.config.js` calls `require` on the SDK (to get `withSentryConfig`) and
30+
# We need to build es5 versions because `next.config.js` calls `require` on the SDK (to get `withSentryConfig`) and
2631
# therefore it looks for `dist/index.js`
2732
yarn build:es5
28-
# we need to build esm versions because that's what `next` actually uses when it builds the app
33+
# We need to build esm versions because that's what `next` actually uses when it builds the app
2934
yarn build:esm
35+
36+
# Set all packages in the repo to point to their siblings as file dependencies. That way, when we install the local copy
37+
# of @sentry/nextjs, it'll pull the local copy of each of its @sentry/* dependents. This mimics what Lerna does with
38+
# symlinks, just with file dependencies (which we have to use because linking seems to lead to module resolution
39+
# errors).
40+
echo " "
41+
echo "POINTING SIBLING DEPENDENCIES IN PACKAGE.JSON AT LOCAL DIRECTORIES"
42+
PACKAGES_DIR="$REPO_DIR/packages"
43+
# Escape all of the slashes in the path for use in sed
44+
ESCAPED_PACKAGES_DIR=$(echo $PACKAGES_DIR | sed s/'\/'/'\\\/'/g)
45+
46+
PACKAGE_NAMES=$(ls PACKAGES_DIR)
47+
48+
# Modify each package's package.json file by searching in it for sentry dependencies from the monorepo and, for each
49+
# sibling dependency found, replacing the version number with a file dependency pointing to the sibling itself (so
50+
# `"@sentry/utils": "6.9.0"` becomes `"@sentry/utils": "file:/abs/path/to/sentry-javascript/packages/utils"`)
51+
for package in $PACKAGE_NAMES; do
52+
# Within a given package.json file, search for each of the other packages in turn, and if found, make the replacement
53+
for package_dep in $PACKAGE_NAMES; do
54+
sed -Ei /"@sentry\/${package_dep}"/s/"[0-9]+\.[0-9]+\.[0-9]+"/"file:${ESCAPED_PACKAGES_DIR}\/${package_dep}"/ ${PACKAGES_DIR}/${package}/package.json
55+
done
56+
done
57+
58+
echo " "
59+
echo "MOVING BACK TO PROJECT DIRECTORY"
3060
cd $PROJECT_DIR
3161

62+
# TODO move this into `yarn vercel:project` script, accounting for differences in SDK repo location between running the
63+
# test app locally and on vercel
64+
echo " "
65+
echo "PATCHING SENTRY.SERVER.CONFIG.JS AND SENTRY.CLIENT.CONFIG.JS"
66+
echo "Removing frame limit on stacktraces"
67+
echo "Tagging events with $(vercel) tag"
68+
echo "Tagging events with SDK repo's most recent commit message"
69+
echo "Tagging events with test project repo's most recent commit message"
70+
71+
INFINITE_STACKTRACE_CODE="
72+
Error.stackTraceLimit = Infinity;
73+
"
74+
75+
SDK_COMMIT_MESSAGE=$(cd sentry-javascript && git log --format="%C(auto)%s" | head -n 1)
76+
CONFIGURE_SCOPE_CODE="
77+
Sentry.configureScope(scope => {
78+
if (process.env.VERCEL) {
79+
scope.setTag('vercel', true);
80+
}
81+
scope.setTag('commitMessage', process.env.VERCEL_GIT_COMMIT_MESSAGE);
82+
scope.setTag('sdkCommitMessage', \"$SDK_COMMIT_MESSAGE\");
83+
});
84+
"
85+
86+
echo "$INFINITE_STACKTRACE_CODE" "$CONFIGURE_SCOPE_CODE" >>sentry.server.config.js
87+
echo "$INFINITE_STACKTRACE_CODE" "$CONFIGURE_SCOPE_CODE" >>sentry.client.config.js
88+
3289
# Add built SDK as a file dependency. This has the side effect of forcing yarn to install all of the other dependencies,
3390
# saving us the trouble of needing to call `yarn` separately after this
3491
echo " "
@@ -38,9 +95,9 @@ yarn add file:sentry-javascript/packages/nextjs
3895

3996
# In case for any reason we ever need to link the local SDK rather than adding it as a file dependency:
4097

41-
# for abs_package_path in ${PROJECT_DIR}/sentry-javascript/packages/*; do
98+
# echo " "
99+
# echo "LINKING LOCAL SDK INTO PROJECT"
42100

43-
# # link the built packages into project dependencies
44101
# for abs_package_path in sentry-javascript/packages/*; do
45102
# package=$(basename $abs_package_path)
46103

packages/nextjs/vercel/instructions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ commit (but not push) this change.
3333

3434
Go into your project settings on Vercel and change the install command to
3535

36-
`source .sentry/install-sentry-from-branch.sh`.
36+
`bash .sentry/install-sentry-from-branch.sh`.
3737

3838
If you're using bundle analyzer, change the build command to
3939

packages/nextjs/vercel/make-project-use-current-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ cd $NEXTJS_SDK_DIR
5858
echo " "
5959
echo "SUCCESS!"
6060
echo "Your project will now use this branch of the SDK repo when deployed to Vercel. If you haven't done so already, go to your project settings in Vercel and set a custom install command:"
61-
echo " $(source .sentry/install-sentry-from-branch.sh)"
61+
echo " bash .sentry/install-sentry-from-branch.sh"
6262
echo " "

0 commit comments

Comments
 (0)