tests.yaml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 '*** python'
  196. which python
  197. python --version
  198. echo '*** clang'
  199. which clang
  200. clang --version
  201. echo '*** clang++'
  202. which clang++
  203. clang++ --version
  204. # Disable uploads when the remote cache is read-only.
  205. - name: Set up remote cache access (read-only)
  206. if:
  207. steps.filter.outputs.has_code == 'true' && github.event_name ==
  208. 'pull_request'
  209. run: |
  210. echo "remote_cache_upload=--remote_upload_local_results=false" \
  211. >> $GITHUB_ENV
  212. # Provide a cache key when the remote cache is read-write.
  213. - name: Set up remote cache access (read-write)
  214. if:
  215. steps.filter.outputs.has_code == 'true' && github.event_name !=
  216. 'pull_request'
  217. env:
  218. REMOTE_CACHE_KEY: ${{ secrets.CARBON_BUILDS_GITHUB }}
  219. run: |
  220. echo "$REMOTE_CACHE_KEY" | base64 -d > $HOME/remote_cache_key.json
  221. echo "remote_cache_upload=--google_credentials=$HOME/remote_cache_key.json" \
  222. >> $GITHUB_ENV
  223. # Add our bazel configuration and print basic info to ease debugging.
  224. - name: Configure Bazel and print info
  225. if: steps.filter.outputs.has_code == 'true'
  226. env:
  227. # Add a cache version for changes that bazel won't otherwise detect,
  228. # like llvm version changes.
  229. CACHE_VERSION: 1
  230. run: |
  231. cat >user.bazelrc <<EOF
  232. # Enable remote cache for our CI but minimize downloads.
  233. build --remote_cache=https://storage.googleapis.com/carbon-builds-github-v${CACHE_VERSION}
  234. build --remote_download_minimal
  235. # We import a special key into every action in order to key the Bazel
  236. # remote cache in a way that avoids collisions between different
  237. # runners. Anything that might change the system external to Bazel but
  238. # not be fully captured by the sand-boxing of the build should be used
  239. # as part of the key. We don't need to use the CPU target for example,
  240. # as that is captured by Bazel's configuration of each action. And the
  241. # Clang version is incorporated by our Clang toolchain setup. But we
  242. # do need to capture any differences between GitHub runner OSes that
  243. # don't impact the Bazel configuration to avoid collisions between
  244. # those.
  245. build --action_env=BAZEL_REMOTE_CACHE_KEY=github-action-${{ matrix.runner }}
  246. build ${{ env.remote_cache_upload }}
  247. # Set an artificially high jobs count. This flag controls the number
  248. # of concurrency Bazel itself uses, which is essential for actions
  249. # that are internally blocked on for example downloading results form
  250. # the cache above. Without setting this high, Bazel will pick a small
  251. # number based on the available host CPUs and the reality will be a
  252. # long chain of largely serialized download events with little or no
  253. # usage of the host machine. Fortunately, local actions are
  254. # *separately* gated on '--local_*_resources' that will avoid a large
  255. # jobs value overwhelming the host. There is a bug to make downloads
  256. # behave completely asynchronously and remove the need for this filed
  257. # back in 2018 but work seemed to not finish:
  258. # https://github.com/bazelbuild/bazel/issues/6394
  259. #
  260. # There is a new effort (yay!) but until then it seems worth using the
  261. # workaround of a high jobs value. The biggest downside (increased
  262. # heap usage) seems like it isn't currently a big loss for our builds.
  263. #
  264. # Higher values like 50 have led to CI failures with network errors
  265. # and IOExceptions, see
  266. # https://discord.com/channels/655572317891461132/707150492370862090/1151605725576056934
  267. build --jobs=32
  268. # General build options.
  269. build --verbose_failures
  270. test --test_output=errors
  271. EOF
  272. bazelisk info
  273. # Just for visibility, print space before and after the build.
  274. - name: Disk space before build
  275. if: steps.filter.outputs.has_code == 'true'
  276. run: df -h
  277. - name: Verify MODULE.bazel.lock
  278. if: steps.filter.outputs.has_code == 'true'
  279. run: |
  280. exit_code=0
  281. bazelisk mod deps --lockfile_mode=error || exit_code=$?
  282. if (( $exit_code != 0 )); then
  283. bazelisk mod deps --lockfile_mode=update
  284. echo "MODULE.bazel.lock is out of date! Use below file for update."
  285. echo "Platforms may require merging output, for example by applying"
  286. echo "an update, re-running triggers, and applying the next update."
  287. echo "============================================================"
  288. cat MODULE.bazel.lock
  289. echo "============================================================"
  290. exit 1
  291. fi
  292. # Build and run all targets on branch pushes to ensure we always have a
  293. # clean tree. We don't expect this to be an interactive path and so don't
  294. # optimize the latency of this step.
  295. - name: Compute impacted pull request targets (for push)
  296. if:
  297. steps.filter.outputs.has_code == 'true' && github.event_name == 'push'
  298. env:
  299. TARGETS_FILE: ${{ runner.temp }}/targets
  300. run: |
  301. echo "//..." >$TARGETS_FILE
  302. # Compute the set of possible rules impacted by this change using
  303. # Bazel-based diffing. This lets PRs and the merge queue have a much more
  304. # efficient test CI action by avoiding even enumerating (and downloading)
  305. # all of the unaffected Bazel targets.
  306. - name: Compute impacted pull request targets
  307. if:
  308. steps.filter.outputs.has_code == 'true' && github.event_name != 'push'
  309. env:
  310. # Compute the base SHA from the different event structures.
  311. GIT_BASE_SHA:
  312. ${{ github.event_name == 'pull_request' &&
  313. github.event.pull_request.base.sha ||
  314. github.event.merge_group.base_sha }}
  315. TARGETS_FILE: ${{ runner.temp }}/targets
  316. run: |
  317. # First fetch the relevant base into the git repository.
  318. git fetch --depth=1 origin $GIT_BASE_SHA
  319. # Then use `target-determinator` as wrapped by our script.
  320. ./scripts/target_determinator.py $GIT_BASE_SHA >$TARGETS_FILE
  321. # Bazel requires a test target to run the test command. There may be
  322. # no targets or there may only be non-test targets that we want to
  323. # build, so simply inject an explicit no-op test target.
  324. echo "//scripts:no_op_test" >> $TARGETS_FILE
  325. # Build and run just the tests impacted by the PR or merge group.
  326. - name: Test (${{ matrix.build_mode }})
  327. if:
  328. steps.filter.outputs.has_code == 'true' && matrix.build_mode !=
  329. 'clang-tidy'
  330. env:
  331. # 'libtool_check_unique failed to generate' workaround.
  332. # https://github.com/bazelbuild/bazel/issues/14113#issuecomment-999794586
  333. BAZEL_USE_CPP_ONLY_TOOLCHAIN: 1
  334. TARGETS_FILE: ${{ runner.temp }}/targets
  335. run: |
  336. for i in {1..5}; do
  337. if (( $i == 4 )); then
  338. # Decrease the jobs sharply if we see repeated failures to try to
  339. # work around transient network errors even if it makes things
  340. # slower.
  341. echo "build --jobs=4" >>user.bazelrc
  342. fi
  343. bazel_exit=0
  344. bazelisk test -c ${{ matrix.build_mode }} \
  345. --target_pattern_file=$TARGETS_FILE || bazel_exit=$?
  346. # If we succeed, we're done.
  347. if (( $bazel_exit == 0 )); then
  348. break
  349. fi
  350. # Several error codes are reliably permanent, break immediately.
  351. # `1` -- The build failed.
  352. # `2` -- Command line or environment problem.
  353. # `3` -- Tests failed or timed out, we don't retry at this layer
  354. # on execution timeout.
  355. # `4` -- No tests found, which should be impossible here.
  356. # `8` -- Explicitly interrupted build.
  357. #
  358. # Note that `36` is documented as "likely permanent", but we retry
  359. # it as most of our transient failures actually produce that error
  360. # code.
  361. if (( $bazel_exit == 1 || $bazel_exit == 2 || $bazel_exit == 3 || \
  362. $bazel_exit == 4 || $bazel_exit == 8 || $bazel_exit == 8 ))
  363. then
  364. break
  365. fi
  366. echo "Retrying a failed build as it may be transient..."
  367. # Also sleep a bit to try to skip over transient machine load.
  368. sleep $i
  369. done
  370. # Propagate the Bazel exit code.
  371. exit $bazel_exit
  372. # Run in the clang-tidy config. This is done as part of tests so that we
  373. # aren't duplicating bazel/llvm setup.
  374. #
  375. # The `-k` flag is used to print all clang-tidy errors.
  376. - name: clang-tidy
  377. if:
  378. steps.filter.outputs.has_code == 'true' && matrix.build_mode ==
  379. 'clang-tidy'
  380. env:
  381. TARGETS_FILE: ${{ runner.temp }}/targets
  382. run: |
  383. bazelisk build --config=clang-tidy -k \
  384. --target_pattern_file=$TARGETS_FILE
  385. # See "Disk space before build".
  386. - name: Disk space after build
  387. if: steps.filter.outputs.has_code == 'true'
  388. run: df -h