Flutter 分析在 Gitlab CI/CD 管道中发现了本地没有发现的问题
我最近开始了一个新的 Flutter 项目。目前这个项目还很简陋。只有几个文件和一些基本的软件包,如 riverpod 和 intl,用于本地化。
每当我在本地机器上运行
flutter analyze
时,我当前的分支上都没有问题。在
flutter clean
和
flutter pub get
等之后,情况仍然相同。
但是,当我提交并将此分支推送到 Gitlab 时,我在 CI 中调用的
flutter analyze
确实产生了三个问题,这些问题似乎与无法找到或访问 intl 包有关。
这是我的 Gitlab CI 文件:
analyse:
stage: test
image: "cirrusci/flutter:stable"
before_script:
- export PATH="$PATH":"$HOME/.flutter-sdk/.pub-cache/bin"
- flutter pub get
- flutter --version
script:
- flutter analyze
test:
stage: test
image: "cirrusci/flutter:stable"
before_script:
- export PATH="$PATH":"$HOME/.flutter-sdk/.pub-cache/bin"
- flutter pub get
script:
- flutter test -j 1 test
版本调用是为了确保 Flutter 和 Dart 版本在 Gitlab 和本地是相同的。
这是分析阶段的输出:
Running with gitlab-runner 15.9.0~beta.115.g598a7c91 (598a7c91)
on blue-4.shared.runners-manager.gitlab.com/default J2nyww-s, system ID: s_5425356d8adf
feature flags: FF_USE_IMPROVED_URL_MASKING:true
Resolving secrets 00:00
Preparing the "docker+machine" executor 01:21
Using Docker executor with image cirrusci/flutter:stable ...
Pulling docker image cirrusci/flutter:stable ...
Using docker image sha256:459db32a49bba36ef4fae0e61d91b27586b2e261bb03465b85da96b43fd46531 for cirrusci/flutter:stable with digest cirrusci/flutter@sha256:32e7d16c7e1458d3135e945ceec82c5d43bfda289526f850711f3908fb927502 ...
Preparing environment 00:02
Running on runner-j2nyww-s-project-43681222-concurrent-0 via runner-j2nyww-s-shared-1678886240-62a8435b...
Getting source from Git repository 00:03
$ eval "$CI_PRE_CLONE_SCRIPT"
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/saxion.nl/aad/hybrid/assignment2-framework1/17/.git/
Created fresh repository.
Checking out 4a9dcab0 as detached HEAD (ref is Outline_creation)...
Skipping Git submodules setup
Executing "step_script" stage of the job script 00:33
Using docker image sha256:459db32a49bba36ef4fae0e61d91b27586b2e261bb03465b85da96b43fd46531 for cirrusci/flutter:stable with digest cirrusci/flutter@sha256:32e7d16c7e1458d3135e945ceec82c5d43bfda289526f850711f3908fb927502 ...
$ flutter clean
$ flutter pub get
Running "flutter pub get" in 17...
Resolving dependencies...
async 2.10.0 (2.11.0 available)
build_daemon 3.1.1 (4.0.0 available)
build_runner 2.3.3 (2.4.1 available)
characters 1.2.1 (1.3.0 available)
collection 1.17.0 (1.17.1 available)
intl 0.17.0 (0.18.0 available)
js 0.6.5 (0.6.7 available)
matcher 0.12.13 (0.12.14 available)
material_color_utilities 0.2.0 (0.3.0 available)
meta 1.8.0 (1.9.0 available)
path 1.8.2 (1.8.3 available)
test_api 0.4.16 (0.4.18 available)
Got dependencies!
$ flutter --version
Flutter 3.7.7 • channel unknown • unknown source
Framework • revision 2ad6cd72c0 (7 days ago) • 2023-03-08 09:41:59 -0800
Engine • revision 1837b5be5f
Tools • Dart 2.19.4 • DevTools 2.20.1
$ flutter analyze
Analyzing 17...
error • Target of URI doesn't exist: 'l10n/l10n.dart' • lib/main.dart:6:8 • uri_does_not_exist
error • The values in a const list literal must be constants • lib/main.dart:20:11 • non_constant_list_element
error • Undefined name 'S' • lib/main.dart:20:11 • undefined_identifier
3 issues found. (ran in 16.6s)
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1
所有问题都发生在主文件中,如下所示:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kook_app/ui/app/app_page.dart';
import 'l10n/l10n.dart';
void main() {
runApp(const KookApp());
}
class KookApp extends StatelessWidget {
const KookApp({super.key});
@override
Widget build(BuildContext context) {
return ProviderScope(
child: MaterialApp(
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', 'UK'),
Locale('nl'),
],
debugShowCheckedModeBanner: false,
title: 'KookApp',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const AppPage(),
),
);
}
}
文件本身也没有报告 Android Studio 中的任何问题。
最后,为了完整起见,这里是我的pubspec:
name: kook_app
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: '>=2.19.2 <3.0.0'
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: 0.17.0
cupertino_icons: ^1.0.2
flutter_riverpod: ^2.3.2
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: any
flutter_lints: ^2.0.0
flutter_intl:
enabled: true
arb_dir: lib/l10n/locales
output_dir: lib/l10n
flutter:
generate: true
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
和 analysis_options:
include: package:flutter_lints/flutter.yaml
linter:
rules:
always_put_required_named_parameters_first: true
prefer_single_quotes: true
如果有人需要,我会把答案放在这里。感谢@Albert221。
包中生成的一些文件被放入 git ignore 中,导致出现问题。我已删除它们,一切正常。
您收到的错误:
error • Target of URI doesn't exist: 'l10n/l10n.dart' • lib/main.dart:6:8 • uri_does_not_exist
error • The values in a const list literal must be constants • lib/main.dart:20:11 • non_constant_list_element
error • Undefined name 'S' • lib/main.dart:20:11 • undefined_identifier
出现在这里是因为
l10n/l10n.dart
文件不存在。
我猜是您的
.gitignore
中有此文件或
l10n/<​​/code> 目录,因此它不在存储库中,并且 GitLab CI 不会克隆它。
您有两个选择:
-
如果您仍然打算不将
l10n
保留在您的存储库中,则需要在before_script
中运行flutter gen-l10n
, -
或从您的
.gitignore
中删除l10n
目录/文件并将其提交到存储库。
我不知道这是否有用,但我在
Github
上使用
melos
的
CI
时遇到过类似的情况。
analyze
在本地成功,但在远程失败:
$ melos analyze
└> flutter analyze
└> FAILED (in 1 packages)
└> my_package (with exit code 1)
为了获得与远程相同的配置,我在本地删除了该软件包并从 PR 检查了分支,然后我就能看到问题。