From 3b7f5614521f05f9fc85840e9c0a60cd6b8fe054 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Thu, 12 Nov 2026 09:10:57 +0100 Subject: [PATCH] Run the .fvmrc Flutter in repo scripts The free-day hook ran the system flutter (3.47.9). On a fresh checkout its pub get rewrote pubspec.lock to its own test_api, so pre-commit failed with "files were modified". Hooks and test_changed.sh now put the pinned fvm SDK on PATH. Co-Authored-By: Claude Opus 5.5 --- phone_app/scripts/sick_days_interop/check.sh | 3 + scripts/free_days_interop/check.sh | 3 + scripts/pinned_flutter.sh | 67 ++++++++++++++++++++ scripts/test_changed.sh | 2 + 4 files changed, 75 insertions(+) create mode 100644 scripts/pinned_flutter.sh diff --git a/phone_app/scripts/sick_days_interop/check.sh b/phone_app/scripts/sick_days_interop/check.sh index 634e450..e828640 100755 --- a/phone_app/scripts/sick_days_interop/check.sh +++ b/phone_app/scripts/sick_days_interop/check.sh @@ -23,6 +23,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" APP_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" readonly SCRIPT_DIR APP_DIR +# shellcheck source=../../../scripts/pinned_flutter.sh +source "$APP_DIR/../scripts/pinned_flutter.sh" readonly SICKDAYS_PYTHON="${SICKDAYS_PYTHON:-$HOME/.local/share/sickdays/venv/bin/python}" TEMP_DIR="" @@ -34,6 +36,7 @@ cleanup() { trap cleanup EXIT validate_requirements() { + use_pinned_flutter "$APP_DIR/.." if [[ ! -x "$SICKDAYS_PYTHON" ]]; then echo "Error: no sickdays python at $SICKDAYS_PYTHON (set SICKDAYS_PYTHON)" >&2 exit 1 diff --git a/scripts/free_days_interop/check.sh b/scripts/free_days_interop/check.sh index 7f95d33..5a73c34 100755 --- a/scripts/free_days_interop/check.sh +++ b/scripts/free_days_interop/check.sh @@ -20,6 +20,8 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_DIR="$(cd "$SCRIPT_DIR/../.." && pwd)" readonly SCRIPT_DIR REPO_DIR +# shellcheck source=../pinned_flutter.sh +source "$REPO_DIR/scripts/pinned_flutter.sh" readonly FREEDAYS_PYTHON="${FREEDAYS_PYTHON:-$HOME/.local/share/freedays/venv/bin/python}" TEMP_DIR="" @@ -39,6 +41,7 @@ validate_requirements() { } main() { + use_pinned_flutter "$REPO_DIR" validate_requirements TEMP_DIR="$(mktemp -d)" local pool="$TEMP_DIR/free_days.json" diff --git a/scripts/pinned_flutter.sh b/scripts/pinned_flutter.sh new file mode 100644 index 0000000..3927418 --- /dev/null +++ b/scripts/pinned_flutter.sh @@ -0,0 +1,67 @@ +#!/bin/bash +# ============================================================================ +# Source this to make `flutter` mean the SDK pinned in the repo's .fvmrc. +# +# Why: a bare `flutter` is whatever is first on PATH (here the system +# /usr/bin/flutter). When that SDK differs from .fvmrc, its implicit +# `pub get` re-resolves phone_app/pubspec.lock against its own pinned +# test_api and the hook "modifies files" on a fresh checkout. The fix is to +# run the pinned SDK, never to commit a lock for whichever SDK ran last. +# +# With fvm: the pinned SDK's bin goes on PATH (installed on first use), +# placed after any leading */shims entries so build-lock shims still wrap it. +# Without fvm (CI installs the pinned version itself): fail fast on a +# mismatch instead of silently rewriting the lock. +# +# Usage: source "$REPO_DIR/scripts/pinned_flutter.sh" && use_pinned_flutter "$REPO_DIR" +# Requires: jq; fvm when the active flutter is not already the pinned one. +# ============================================================================ + +# pinned_flutter_version REPO_DIR: print the version pinned in .fvmrc. +pinned_flutter_version() { + local fvmrc="$1/.fvmrc" + [[ -f "$fvmrc" ]] || { echo "Error: no $fvmrc" >&2; return 1; } + jq -er '.flutter' "$fvmrc" || { echo "Error: no .flutter in $fvmrc" >&2; return 1; } +} + +# path_with_bin BIN: print PATH with BIN inserted after leading */shims dirs. +path_with_bin() { + local bin="$1" out="" placed=0 part + local -a parts + IFS=':' read -r -a parts <<<"$PATH" + for part in "${parts[@]}"; do + if [[ $placed -eq 0 && $part != */shims ]]; then + out="${out:+$out:}$bin" + placed=1 + fi + [[ $part == "$bin" ]] && continue + out="${out:+$out:}$part" + done + [[ $placed -eq 1 ]] || out="${out:+$out:}$bin" + printf '%s\n' "$out" +} + +# use_pinned_flutter REPO_DIR: export PATH so `flutter` is the pinned SDK. +use_pinned_flutter() { + local version cache bin + version="$(pinned_flutter_version "$1")" || return 1 + if command -v fvm >/dev/null; then + cache="$(fvm api context | jq -er '.context.config.cachePath')" || { + echo "Error: fvm reported no cache path" >&2 + return 1 + } + bin="$cache/versions/$version/bin" + if [[ ! -x "$bin/flutter" ]]; then + fvm install "$version" >&2 || return 1 + fi + PATH="$(path_with_bin "$bin")" + export PATH + return 0 + fi + local active + active="$(flutter --version --machine 2>/dev/null | jq -r '.frameworkVersion')" + if [[ "$active" != "$version" ]]; then + echo "Error: flutter $active is active but .fvmrc pins $version (install fvm)" >&2 + return 1 + fi +} diff --git a/scripts/test_changed.sh b/scripts/test_changed.sh index 589448c..3242c63 100755 --- a/scripts/test_changed.sh +++ b/scripts/test_changed.sh @@ -52,6 +52,8 @@ for f in "${CHANGED[@]}"; do done dart_run() { + # shellcheck source=pinned_flutter.sh + source scripts/pinned_flutter.sh && use_pinned_flutter "$PWD" if [[ $dfull -eq 1 || ${#dtests[@]} -eq 0 ]]; then (cd phone_app && flutter test --reporter=compact) else (cd phone_app && flutter test --reporter=compact "${dtests[@]}"); fi }