.envrc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. # This is used to group issues on Sentry.io.
  27. # If an issue does not call info() or die() it will be grouped under this
  28. error_message="Unknown issue"
  29. # This has to be the same value as what sentry-cli accepts
  30. log_level="info"
  31. help_message() {
  32. cat <<EOF
  33. For more help run: make direnv-help
  34. EOF
  35. }
  36. failure_message() {
  37. cat <<EOF
  38. ${red}${bold}direnv wasn't able to complete execution.
  39. You may have been given some recommendations in the error message.
  40. Follow them, and then you'll need to re-run direnv by running "direnv allow".${reset}
  41. EOF
  42. help_message
  43. }
  44. notice() {
  45. [ $? -eq 0 ] && return
  46. failure_message
  47. [ -z "${SENTRY_DEVENV_NO_REPORT+x}" ] && report_to_sentry
  48. }
  49. report_to_sentry() {
  50. if ! require sentry-cli; then
  51. curl -sL https://sentry.io/get-cli/ | bash
  52. fi
  53. # Report to sentry-dev-env project
  54. SENTRY_DSN="https://9bdb053cb8274ea69231834d1edeec4c@o1.ingest.sentry.io/5723503" \
  55. sentry-cli send-event -m "$error_message" --logfile "$_SENTRY_LOG_FILE" --level "$log_level"
  56. rm "$_SENTRY_LOG_FILE"
  57. }
  58. debug() {
  59. if [ "${SENTRY_DIRENV_DEBUG-}" ]; then
  60. echo -e "${@}"
  61. fi
  62. }
  63. info() {
  64. echo -e "${bold}${*}${reset}"
  65. }
  66. warn() {
  67. echo -e "${yellow}${*}${reset}" >&2
  68. log_level="warning"
  69. }
  70. die() {
  71. echo -e "${red}${bold}FATAL: ${*}${reset}" >&2
  72. # When reporting to Sentry, this will allow grouping the errors differently
  73. # NOTE: The first line of the output is used to group issues
  74. error_message=("${@}")
  75. log_level="error"
  76. return 1
  77. }
  78. show_commands_info() {
  79. echo -e "\n${red}Run the following commands to bring your environment up-to-date:"
  80. for cmd in "${commands_to_run[@]}"; do
  81. warn " ${red}$cmd"
  82. done
  83. echo ""
  84. }
  85. ### Environment ###
  86. commands_to_run=()
  87. # Always write stdout immediately. Very helpful for debugging
  88. export PYTHONUNBUFFERED=1
  89. # make sure we don't have any conflicting PYTHONPATH
  90. unset PYTHONPATH
  91. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  92. export PIP_DISABLE_PIP_VERSION_CHECK=on
  93. # increase node's memory limit, required for our webpacking
  94. export NODE_OPTIONS=--max-old-space-size=4096
  95. # Frontend hot module reloader using `react-refresh`
  96. # Enable this by default for development envs (CI/deploys do not use envrc)
  97. export SENTRY_UI_HOT_RELOAD=1
  98. ### You can override the exported variables with a .env file
  99. # All exports should happen before here unless they're safeguarded (see devenv error reporting below)
  100. if [ -f "${SENTRY_ROOT}/.env" ]; then
  101. info "Loading variables from ${SENTRY_ROOT}/.env"
  102. dotenv "${SENTRY_ROOT}/.env"
  103. fi
  104. ## Notify of reporting to Sentry
  105. if [ -n "${SENTRY_DEVENV_NO_REPORT+x}" ]; then
  106. debug "No development environment errors will be reported (since you've defined SENTRY_DEVENV_NO_REPORT)."
  107. _SENTRY_LOG_FILE=''
  108. else
  109. # Since direnv traps the EXIT signal we place the temp file under /tmp for the odd time
  110. # the script will use the EXIT path
  111. _SENTRY_LOG_FILE=$(mktemp /tmp/sentry.envrc.$$.out || mktemp /tmp/sentry.envrc.XXXXXXXX.out)
  112. exec > >(tee "$_SENTRY_LOG_FILE")
  113. exec 2>&1
  114. debug "Development errors will be reported to Sentry.io. If you wish to opt-out, set SENTRY_DEVENV_NO_REPORT as an env variable."
  115. # This will allow `sentry devservices` errors to be reported
  116. export SENTRY_DEVSERVICES_DSN=https://23670f54c6254bfd9b7de106637808e9@o1.ingest.sentry.io/1492057
  117. fi
  118. # We can remove these lines in few months
  119. if [ "$SHELL" == "/bin/zsh" ]; then
  120. zshrc_path="${HOME}/.zshrc"
  121. header="# Apple M1 environment variables"
  122. if grep -qF "${header}" "${zshrc_path}"; then
  123. echo -e "\n${red}Please delete from ${zshrc_path}, the following three lines:${reset}"
  124. echo -e "${header}
  125. export CPATH=/opt/homebrew/Cellar/librdkafka/1.8.2/include
  126. export LDFLAGS=-L/opt/homebrew/Cellar/gettext/0.21/lib"
  127. echo -e "\nWe have moved exporting of these variables to the right place."
  128. return 1
  129. fi
  130. fi
  131. ### System ###
  132. for pkg in \
  133. make \
  134. docker \
  135. chromedriver \
  136. openssl; do
  137. if ! require "$pkg"; then
  138. die "You seem to be missing the system dependency: ${pkg}
  139. Please install homebrew, and run brew bundle."
  140. fi
  141. done
  142. ### Git ###
  143. debug "Configuring git..."
  144. make setup-git-config
  145. ### Python ###
  146. if [ -f .venv/bin/devenv ]; then
  147. DEVENV=.venv/bin/devenv
  148. else
  149. DEVENV=devenv
  150. fi
  151. export SENTRY_DEVENV_HOME="${SENTRY_DEVENV_HOME:-$XDG_DATA_HOME/sentry-devenv}"
  152. PATH_add "${SENTRY_DEVENV_HOME}/bin"
  153. if ! command -v "$DEVENV" >/dev/null; then
  154. die '
  155. Please install the devenv tool:
  156. https://github.com/getsentry/devenv#install
  157. '
  158. fi
  159. PATH_add .venv/bin
  160. export VIRTUAL_ENV="$PWD/.venv"
  161. if ! require sentry; then
  162. warn "Your virtualenv is activated, but sentry doesn't seem to be installed."
  163. commands_to_run+=("devenv sync")
  164. fi
  165. ### pre-commit ###
  166. debug "Checking pre-commit..."
  167. if ! require pre-commit; then
  168. warn "Looks like you don't have pre-commit installed."
  169. commands_to_run+=("make setup-git")
  170. fi
  171. python3 -m tools.docker_memory_check
  172. ### Node ###
  173. debug "Checking node..."
  174. if ! require node; then
  175. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  176. fi
  177. make node-version-check
  178. if [ ! -x "node_modules/.bin/webpack" ]; then
  179. warn "You don't seem to have yarn packages installed."
  180. commands_to_run+=("make install-js-dev")
  181. fi
  182. PATH_add node_modules/.bin
  183. # These are commands that can take a significant amount of time
  184. if [ ${#commands_to_run[@]} -ne 0 ]; then
  185. show_commands_info
  186. fi
  187. if [ "${log_level}" != "info" ]; then
  188. help_message
  189. warn "\nPartial success. The virtualenv is active, however, you're not fully up-to-date (see messages above)."
  190. else
  191. echo "${green}${bold}SUCCESS!${reset}"
  192. fi
  193. # Since we can't use an EXIT routine we need to guarantee we delete the file here
  194. [ -z "$_SENTRY_LOG_FILE" ] || rm -f "$_SENTRY_LOG_FILE"