.envrc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. # make sure we don't have any conflicting PYTHONPATH
  70. unset PYTHONPATH
  71. # don't check pypi for a potential new pip version; low-hanging fruit to save a bit of time
  72. export PIP_DISABLE_PIP_VERSION_CHECK=on
  73. # increase node's memory limit, required for our webpacking
  74. export NODE_OPTIONS=--max-old-space-size=4096
  75. # Frontend hot module reloader using `react-refresh`
  76. # Enable this by default for development envs (CI/deploys do not use envrc)
  77. export SENTRY_UI_HOT_RELOAD=1
  78. ### System ###
  79. for pkg in \
  80. make \
  81. docker \
  82. chromedriver \
  83. pkg-config \
  84. openssl; do
  85. if ! require "$pkg"; then
  86. die "You seem to be missing the system dependency: ${pkg}
  87. Please install homebrew, and run brew bundle."
  88. fi
  89. done
  90. ### Git ###
  91. info "Configuring git..."
  92. make setup-git-config
  93. ### Python ###
  94. venv_name=".venv"
  95. info "Activating virtualenv..."
  96. if [ ! -f "${venv_name}/bin/activate" ]; then
  97. info "You don't seem to have a virtualenv."
  98. advice_init_venv "$venv_name"
  99. fi
  100. # The user might be cd'ing into sentry with another non-direnv managed
  101. # (in that it would be automatically deactivated) virtualenv active.
  102. deactivate 2>/dev/null || true
  103. source "${venv_name}/bin/activate"
  104. # XXX: ideally, direnv is able to export PS1 as modified by sourcing venvs
  105. # but we'd have to patch direnv, and ".venv" isn't descriptive anyways
  106. unset PS1
  107. info "Ensuring proper virtualenv..."
  108. ${PWD}/scripts/ensure-venv.sh
  109. if ! require sentry; then
  110. info "Your virtualenv is activated, but sentry doesn't seem to be installed."
  111. # XXX: if direnv fails, the venv won't be activated outside of direnv execution...
  112. # So, it is critical that make install-py-dev is guarded by scripts/ensure-venv.
  113. advice_install_sentry
  114. fi
  115. ### pre-commit ###
  116. info "Checking pre-commit..."
  117. if ! require pre-commit; then
  118. info "Looks like you don't have pre-commit installed."
  119. advice_install_pre_commit
  120. fi
  121. ### Node ###
  122. info "Checking node..."
  123. if ! require node; then
  124. die "You don't seem to have node installed. Install volta (a node version manager): https://develop.sentry.dev/environment/#javascript"
  125. fi
  126. make node-version-check
  127. if [ ! -x "node_modules/.bin/webpack" ]; then
  128. info "You don't seem to have yarn packages installed."
  129. advice_install_yarn_pkgs
  130. fi
  131. PATH_add node_modules/.bin
  132. ### Overrides ###
  133. if [ -f '.env' ]; then
  134. info ".env found. Reading it..."
  135. dotenv .env
  136. fi
  137. cat <<EOF
  138. ${green}${bold}direnv: SUCCESS!
  139. ${reset}
  140. EOF
  141. if [ -z "${SENTRY_SILENCE_DIRENV_NOTICE:-}" ]; then
  142. cat <<EOF
  143. If you're stuck or have questions, ask in #discuss-dev-tooling.
  144. EOF
  145. fi