.envrc 6.7 KB

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