Fix typecheck foundations (#167060)

## Summary
This PR is the core part of #166813. The original work seems to grow
large, and we'd like to enable a preventive check beforehand to prevent
more errors from entering the codebase.

The idea is to have a selective type check that would only check changed
files' projects.
- [x] when there's no extra label, run the selective type check only on
the diffing files' projects (success:
https://buildkite.com/elastic/kibana-pull-request/builds/161837)
- [x] when the label `ci:hard-typecheck` is present, run the regular
(but now, working) full typecheck (expected to fail: )

cc: @watson

---------

Co-authored-by: Brad White <brad.white@elastic.co>
Co-authored-by: Thomas Watson <w@tson.dk>
Co-authored-by: Thomas Watson <watson@elastic.co>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Alex Szabo 2023-09-26 01:28:56 +02:00 committed by GitHub
parent 4662960980
commit e81728ee96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 181 additions and 16 deletions

View file

@ -59,6 +59,12 @@ const uploadPipeline = (pipelineContent: string | object) => {
pipeline.push(getPipeline('.buildkite/pipelines/pull_request/kbn_handlebars.yml'));
}
if (GITHUB_PR_LABELS.includes('ci:hard-typecheck')) {
pipeline.push(getPipeline('.buildkite/pipelines/pull_request/type_check.yml'));
} else {
pipeline.push(getPipeline('.buildkite/pipelines/pull_request/type_check_selective.yml'));
}
if (
(await doAnyChangesMatch([
/^src\/plugins\/controls/,

View file

@ -4,8 +4,9 @@ set -euo pipefail
.buildkite/scripts/bootstrap.sh
echo "--- Run scripts/type_check to ensure that all build available"
node scripts/type_check
# TODO: Enable in #166813 after fixing types
# echo "--- Run scripts/type_check to ensure that all build available"
# node scripts/type_check
echo "--- Build API Docs"
node --max-old-space-size=12000 scripts/build_api_docs

View file

@ -0,0 +1,114 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ "${CI-}" == "true" ]]; then
.buildkite/scripts/bootstrap.sh
sha1="${GITHUB_PR_TARGET_BRANCH-}"
sha2="${GITHUB_PR_TRIGGERED_SHA-}"
else
# Script take between 0 and 2 arguments representing two commit SHA's:
# If 0, it will diff HEAD and HEAD^
# If 1 (SHA1), it will diff SHA1 and SHA1^
# If 2 (SHA1, SHA2), it will diff SHA1 and SHA2
sha1="${1-HEAD}"
sha2="${2-$sha1^}"
fi
uniq_dirs=()
uniq_tsconfigs=()
echo "Detecting files changed between $sha1 and $sha2..."
files=($(git diff --name-only $sha1 $sha2))
add_dir () {
new_dir=$1
if [ ${#uniq_dirs[@]} -gt 0 ]; then
for dir in "${uniq_dirs[@]}"
do
if [[ "$new_dir" == "$dir" ]]; then
return
fi
done
fi
uniq_dirs+=($new_dir)
}
add_tsconfig () {
new_tsconfig=$1
if [ ${#uniq_tsconfigs[@]} -gt 0 ]; then
for tsconfig in "${uniq_tsconfigs[@]}"
do
if [[ "$new_tsconfig" == "$tsconfig" ]]; then
return
fi
done
fi
echo " $new_tsconfig"
uniq_tsconfigs+=($new_tsconfig)
}
contains_tsconfig () {
dir=$1
tsconfig="$dir/tsconfig.json"
if [ -f "$tsconfig" ]; then
true
else
false
fi
}
find_tsconfig () {
dir=$1
if [[ "$dir" == "." ]]; then
return
fi
if contains_tsconfig $dir; then
add_tsconfig "$dir/tsconfig.json"
else
find_tsconfig $(dirname -- "$dir")
fi
}
if [ ${#files[@]} -eq 0 ]; then
echo "No files found!"
exit
fi
for file in "${files[@]}"
do
dir=$(dirname -- "$file")
# Ignore buildkite dir because it traverses many kbn packages and emits incorrect results
if [[ "$dir" != .buildkite* ]]; then
add_dir $dir
fi
done
echo "Looking for related tsconfig.json files..."
for dir in "${uniq_dirs[@]}"
do
find_tsconfig $dir
done
if [ ${#uniq_tsconfigs[@]} -eq 0 ]; then
echo "No tsconfig.json files found for changes in $sha1 $sha2"
exit
fi
echo "Running scripts/type_check for each found tsconfig.json file..."
for tsconfig in "${uniq_tsconfigs[@]}"
do
node scripts/type_check --project $tsconfig
done