.envrc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # This is the .envrc for sentry, for use with direnv.
  2. # It's responsible for enforcing a standard dev environment by checking as much state as possible, and either performing
  3. # initialization (e.g. activating the venv) or giving recommendations on how to reach the desired state.
  4. # It also sets useful environment variables.
  5. # If you'd like to override or set any custom environment variables, this .envrc will read a .env file at the end.
  6. set -e
  7. SENTRY_ROOT="$(
  8. cd "$(dirname "${BASH_SOURCE[0]}")"
  9. pwd -P
  10. )"
  11. source "${SENTRY_ROOT}/scripts/lib.sh"
  12. # XXX: we can't trap bash EXIT, because it'll override direnv's finalizing routines.
  13. # consequently, using "exit" anywhere will skip this notice from showing.
  14. # so need to use set -e, and return 1.
  15. trap notice ERR
  16. # This is used to group issues on Sentry.io.
  17. # If an issue does not call info() or die() it will be grouped under this
  18. error_message="Unknown issue"
  19. # This has to be the same value as what sentry-cli accepts
  20. log_level="info"
  21. help_message() {
  22. cat <<EOF
  23. For more help run: make direnv-help
  24. EOF
  25. }
  26. failure_message() {
  27. cat <<EOF
  28. ${red}${bold}direnv wasn't able to complete execution.
  29. You may have been given some recommendations in the error message.
  30. Follow them, and then you'll need to re-run direnv by running "direnv allow".${reset}
  31. EOF
  32. help_message
  33. }
  34. notice() {
  35. [ $? -eq 0 ] && return
  36. failure_message
  37. [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ] && report_to_sentry
  38. }
  39. report_to_sentry() {
  40. if ! require sentry-cli; then
  41. curl -sL https://sentry.io/get-cli/ | bash
  42. fi
  43. # Report to sentry-dev-env
  44. SENTRY_DSN="https://9bdb053cb8274ea69231834d1edeec4c@o1.ingest.sentry.io/5723503" \
  45. sentry-cli send-event -m "$error_message" --logfile "$_SENTRY_LOG_FILE" --level $log_level
  46. rm "$_SENTRY_LOG_FILE"
  47. }
  48. debug() {
  49. if [ "${SENTRY_DIRENV_DEBUG-}" ]; then
  50. echo -e "${@}"
  51. fi
  52. }
  53. info() {
  54. echo -e "${bold}${*}${reset}"
  55. }
  56. warn() {
  57. echo -e "${yellow}${*}${reset}" >&2
  58. log_level="warning"
  59. }
  60. die() {
  61. echo -e "${red}${bold}FATAL: ${*}${reset}" >&2
  62. # When reporting to Sentry, this will allow grouping the errors differently
  63. # NOTE: The first line of the output is used to group issues
  64. error_message=("${@}")
  65. log_level="error"
  66. return 1
  67. }
  68. prompt_python_venv_creation() {
  69. echo -e "${yellow}You are missing a Python virtualenv and we ${bold}need${reset}${yellow} to run a bootstrapping script (it can take a few minutes)"
  70. info "About to create ${venv_name}..."
  71. echo -e "\nContinue (y/N)?"
  72. read -r resp
  73. case "$resp" in
  74. y | Y) echo "Okay, let's do this." ;;
  75. *)
  76. die "Aborted!"
  77. ;;
  78. esac
  79. }
  80. show_commands_info() {
  81. echo -e "\n${red}Run the following commands to bring your environment up-to-date:"
  82. for cmd in "${commands_to_run[@]}"; do
  83. warn " ${red}$cmd"
  84. done
  85. echo ""
  86. }
  87. ### Environment ###
  88. commands_to_run=()
  89. # don't write *.pyc files; using stale python code occasionally causes subtle problems
  90. export PYTHONDONTWRITEBYTECODE=1
  91. # Always write stdout immediately. Very helpful for debugging
  92. export PYTHONUNBUFFERED=1
  93. # make sure we don't have any conflicting PYTHONPATH
  94. unset PYTHONPATH
  95. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  96. export PIP_DISABLE_PIP_VERSION_CHECK=on
  97. # increase node's memory limit, required for our webpacking
  98. export NODE_OPTIONS=--max-old-space-size=4096
  99. # Frontend hot module reloader using `react-refresh`
  100. # Enable this by default for development envs (CI/deploys do not use envrc)
  101. export SENTRY_UI_HOT_RELOAD=1
  102. ### You can override the exported variables with a .env file
  103. # All exports should happen before here unless they're safeguarded (see devenv error reporting below)
  104. if [ -f "${SENTRY_ROOT}/.env" ]; then
  105. info "Loading variables from ${SENTRY_ROOT}/.env"
  106. dotenv "${SENTRY_ROOT}/.env"
  107. fi
  108. ## Notify of reporting to Sentry
  109. if [ -n "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  110. debug "No development environment errors will be reported (since you've defined SENTRY_DEVENV_NO_REPORT)."
  111. else
  112. # Since direnv traps the EXIT signal we place the temp file under /tmp for the odd time
  113. # the script will use the EXIT path
  114. _SENTRY_LOG_FILE=$(mktemp /tmp/sentry.envrc.$$.out || mktemp /tmp/sentry.envrc.XXXXXXXX.out)
  115. exec > >(tee "$_SENTRY_LOG_FILE")
  116. exec 2>&1
  117. debug "Development errors will be reported to Sentry.io. If you wish to opt-out, set SENTRY_DEVENV_NO_REPORT as an env variable."
  118. # This will allow `sentry devservices` errors to be reported
  119. export SENTRY_DEVSERVICES_DSN=https://23670f54c6254bfd9b7de106637808e9@o1.ingest.sentry.io/1492057
  120. fi
  121. ### System ###
  122. for pkg in \
  123. make \
  124. docker \
  125. chromedriver \
  126. pkg-config \
  127. pyenv \
  128. openssl; do
  129. if ! require "$pkg"; then
  130. die "You seem to be missing the system dependency: ${pkg}
  131. Please install homebrew, and run brew bundle."
  132. fi
  133. done
  134. ### Git ###
  135. debug "Configuring git..."
  136. make setup-git-config
  137. ### Python ###
  138. venv_name=".venv"
  139. if [ ! -f "${venv_name}/bin/activate" ]; then
  140. prompt_python_venv_creation
  141. # This is time consuming but it has to be done
  142. source "${SENTRY_ROOT}/scripts/bootstrap-py3-venv"
  143. fi
  144. # The user might be cd'ing into sentry with another non-direnv managed
  145. # (in that it would be automatically deactivated) virtualenv active.
  146. deactivate 2>/dev/null || true
  147. # shellcheck disable=SC1091
  148. source "${venv_name}/bin/activate"
  149. # XXX: ideally, direnv is able to export PS1 as modified by sourcing venvs
  150. # but we'd have to patch direnv, and ".venv" isn't descriptive anyways
  151. unset PS1
  152. debug "Ensuring proper virtualenv..."
  153. "${SENTRY_ROOT}/scripts/ensure-venv.sh"
  154. if ! require sentry; then
  155. warn "Your virtualenv is activated, but sentry doesn't seem to be installed."
  156. commands_to_run+=("make install-py-dev")
  157. fi
  158. ### pre-commit ###
  159. debug "Checking pre-commit..."
  160. if ! require pre-commit; then
  161. warn "Looks like you don't have pre-commit installed."
  162. commands_to_run+=("make setup-git")
  163. fi
  164. ### Node ###
  165. debug "Checking node..."
  166. if ! require node; then
  167. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  168. fi
  169. make node-version-check
  170. if [ ! -x "node_modules/.bin/webpack" ]; then
  171. warn "You don't seem to have yarn packages installed."
  172. commands_to_run+=("make install-js-dev")
  173. fi
  174. PATH_add node_modules/.bin
  175. # These are commands that can take a significant amount of time
  176. if [ ${#commands_to_run[@]} -ne 0 ]; then
  177. show_commands_info
  178. fi
  179. if [ "${log_level}" != "info" ]; then
  180. help_message
  181. warn "\nPartial success. The virtualenv is active, however, you're not fully up-to-date (see messages above)."
  182. else
  183. echo "${green}${bold}SUCCESS!${reset}"
  184. fi
  185. # Since we can't use an EXIT routine we need to guarantee we delete the file here
  186. rm -f "$_SENTRY_LOG_FILE"