.envrc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. direnv tooling is in an ALPHA state!
  23. If you're having trouble, or have questions, please ask in #discuss-dev-tooling
  24. and/or reach out to @josh.
  25. EOF
  26. }
  27. require() {
  28. command -v "$1" 2>&1 >/dev/null
  29. }
  30. warn() {
  31. cat <<EOF
  32. ${yellow}${bold}direnv: ${@}${reset}
  33. EOF
  34. }
  35. info() {
  36. cat <<EOF
  37. ${bold}direnv: ${@}${reset}
  38. EOF
  39. }
  40. die() {
  41. cat >&2 <<EOF
  42. ${red}${bold}direnv FATAL: ${@}
  43. ${reset}
  44. EOF
  45. return 1
  46. }
  47. advice_init_venv() {
  48. python_version="$1"
  49. venv_name="$2"
  50. deactivate 2>/dev/null || true
  51. info "To create a virtualenv, please type: ${python_version} -m virtualenv ${venv_name}"
  52. require ${python_version} ||
  53. die "You'll need to install ${python_version}, or make it available on your PATH.
  54. It's recommended to use pyenv - please refer to https://docs.sentry.io/development/contribute/environment"
  55. return 1
  56. }
  57. advice_install_sentry() {
  58. info "To install sentry, please type: make install-py-dev"
  59. return 1
  60. }
  61. advice_install_pre_commit() {
  62. info "To install pre-commit, please type: make setup-git"
  63. return 1
  64. }
  65. advice_install_yarn_pkgs() {
  66. info "To install yarn packages, please type: make install-js-dev"
  67. return 1
  68. }
  69. ### Environment ###
  70. # don't write *.pyc files; using stale python code occasionally causes subtle problems
  71. export PYTHONDONTWRITEBYTECODE=1
  72. # make sure we don't have any conflicting PYTHONPATH
  73. unset PYTHONPATH
  74. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  75. export PIP_DISABLE_PIP_VERSION_CHECK=on
  76. # increase node's memory limit, required for our webpacking
  77. export NODE_OPTIONS=--max-old-space-size=4096
  78. # Frontend hot module reloader using `react-refresh`
  79. # Enable this by default for development envs (CI/deploys do not use envrc)
  80. export SENTRY_UI_HOT_RELOAD=1
  81. ### System ###
  82. for pkg in \
  83. make \
  84. docker \
  85. chromedriver \
  86. pkg-config \
  87. openssl; do
  88. if ! require "$pkg"; then
  89. die "You seem to be missing the system dependency: ${pkg}
  90. Please install homebrew, and run brew bundle."
  91. fi
  92. done
  93. ### Git ###
  94. info "Configuring git..."
  95. make setup-git-config
  96. ### Python ###
  97. # we're enforcing that virtualenv be in .venv{,3}, since future tooling e.g.
  98. # venv-update will rely on this.
  99. venv_name=".venv"
  100. python_version="python2.7"
  101. if [ "$SENTRY_PYTHON3" = "1" ]; then
  102. venv_name=".venv3"
  103. python_version="python3.6"
  104. warn "Running in EXPERIMENTAL Python 3 mode... Good luck"
  105. fi
  106. info "Activating virtualenv..."
  107. if [ ! -f "${venv_name}/bin/activate" ]; then
  108. info "You don't seem to have a virtualenv."
  109. advice_init_venv "$python_version" "$venv_name"
  110. fi
  111. # The user might be cd'ing into sentry with another non-direnv managed
  112. # (in that it would be automatically deactivated) virtualenv active.
  113. deactivate 2>/dev/null || true
  114. source ${venv_name}/bin/activate
  115. # XXX: ideally, direnv is able to export PS1 as modified by sourcing venvs
  116. # but we'd have to patch direnv, and ".venv" isn't descriptive anyways
  117. unset PS1
  118. # We're explicitly disallowing symlinked venvs (python would resolve to the canonical location)
  119. if [ "$(command -v python)" != "${PWD}/${venv_name}/bin/python" ]; then
  120. info "Failed to activate virtualenv. Your virtualenv's probably symlinked." \
  121. "We want everyone to be on the same page here, so you'll have to recreate your virtualenv."
  122. advice_init_venv "$python_version" "$venv_name"
  123. fi
  124. if [ "$SENTRY_PYTHON3" = "1" ]; then
  125. python -c "import sys; sys.exit(sys.version_info[:2] != (3, 6))" ||
  126. die "For some reason, the virtualenv isn't Python 3.6."
  127. else
  128. python -c "import sys; sys.exit(sys.version_info[:2] != (2, 7))" ||
  129. die "For some reason, the virtualenv isn't Python 2.7."
  130. fi
  131. if [ "$(command -v sentry)" != "${PWD}/${venv_name}/bin/sentry" ]; then
  132. info "Your virtual env is activated, but sentry doesn't seem to be installed."
  133. # XXX: if direnv fails, the venv won't be activated outside of direnv execution...
  134. # So, it is critical that make install-py-dev is guarded by scripts/ensure-venv.
  135. advice_install_sentry
  136. fi
  137. ### pre-commit ###
  138. info "Checking pre-commit..."
  139. if ! require pre-commit; then
  140. info "Looks like you don't have pre-commit installed."
  141. advice_install_pre_commit
  142. fi
  143. ### Node ###
  144. info "Checking node..."
  145. if ! require node; then
  146. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  147. fi
  148. make node-version-check
  149. if [ ! -x "node_modules/.bin/webpack" ]; then
  150. info "You don't seem to have yarn packages installed."
  151. advice_install_yarn_pkgs
  152. fi
  153. PATH_add node_modules/.bin
  154. ### Overrides ###
  155. if [ -f '.env' ]; then
  156. info ".env found. Reading it..."
  157. dotenv .env
  158. fi
  159. cat <<EOF
  160. ${green}${bold}direnv: SUCCESS!
  161. ${reset}
  162. EOF
  163. if [ ! -n "${SENTRY_SILENCE_DIRENV_NOTICE:-}" ]; then
  164. cat <<EOF
  165. direnv tooling is in an ALPHA state!
  166. If you're having trouble, or have questions, please ask in #discuss-dev-tooling
  167. and/or reach out to @josh.
  168. EOF
  169. fi