.envrc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. notice() {
  17. [ $? -eq 0 ] && return
  18. cat <<EOF
  19. ${red}${bold}direnv wasn't able to complete execution.
  20. You may have been given some recommendations in the error message.
  21. Follow them, and then you'll need to redo direnv by running "direnv allow".${reset}
  22. If you're stuck or have questions, ask in #discuss-dev-tooling.
  23. EOF
  24. }
  25. require() {
  26. command -v "$1" >/dev/null 2>&1
  27. }
  28. warn() {
  29. cat <<EOF
  30. ${yellow}${bold}direnv: ${@}${reset}
  31. EOF
  32. }
  33. info() {
  34. cat <<EOF
  35. ${bold}direnv: ${@}${reset}
  36. EOF
  37. }
  38. die() {
  39. cat >&2 <<EOF
  40. ${red}${bold}direnv FATAL: ${@}
  41. ${reset}
  42. EOF
  43. return 1
  44. }
  45. advice_init_venv() {
  46. venv_name="$1"
  47. deactivate 2>/dev/null || true
  48. info "To create a virtualenv, please type: python3.6 -m venv ${venv_name}"
  49. require "python3.6" ||
  50. die "You'll need to install python3, or make it available on your PATH.
  51. It's recommended to use pyenv - please refer to https://docs.sentry.io/development/contribute/environment"
  52. return 1
  53. }
  54. advice_install_sentry() {
  55. info "To install sentry, please type: make install-py-dev"
  56. return 1
  57. }
  58. advice_install_pre_commit() {
  59. info "To install pre-commit, please type: make setup-git"
  60. return 1
  61. }
  62. advice_install_yarn_pkgs() {
  63. info "To install yarn packages, please type: make install-js-dev"
  64. return 1
  65. }
  66. ### Environment ###
  67. # don't write *.pyc files; using stale python code occasionally causes subtle problems
  68. export PYTHONDONTWRITEBYTECODE=1
  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. ### System ###
  81. for pkg in \
  82. make \
  83. docker \
  84. chromedriver \
  85. pkg-config \
  86. openssl; do
  87. if ! require "$pkg"; then
  88. die "You seem to be missing the system dependency: ${pkg}
  89. Please install homebrew, and run brew bundle."
  90. fi
  91. done
  92. ### Git ###
  93. info "Configuring git..."
  94. make setup-git-config
  95. ### Python ###
  96. venv_name=".venv"
  97. info "Activating virtualenv..."
  98. if [ ! -f "${venv_name}/bin/activate" ]; then
  99. info "You don't seem to have a virtualenv."
  100. advice_init_venv "$venv_name"
  101. fi
  102. # The user might be cd'ing into sentry with another non-direnv managed
  103. # (in that it would be automatically deactivated) virtualenv active.
  104. deactivate 2>/dev/null || true
  105. source "${venv_name}/bin/activate"
  106. # XXX: ideally, direnv is able to export PS1 as modified by sourcing venvs
  107. # but we'd have to patch direnv, and ".venv" isn't descriptive anyways
  108. unset PS1
  109. info "Ensuring proper virtualenv..."
  110. ${PWD}/scripts/ensure-venv.sh
  111. if ! require sentry; then
  112. info "Your virtualenv is activated, but sentry doesn't seem to be installed."
  113. # XXX: if direnv fails, the venv won't be activated outside of direnv execution...
  114. # So, it is critical that make install-py-dev is guarded by scripts/ensure-venv.
  115. advice_install_sentry
  116. fi
  117. ### pre-commit ###
  118. info "Checking pre-commit..."
  119. if ! require pre-commit; then
  120. info "Looks like you don't have pre-commit installed."
  121. advice_install_pre_commit
  122. fi
  123. ### Node ###
  124. info "Checking node..."
  125. if ! require node; then
  126. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  127. fi
  128. make node-version-check
  129. if [ ! -x "node_modules/.bin/webpack" ]; then
  130. info "You don't seem to have yarn packages installed."
  131. advice_install_yarn_pkgs
  132. fi
  133. PATH_add node_modules/.bin
  134. ### Overrides ###
  135. if [ -f '.env' ]; then
  136. info ".env found. Reading it..."
  137. dotenv .env
  138. fi
  139. cat <<EOF
  140. ${green}${bold}direnv: SUCCESS!
  141. ${reset}
  142. EOF
  143. if [ -z "${SENTRY_SILENCE_DIRENV_NOTICE:-}" ]; then
  144. cat <<EOF
  145. If you're stuck or have questions, ask in #discuss-dev-tooling.
  146. EOF
  147. fi