Flutter分析在Gitlab CI/CD管道中发现问题,而不是在本地

am46iovg  于 2023-03-19  发布在  Flutter
关注(0)|答案(2)|浏览(143)

我最近开始了一个新的Flutter项目。目前它的内容非常少。只有几个文件和一些基本的包,如Riverpod和Intl用于本地化。
每当我在本地机器上运行flutter analyze时,当前分支上没有任何问题。在运行flutter cleanflutter 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

和分析选项:

include: package:flutter_lints/flutter.yaml

linter:
  rules:
    always_put_required_named_parameters_first: true
    prefer_single_quotes: true
tv6aics1

tv6aics11#

您收到的错误:

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/目录,所以它不在存储库中,GitLab CI也不会克隆它。
您有两种选择:

  • 如果您仍然不打算将l10n保存在存储库中,则需要在before_script中运行flutter gen-l10n
  • 或者从.gitignore中删除l10n目录/文件并将其提交到存储库。
r55awzrz

r55awzrz2#

如果有人需要,我会把答案放在这里。由@Albert221提供。
一些从包中生成的文件被放到了git ignore中,导致了这个问题。我已经删除了它们,一切都很好。

相关问题