.envrc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/not/executable/bash
  2. # This is the .envrc for sentry, for use with direnv.
  3. # It's responsible for enforcing a standard dev environment by checking as much state as possible, and either performing
  4. # initialization (e.g. activating the venv) or giving recommendations on how to reach the desired state.
  5. # It also sets useful environment variables.
  6. # If you'd like to override or set any custom environment variables, this .envrc will read a .env file at the end.
  7. set -e
  8. # Upgrading Mac can uninstall the Command Line Tools, thus, removing our access to git
  9. # The message talks about xcrun, however, we can use the lack of git as a way to know that we need this
  10. # xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools),
  11. # missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
  12. if [ "$(uname -s)" == "Darwin" ] && [ ! -f "/Library/Developer/CommandLineTools/usr/bin/git" ]; then
  13. echo -e "$(tput setaf 1)\nERROR: Complete the interactive installation (10+ mins) of the Command Line Tools.$(tput sgr0)"
  14. xcode-select --install
  15. return 1
  16. fi
  17. SENTRY_ROOT="$(
  18. cd "$(dirname "${BASH_SOURCE[0]}")"
  19. pwd -P
  20. )"
  21. source "${SENTRY_ROOT}/scripts/lib.sh"
  22. # XXX: we can't trap bash EXIT, because it'll override direnv's finalizing routines.
  23. # consequently, using "exit" anywhere will skip this notice from showing.
  24. # so need to use set -e, and return 1.
  25. trap notice ERR
  26. complete_success="yup"
  27. help_message() {
  28. cat <<EOF
  29. For more help run: make direnv-help
  30. EOF
  31. }
  32. failure_message() {
  33. cat <<EOF
  34. ${red}${bold}direnv wasn't able to complete execution.
  35. You may have been given some recommendations in the error message.
  36. Follow them, and then you'll need to re-run direnv by running "direnv allow".${reset}
  37. EOF
  38. help_message
  39. }
  40. notice() {
  41. [ $? -eq 0 ] && return
  42. failure_message
  43. }
  44. debug() {
  45. if [ "${SENTRY_DIRENV_DEBUG-}" ]; then
  46. echo -e "${@}"
  47. fi
  48. }
  49. info() {
  50. echo -e "${bold}${*}${reset}"
  51. }
  52. warn() {
  53. echo -e "${yellow}${*}${reset}" >&2
  54. complete_success="nope"
  55. }
  56. die() {
  57. echo -e "${red}${bold}FATAL: ${*}${reset}" >&2
  58. return 1
  59. }
  60. show_commands_info() {
  61. echo -e "\n${red}Run the following commands to bring your environment up-to-date:"
  62. for cmd in "${commands_to_run[@]}"; do
  63. warn " ${red}$cmd"
  64. done
  65. echo ""
  66. }
  67. ### Environment ###
  68. commands_to_run=()
  69. # Always write stdout immediately. Very helpful for debugging
  70. export PYTHONUNBUFFERED=1
  71. # make sure we don't have any conflicting PYTHONPATH
  72. unset PYTHONPATH
  73. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  74. export PIP_DISABLE_PIP_VERSION_CHECK=on
  75. # increase node's memory limit, required for our webpacking
  76. export NODE_OPTIONS=--max-old-space-size=4096
  77. # Frontend hot module reloader using `react-refresh`
  78. # Enable this by default for development envs (CI/deploys do not use envrc)
  79. export SENTRY_UI_HOT_RELOAD=1
  80. ### You can override the exported variables with a .env file
  81. # All exports should happen before here unless they're safeguarded (see devenv error reporting below)
  82. if [ -f "${SENTRY_ROOT}/.env" ]; then
  83. info "Loading variables from ${SENTRY_ROOT}/.env"
  84. dotenv "${SENTRY_ROOT}/.env"
  85. fi
  86. ## Notify of reporting to Sentry
  87. if [ -n "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  88. debug "No development environment errors will be reported (since you've defined SENTRY_DEVENV_NO_REPORT)."
  89. _SENTRY_LOG_FILE=''
  90. else
  91. # Since direnv traps the EXIT signal we place the temp file under /tmp for the odd time
  92. # the script will use the EXIT path
  93. _SENTRY_LOG_FILE=$(mktemp /tmp/sentry.envrc.$$.out || mktemp /tmp/sentry.envrc.XXXXXXXX.out)
  94. exec > >(tee "$_SENTRY_LOG_FILE")
  95. exec 2>&1
  96. debug "Development errors will be reported to Sentry.io. If you wish to opt-out, set SENTRY_DEVENV_NO_REPORT as an env variable."
  97. # This will allow `sentry devservices` errors to be reported
  98. export SENTRY_DEVSERVICES_DSN=https://23670f54c6254bfd9b7de106637808e9@o1.ingest.sentry.io/1492057
  99. fi
  100. ### System ###
  101. for pkg in \
  102. make \
  103. docker \
  104. chromedriver \
  105. openssl; do
  106. if ! require "$pkg"; then
  107. die "You seem to be missing the system dependency: ${pkg}
  108. Please install homebrew, and run brew bundle."
  109. fi
  110. done
  111. ### Python ###
  112. if [ -f .venv/bin/devenv ]; then
  113. DEVENV=.venv/bin/devenv
  114. else
  115. DEVENV=devenv
  116. fi
  117. export SENTRY_DEVENV_HOME="${SENTRY_DEVENV_HOME:-$XDG_DATA_HOME/sentry-devenv}"
  118. PATH_add "${SENTRY_DEVENV_HOME}/bin"
  119. if ! command -v "$DEVENV" >/dev/null; then
  120. die '
  121. Please install the devenv tool:
  122. https://github.com/getsentry/devenv#install
  123. '
  124. fi
  125. PATH_add .venv/bin
  126. export VIRTUAL_ENV="$PWD/.venv"
  127. if ! require sentry; then
  128. warn "Your virtualenv is activated, but sentry doesn't seem to be installed."
  129. commands_to_run+=("devenv sync")
  130. fi
  131. # this is the standard repo-local bin root
  132. PATH_add "${PWD}/.devenv/bin"
  133. ### pre-commit ###
  134. debug "Checking pre-commit..."
  135. if ! require pre-commit; then
  136. warn "Looks like you don't have pre-commit installed."
  137. commands_to_run+=("devenv sync")
  138. fi
  139. python3 -m tools.docker_memory_check
  140. ### Node ###
  141. debug "Checking node..."
  142. if ! require node; then
  143. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  144. fi
  145. make node-version-check
  146. if [ ! -x "node_modules/.bin/webpack" ]; then
  147. warn "You don't seem to have yarn packages installed."
  148. commands_to_run+=("devenv sync")
  149. fi
  150. PATH_add node_modules/.bin
  151. # These are commands that can take a significant amount of time
  152. if [ ${#commands_to_run[@]} -ne 0 ]; then
  153. show_commands_info
  154. fi
  155. if [ "${complete_success}" != "yup" ]; then
  156. help_message
  157. warn "\nPartial success. The virtualenv is active, however, you're not fully up-to-date (see messages above)."
  158. else
  159. echo "${green}${bold}SUCCESS!${reset}"
  160. fi
  161. # Since we can't use an EXIT routine we need to guarantee we delete the file here
  162. [ -z "$_SENTRY_LOG_FILE" ] || rm -f "$_SENTRY_LOG_FILE"