tests.yaml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. name: test
  5. on:
  6. push:
  7. branches: [trunk, action-test]
  8. pull_request:
  9. merge_group:
  10. permissions:
  11. contents: read # For actions/checkout.
  12. pull-requests: read # For dorny/paths-filter to read pull requests.
  13. # Cancel previous workflows on the PR when there are multiple fast commits.
  14. # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency
  15. concurrency:
  16. group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  17. cancel-in-progress: true
  18. jobs:
  19. test:
  20. strategy:
  21. matrix:
  22. # On PRs and in the merge queue test a recent version of each supported
  23. # OS. On push (post-submit), also run on `macos-12` to get Intel macOS
  24. # coverage.
  25. runner:
  26. ${{ fromJSON(github.event_name != 'push' && '["ubuntu-22.04",
  27. "macos-14"]' || '["ubuntu-22.04", "macos-14", "macos-12"]') }}
  28. build_mode: [fastbuild, opt]
  29. include:
  30. # The clang-tidy config doesn't work on macos (missing `truncate`).
  31. - runner: ubuntu-22.04
  32. build_mode: clang-tidy
  33. runs-on: ${{ matrix.runner }}
  34. env:
  35. os: ${{ startsWith(matrix.runner, 'ubuntu') && 'ubuntu' || 'macos' }}
  36. steps:
  37. - name: Harden Runner
  38. uses: step-security/harden-runner@63c24ba6bd7ba022e95695ff85de572c04a18142 # v2.7.0
  39. with:
  40. egress-policy: audit
  41. # Ubuntu images start with 23GB available, and this adds 14GB more. For
  42. # comparison, MacOS images have >100GB free.
  43. #
  44. # Although we could delete more, if we run into a limit, not deleting
  45. # everything provides a little flexibility to get space while trying
  46. # to shrink the build.
  47. - name: Free up disk space (Ubuntu)
  48. if: env.os == 'ubuntu'
  49. uses: jlumbroso/free-disk-space@54081f138730dfa15788a46383842cd2f914a1be # v1.3.1
  50. with:
  51. android: true
  52. dotnet: true
  53. haskell: true
  54. # Checkout the pull request head or the branch.
  55. - name: Checkout pull request
  56. if: github.event_name == 'pull_request'
  57. uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
  58. with:
  59. ref: ${{ github.event.pull_request.head.sha }}
  60. - name: Checkout branch
  61. if: github.event_name != 'pull_request'
  62. uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
  63. # Tests should only run on applicable paths, but we still need to have an
  64. # action run for the merge queue. We filter steps based on the paths here,
  65. # and condition steps on the output.
  66. - id: filter
  67. uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
  68. with:
  69. filters: |
  70. has_code:
  71. - '!{**/*.md,LICENSE,CODEOWNERS,.git*}'
  72. # Setup Python and related tools.
  73. - uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
  74. if: steps.filter.outputs.has_code == 'true'
  75. with:
  76. # Match the min version listed in docs/project/contribution_tools.md
  77. # or the oldest version available on the OS.
  78. python-version: ${{ matrix.runner == 'macos-14' && '3.11' || '3.9' }}
  79. # Install and cache LLVM 16 from Homebrew.
  80. # TODO: We can potentially remove this and simplify things when the
  81. # Homebrew version of LLVM updates to 16 here:
  82. # https://github.com/actions/runner-images/blob/main/images/macos/macos-12-Readme.md
  83. - name: Cache Homebrew (macOS)
  84. if: steps.filter.outputs.has_code == 'true' && env.os == 'macos'
  85. id: cache-homebrew-macos
  86. uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
  87. env:
  88. cache-name: cache-homebrew
  89. with:
  90. # Cover all the critical parts of Homebrew here. Homebrew on Arm macOS
  91. # uses its own prefix making this easy to cover, but we need a few
  92. # different paths for Intel.
  93. path: |
  94. ${{
  95. runner.arch == 'ARM64' && '/opt/homebrew' ||
  96. '
  97. /usr/local/Homebrew
  98. /usr/local/Cellar
  99. /usr/local/Frameworks
  100. /usr/local/bin
  101. /usr/local/opt
  102. '
  103. }}
  104. # Note the key needs to include all the packages we're adding.
  105. key: Homebrew-Cache-${{ matrix.runner }}-${{ runner.arch }}
  106. - name: Install LLVM and Clang with Homebrew (macOS)
  107. if:
  108. steps.filter.outputs.has_code == 'true' && env.os == 'macos' &&
  109. steps.cache-homebrew-macos.outputs.cache-hit != 'true'
  110. run: |
  111. echo '*** Prune brew leaves'
  112. # We prune all the leaf packages to have a minimal environment. This
  113. # both minimizes the install space and avoids accidental dependencies
  114. # on installed packages.
  115. brew leaves
  116. LEAVES=$(brew leaves | egrep -v '^(bazelisk|gh|git|git-lfs|gnu-tar|go@.*|jq|pipx|node@.*|openssl@.*|wget|yq|zlib)$')
  117. brew uninstall -f --ignore-dependencies $LEAVES
  118. echo '*** Installing LLVM deps'
  119. brew install --force-bottle --only-dependencies llvm@16
  120. echo '*** Installing LLVM itself'
  121. brew install --force-bottle --force --verbose llvm@16
  122. echo '*** brew info llvm@16'
  123. brew info llvm@16
  124. echo '*** brew autoremove'
  125. brew autoremove
  126. echo '*** brew info'
  127. brew info
  128. echo '*** brew leaves'
  129. brew leaves
  130. echo '*** brew config'
  131. brew config
  132. - name: Setup LLVM and Clang (macOS)
  133. if: steps.filter.outputs.has_code == 'true' && env.os == 'macos'
  134. run: |
  135. LLVM_PATH="$(brew --prefix llvm@16)"
  136. echo "Using ${LLVM_PATH}"
  137. echo "${LLVM_PATH}/bin" >> $GITHUB_PATH
  138. echo '*** ls "${LLVM_PATH}"'
  139. ls "${LLVM_PATH}"
  140. echo '*** ls "${LLVM_PATH}/bin"'
  141. ls "${LLVM_PATH}/bin"
  142. # Cache and install a recent version of LLVM. This uses the GitHub action
  143. # cache to avoid directly downloading on each iteration and improve
  144. # reliability.
  145. - name: Cache LLVM and Clang installation (Ubuntu)
  146. if: steps.filter.outputs.has_code == 'true' && env.os == 'ubuntu'
  147. id: cache-llvm-ubuntu
  148. uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
  149. env:
  150. cache-name: cache-llvm
  151. with:
  152. path: ~/llvm
  153. key: LLVM-16-Cache-${{ env.os }}-${{ runner.arch }}
  154. - name: Download LLVM and Clang installation (Ubuntu)
  155. if:
  156. steps.filter.outputs.has_code == 'true' && env.os == 'ubuntu' &&
  157. steps.cache-llvm-ubuntu.outputs.cache-hit != 'true'
  158. run: |
  159. cd ~
  160. LLVM_RELEASE=clang+llvm-16.0.4-x86_64-linux-gnu-ubuntu-22.04
  161. echo "*** Downloading $LLVM_RELEASE"
  162. wget --show-progress=off "https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.4/$LLVM_RELEASE.tar.xz"
  163. echo "*** Extracting $LLVM_RELEASE"
  164. tar -xJf "$LLVM_RELEASE.tar.xz"
  165. echo "*** Moving to 'llvm'"
  166. mv "$LLVM_RELEASE" llvm
  167. echo "*** Testing `clang++ --version`"
  168. ~/llvm/bin/clang++ --version
  169. # The installation contains *huge* parts of LLVM we don't need for the
  170. # toolchain. Prune them here to keep our cache small.
  171. echo "*** Cleaning the 'llvm' directory"
  172. rm llvm/lib/{*.a,*.so,*.so.*,*.bc}
  173. rm llvm/bin/{flang-*,mlir-*,clang-{scan-deps,check,repl},*-test,llvm-{lto*,reduce,bolt*,exegesis,jitlink},bugpoint,opt,llc}
  174. echo "*** Size of the 'llvm' directory"
  175. du -hs llvm
  176. - name: Setup LLVM and Clang paths (Ubuntu)
  177. if: steps.filter.outputs.has_code == 'true' && env.os == 'ubuntu'
  178. run: |
  179. LLVM_PATH=~/llvm
  180. echo "Using ${LLVM_PATH}"
  181. echo "${LLVM_PATH}/bin" >> $GITHUB_PATH
  182. echo '*** ls "${LLVM_PATH}"'
  183. ls "${LLVM_PATH}"
  184. echo '*** ls "${LLVM_PATH}/bin"'
  185. ls "${LLVM_PATH}/bin"
  186. # Print the various tool paths and versions to help in debugging.
  187. - name: Print tool debugging info
  188. if: steps.filter.outputs.has_code == 'true'
  189. run: |
  190. echo '*** PATH'
  191. echo $PATH
  192. echo '*** bazelisk'
  193. which bazelisk
  194. bazelisk --version
  195. echo '*** run_bazel.py'
  196. ./scripts/run_bazel.py --version
  197. echo '*** python'
  198. which python
  199. python --version
  200. echo '*** clang'
  201. which clang
  202. clang --version
  203. echo '*** clang++'
  204. which clang++
  205. clang++ --version
  206. # Disable uploads when the remote cache is read-only.
  207. - name: Set up remote cache access (read-only)
  208. if:
  209. steps.filter.outputs.has_code == 'true' && github.event_name ==
  210. 'pull_request'
  211. run: |
  212. echo "remote_cache_upload=--remote_upload_local_results=false" \
  213. >> $GITHUB_ENV
  214. # Provide a cache key when the remote cache is read-write.
  215. - name: Set up remote cache access (read-write)
  216. if:
  217. steps.filter.outputs.has_code == 'true' && github.event_name !=
  218. 'pull_request'
  219. env:
  220. REMOTE_CACHE_KEY: ${{ secrets.CARBON_BUILDS_GITHUB }}
  221. run: |
  222. echo "$REMOTE_CACHE_KEY" | base64 -d > $HOME/remote_cache_key.json
  223. echo "remote_cache_upload=--google_credentials=$HOME/remote_cache_key.json" \
  224. >> $GITHUB_ENV
  225. # Add our bazel configuration and print basic info to ease debugging.
  226. - name: Configure Bazel and print info
  227. if: steps.filter.outputs.has_code == 'true'
  228. env:
  229. # Add a cache version for changes that bazel won't otherwise detect,
  230. # like llvm version changes.
  231. CACHE_VERSION: 1
  232. run: |
  233. cat >user.bazelrc <<EOF
  234. # Enable remote cache for our CI but minimize downloads.
  235. build --remote_cache=https://storage.googleapis.com/carbon-builds-github-v${CACHE_VERSION}
  236. build --remote_download_minimal
  237. # We import a special key into every action in order to key the Bazel
  238. # remote cache in a way that avoids collisions between different
  239. # runners. Anything that might change the system external to Bazel but
  240. # not be fully captured by the sand-boxing of the build should be used
  241. # as part of the key. We don't need to use the CPU target for example,
  242. # as that is captured by Bazel's configuration of each action. And the
  243. # Clang version is incorporated by our Clang toolchain setup. But we
  244. # do need to capture any differences between GitHub runner OSes that
  245. # don't impact the Bazel configuration to avoid collisions between
  246. # those.
  247. build --action_env=BAZEL_REMOTE_CACHE_KEY=github-action-${{ matrix.runner }}
  248. build ${{ env.remote_cache_upload }}
  249. # Set an artificially high jobs count. This flag controls the number
  250. # of concurrency Bazel itself uses, which is essential for actions
  251. # that are internally blocked on for example downloading results form
  252. # the cache above. Without setting this high, Bazel will pick a small
  253. # number based on the available host CPUs and the reality will be a
  254. # long chain of largely serialized download events with little or no
  255. # usage of the host machine. Fortunately, local actions are
  256. # *separately* gated on '--local_*_resources' that will avoid a large
  257. # jobs value overwhelming the host. There is a bug to make downloads
  258. # behave completely asynchronously and remove the need for this filed
  259. # back in 2018 but work seemed to not finish:
  260. # https://github.com/bazelbuild/bazel/issues/6394
  261. #
  262. # There is a new effort (yay!) but until then it seems worth using the
  263. # workaround of a high jobs value. The biggest downside (increased
  264. # heap usage) seems like it isn't currently a big loss for our builds.
  265. #
  266. # Higher values like 50 have led to CI failures with network errors
  267. # and IOExceptions, see
  268. # https://discord.com/channels/655572317891461132/707150492370862090/1151605725576056934
  269. build --jobs=32
  270. # Avoid any cache impact from build stamping in CI.
  271. build --nostamp
  272. # General build options.
  273. build --verbose_failures
  274. test --test_output=errors
  275. EOF
  276. ./scripts/run_bazel.py info
  277. # Just for visibility, print space before and after the build.
  278. - name: Disk space before build
  279. if: steps.filter.outputs.has_code == 'true'
  280. run: df -h
  281. - name: Verify MODULE.bazel.lock
  282. if: steps.filter.outputs.has_code == 'true'
  283. run: |
  284. exit_code=0
  285. ./scripts/run_bazel.py \
  286. --attempts=5 \
  287. mod deps --lockfile_mode=error || exit_code=$?
  288. if (( $exit_code != 0 )); then
  289. ./scripts/run_bazel.py \
  290. --attempts=5 \
  291. mod deps --lockfile_mode=update
  292. echo "MODULE.bazel.lock is out of date! Use below file for update."
  293. echo "Platforms may require merging output, for example by applying"
  294. echo "an update, re-running triggers, and applying the next update."
  295. echo "============================================================"
  296. cat MODULE.bazel.lock
  297. echo "============================================================"
  298. exit 1
  299. fi
  300. # Build and run all targets on branch pushes to ensure we always have a
  301. # clean tree. We don't expect this to be an interactive path and so don't
  302. # optimize the latency of this step.
  303. - name: Compute impacted pull request targets (for push)
  304. if:
  305. steps.filter.outputs.has_code == 'true' && github.event_name == 'push'
  306. env:
  307. TARGETS_FILE: ${{ runner.temp }}/targets
  308. run: |
  309. echo "//..." >$TARGETS_FILE
  310. # Compute the set of possible rules impacted by this change using
  311. # Bazel-based diffing. This lets PRs and the merge queue have a much more
  312. # efficient test CI action by avoiding even enumerating (and downloading)
  313. # all of the unaffected Bazel targets.
  314. - name: Compute impacted pull request targets
  315. if:
  316. steps.filter.outputs.has_code == 'true' && github.event_name != 'push'
  317. env:
  318. # Compute the base SHA from the different event structures.
  319. GIT_BASE_SHA:
  320. ${{ github.event_name == 'pull_request' &&
  321. github.event.pull_request.base.sha ||
  322. github.event.merge_group.base_sha }}
  323. TARGETS_FILE: ${{ runner.temp }}/targets
  324. run: |
  325. # First fetch the relevant base into the git repository.
  326. git fetch --depth=1 origin $GIT_BASE_SHA
  327. # Then use `target-determinator` as wrapped by our script.
  328. ./scripts/target_determinator.py $GIT_BASE_SHA >$TARGETS_FILE
  329. # Bazel requires a test target to run the test command. There may be
  330. # no targets or there may only be non-test targets that we want to
  331. # build, so simply inject an explicit no-op test target.
  332. echo "//scripts:no_op_test" >> $TARGETS_FILE
  333. # Build and run just the tests impacted by the PR or merge group.
  334. - name: Test (${{ matrix.build_mode }})
  335. if:
  336. steps.filter.outputs.has_code == 'true' && matrix.build_mode !=
  337. 'clang-tidy'
  338. env:
  339. # 'libtool_check_unique failed to generate' workaround.
  340. # https://github.com/bazelbuild/bazel/issues/14113#issuecomment-999794586
  341. BAZEL_USE_CPP_ONLY_TOOLCHAIN: 1
  342. TARGETS_FILE: ${{ runner.temp }}/targets
  343. run: |
  344. # Decrease the jobs sharply if we see repeated failures to try to
  345. # work around transient network errors even if it makes things
  346. # slower.
  347. ./scripts/run_bazel.py \
  348. --attempts=5 --jobs-on-last-attempt=4 \
  349. test -c ${{ matrix.build_mode }} \
  350. --target_pattern_file=$TARGETS_FILE
  351. # Run in the clang-tidy config. This is done as part of tests so that we
  352. # aren't duplicating bazel/llvm setup.
  353. #
  354. # The `-k` flag is used to print all clang-tidy errors.
  355. - name: clang-tidy
  356. if:
  357. steps.filter.outputs.has_code == 'true' && matrix.build_mode ==
  358. 'clang-tidy'
  359. env:
  360. TARGETS_FILE: ${{ runner.temp }}/targets
  361. run: |
  362. ./scripts/run_bazel.py \
  363. --attempts=5 \
  364. build --config=clang-tidy -k \
  365. --target_pattern_file=$TARGETS_FILE
  366. # See "Disk space before build".
  367. - name: Disk space after build
  368. if: steps.filter.outputs.has_code == 'true'
  369. run: df -h