.envrc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. bold="$(tput bold)"
  8. red="$(tput setaf 1)"
  9. green="$(tput setaf 2)"
  10. yellow="$(tput setaf 3)"
  11. reset="$(tput sgr0)"
  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. log_level="info"
  20. notice() {
  21. [ $? -eq 0 ] && return
  22. cat <<EOF
  23. ${red}${bold}direnv wasn't able to complete execution.
  24. You may have been given some recommendations in the error message.
  25. Follow them, and then you'll need to redo direnv by running "direnv allow".${reset}
  26. If you're stuck or have questions, ask in #discuss-dev-tooling.
  27. EOF
  28. [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ] && report_to_sentry
  29. }
  30. report_to_sentry() {
  31. if ! require sentry-cli; then
  32. curl -sL https://sentry.io/get-cli/ | bash
  33. fi
  34. # Report to sentry-dev-env
  35. SENTRY_DSN="https://9bdb053cb8274ea69231834d1edeec4c@o1.ingest.sentry.io/5723503" \
  36. sentry-cli send-event -m "$error_message" --logfile "$_SENTRY_LOG_FILE" --level $log_level
  37. rm "$_SENTRY_LOG_FILE"
  38. }
  39. require() {
  40. command -v "$1" >/dev/null 2>&1
  41. }
  42. warn() {
  43. cat <<EOF
  44. ${yellow}${bold}direnv: ${@}${reset}
  45. EOF
  46. }
  47. info() {
  48. cat <<EOF
  49. ${bold}direnv: ${@}${reset}
  50. EOF
  51. # info() calls happen multiple times in this script. The last info statement
  52. # is the one that will be used to group Sentry issues
  53. error_message=("${@}")
  54. }
  55. die() {
  56. cat >&2 <<EOF
  57. ${red}${bold}direnv FATAL: ${@}
  58. ${reset}
  59. EOF
  60. # When reporting to Sentry, this will allow grouping the errors differently
  61. # NOTE: The first line of the output is used to group issues
  62. error_message=("${@}")
  63. log_level="error"
  64. return 1
  65. }
  66. advice_init_venv() {
  67. venv_name="$1"
  68. deactivate 2>/dev/null || true
  69. info "To create a virtualenv, please type: python3.6 -m venv ${venv_name}"
  70. require "python3.6" ||
  71. die "You'll need to install python3, or make it available on your PATH.
  72. It's recommended to use pyenv - please refer to https://docs.sentry.io/development/contribute/environment"
  73. return 1
  74. }
  75. advice_install_sentry() {
  76. info "To install sentry, please type: make install-py-dev"
  77. return 1
  78. }
  79. advice_install_pre_commit() {
  80. info "To install pre-commit, please type: make setup-git"
  81. return 1
  82. }
  83. advice_install_yarn_pkgs() {
  84. info "To install yarn packages, please type: make install-js-dev"
  85. return 1
  86. }
  87. ### Environment ###
  88. # don't write *.pyc files; using stale python code occasionally causes subtle problems
  89. export PYTHONDONTWRITEBYTECODE=1
  90. # Always write stdout immediately. Very helpful for debugging
  91. export PYTHONUNBUFFERED=1
  92. # make sure we don't have any conflicting PYTHONPATH
  93. unset PYTHONPATH
  94. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  95. export PIP_DISABLE_PIP_VERSION_CHECK=on
  96. # increase node's memory limit, required for our webpacking
  97. export NODE_OPTIONS=--max-old-space-size=4096
  98. # Frontend hot module reloader using `react-refresh`
  99. # Enable this by default for development envs (CI/deploys do not use envrc)
  100. export SENTRY_UI_HOT_RELOAD=1
  101. ### You can override the exported variables with a .env file
  102. # All exports should happen before here unless they're safeguarded (see devenv error reporting below)
  103. if [ -f '.env' ]; then
  104. info ".env found. Reading it..."
  105. dotenv .env
  106. fi
  107. ## Notify of reporting to Sentry
  108. if [ -n "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  109. info "No development environment errors will be reported (since you've defined SENTRY_DEVENV_NO_REPORT)."
  110. else
  111. # Since direnv traps the EXIT signal we place the temp file under /tmp for the odd time
  112. # the script will use the EXIT path
  113. _SENTRY_LOG_FILE=$(mktemp /tmp/sentry.envrc.$$.out || mktemp /tmp/sentry.envrc.XXXXXXXX.out)
  114. exec > >(tee "$_SENTRY_LOG_FILE")
  115. exec 2>&1
  116. info "Development errors will be reported to Sentry.io."$'\n'" If you wish to opt-out, set SENTRY_DEVENV_NO_REPORT as an env variable."
  117. # This will allow `sentry devservices` errors to be reported
  118. export SENTRY_DEVSERVICES_DSN=https://23670f54c6254bfd9b7de106637808e9@o1.ingest.sentry.io/1492057
  119. fi
  120. ### System ###
  121. for pkg in \
  122. make \
  123. docker \
  124. chromedriver \
  125. pkg-config \
  126. pyenv \
  127. openssl; do
  128. if ! require "$pkg"; then
  129. die "You seem to be missing the system dependency: ${pkg}
  130. Please install homebrew, and run brew bundle."
  131. fi
  132. done
  133. ### Git ###
  134. info "Configuring git..."
  135. make setup-git-config
  136. ### Python ###
  137. venv_name=".venv"
  138. info "Activating virtualenv..."
  139. if [ ! -f "${venv_name}/bin/activate" ]; then
  140. info "You don't seem to have a virtualenv."
  141. # If the version doesn't match the contents of .python-version it will fail
  142. [[ $(pyenv version) ]] ||
  143. die "pyenv does not have the right Python version installed. Please run:
  144. make setup-pyenv && python3.6 -m venv ${venv_name}"
  145. advice_init_venv "$venv_name"
  146. fi
  147. # The user might be cd'ing into sentry with another non-direnv managed
  148. # (in that it would be automatically deactivated) virtualenv active.
  149. deactivate 2>/dev/null || true
  150. source "${venv_name}/bin/activate"
  151. # XXX: ideally, direnv is able to export PS1 as modified by sourcing venvs
  152. # but we'd have to patch direnv, and ".venv" isn't descriptive anyways
  153. unset PS1
  154. info "Ensuring proper virtualenv..."
  155. ${PWD}/scripts/ensure-venv.sh
  156. if ! require sentry; then
  157. info "Your virtualenv is activated, but sentry doesn't seem to be installed."
  158. # XXX: if direnv fails, the venv won't be activated outside of direnv execution...
  159. # So, it is critical that make install-py-dev is guarded by scripts/ensure-venv.
  160. advice_install_sentry
  161. fi
  162. ### pre-commit ###
  163. info "Checking pre-commit..."
  164. if ! require pre-commit; then
  165. info "Looks like you don't have pre-commit installed."
  166. advice_install_pre_commit
  167. fi
  168. ### Node ###
  169. info "Checking node..."
  170. if ! require node; then
  171. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  172. fi
  173. make node-version-check
  174. if [ ! -x "node_modules/.bin/webpack" ]; then
  175. info "You don't seem to have yarn packages installed."
  176. advice_install_yarn_pkgs
  177. fi
  178. PATH_add node_modules/.bin
  179. ### Overrides ###
  180. cat <<EOF
  181. ${green}${bold}direnv: SUCCESS!
  182. ${reset}
  183. EOF
  184. if [ -z "${SENTRY_SILENCE_DIRENV_NOTICE:-}" ]; then
  185. cat <<EOF
  186. If you're stuck or have questions, ask in #discuss-dev-tooling.
  187. EOF
  188. fi
  189. # Since we can't use an EXIT routine we need to guarantee we delete the file here
  190. rm -f "$_SENTRY_LOG_FILE"