.envrc 8.0 KB

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