tests.yaml 17 KB

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