From 9102ada607f0cdc17c845621e736c31edb5c8a8d Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Thu, 14 Dec 2023 22:31:08 -0600 Subject: [PATCH] Misc Dart services cleanup --- pkgs/dart_services/lib/server.dart | 8 +-- pkgs/dart_services/lib/src/analysis.dart | 18 +++---- pkgs/dart_services/lib/src/caching.dart | 6 +-- pkgs/dart_services/lib/src/common_server.dart | 5 +- pkgs/dart_services/lib/src/compiling.dart | 8 +-- pkgs/dart_services/lib/src/logging.dart | 14 +++-- pkgs/dart_services/lib/src/pub.dart | 4 +- pkgs/dart_services/lib/src/utils.dart | 14 ++--- pkgs/dart_services/pubspec.lock | 52 +++++++++---------- pkgs/dart_services/pubspec.yaml | 5 +- pkgs/dart_services/test/compiling_test.dart | 1 - pkgs/dart_services/test/pub_test.dart | 4 -- 12 files changed, 65 insertions(+), 74 deletions(-) diff --git a/pkgs/dart_services/lib/server.dart b/pkgs/dart_services/lib/server.dart index fc76b811d..027a83995 100644 --- a/pkgs/dart_services/lib/server.dart +++ b/pkgs/dart_services/lib/server.dart @@ -42,13 +42,15 @@ Future main(List args) async { final sdk = Sdk(); - var port = 8080; + final int port; // Read port from args; fall back to using an env. variable. if (results.wasParsed('port')) { port = int.parse(results['port'] as String); - } else if (Platform.environment.containsKey('PORT')) { - port = int.parse(Platform.environment['PORT']!); + } else if (Platform.environment['PORT'] case final environmentPath?) { + port = int.parse(environmentPath); + } else { + port = 8080; } Logger.root.level = Level.FINER; diff --git a/pkgs/dart_services/lib/src/analysis.dart b/pkgs/dart_services/lib/src/analysis.dart index 7d4803e27..a376b9a23 100644 --- a/pkgs/dart_services/lib/src/analysis.dart +++ b/pkgs/dart_services/lib/src/analysis.dart @@ -83,7 +83,7 @@ class AnalysisServerWrapper { String get mainPath => _getPathFromName(kMainDart); Future init() async { - final serverArgs = ['--client-id=DartPad']; + const serverArgs = ['--client-id=DartPad']; _logger.info('Starting analysis server ' '(sdk: ${path.relative(sdkPath)}, args: ${serverArgs.join(' ')})'); @@ -231,11 +231,11 @@ class AnalysisServerWrapper { } Future dartdoc(String src, int offset) async { - final sourcepath = _getPathFromName(kMainDart); + final sourcePath = _getPathFromName(kMainDart); await _loadSources(_getOverlayMapWithPaths({kMainDart: src})); - final result = await analysisServer.analysis.getHover(sourcepath, offset); + final result = await analysisServer.analysis.getHover(sourcePath, offset); if (result.hovers.isEmpty) { return api.DocumentResponse(); @@ -260,9 +260,9 @@ class AnalysisServerWrapper { final errors = []; // Loop over all files and collect errors. - for (final sourcepath in sources.keys) { + for (final sourcePath in sources.keys) { errors - .addAll((await analysisServer.analysis.getErrors(sourcepath)).errors); + .addAll((await analysisServer.analysis.getErrors(sourcePath)).errors); } final issues = errors.map((error) { @@ -310,18 +310,16 @@ class AnalysisServerWrapper { final imports = getAllImportsFor(source); final importIssues = []; + late final lines = Lines(source); + for (final import in imports) { final start = import.firstTokenAfterCommentAndMetadata; final end = import.endToken; - Lines? lines; - if (import.dartImport) { // ignore dart: imports. } else if (import.packageImport) { if (!isSupportedPackage(import.packageName)) { - lines ??= Lines(source); - importIssues.add(api.AnalysisIssue( kind: 'warning', message: "Unsupported package: 'package:${import.packageName}'.", @@ -334,8 +332,6 @@ class AnalysisServerWrapper { )); } } else { - lines ??= Lines(source); - importIssues.add(api.AnalysisIssue( kind: 'error', message: 'Import type not supported.', diff --git a/pkgs/dart_services/lib/src/caching.dart b/pkgs/dart_services/lib/src/caching.dart index bbf274b81..42ffe2dd1 100644 --- a/pkgs/dart_services/lib/src/caching.dart +++ b/pkgs/dart_services/lib/src/caching.dart @@ -173,16 +173,16 @@ class RedisCache implements ServerCache { } @override - Future remove(String key) async { + Future remove(String key) async { key = _genKey(key); if (!_isConnected()) { log.warning('$_logPrefix: no cache available when removing key $key'); - return null; + return; } final commands = RespCommandsTier2(redisClient!); try { - return commands.del([key]).timeout(cacheOperationTimeout, + await commands.del([key]).timeout(cacheOperationTimeout, onTimeout: () async { log.warning('$_logPrefix: timeout on remove operation for key $key'); await _connection?.close(); diff --git a/pkgs/dart_services/lib/src/common_server.dart b/pkgs/dart_services/lib/src/common_server.dart index 6c462cdd5..5be1b3d29 100644 --- a/pkgs/dart_services/lib/src/common_server.dart +++ b/pkgs/dart_services/lib/src/common_server.dart @@ -228,10 +228,11 @@ class CommonServerApi { final packageVersions = getPackageVersions(); final packages = [ - for (final packageName in packageVersions.keys) + for (final MapEntry(key: packageName, value: packageVersion) + in packageVersions.entries) api.PackageInfo( name: packageName, - version: packageVersions[packageName]!, + version: packageVersion, supported: isSupportedPackage(packageName), ), ]; diff --git a/pkgs/dart_services/lib/src/compiling.dart b/pkgs/dart_services/lib/src/compiling.dart index 49038a9f3..b450f4277 100644 --- a/pkgs/dart_services/lib/src/compiling.dart +++ b/pkgs/dart_services/lib/src/compiling.dart @@ -14,7 +14,7 @@ import 'project_templates.dart'; import 'pub.dart'; import 'sdk.dart'; -Logger _logger = Logger('compiler'); +final Logger _logger = Logger('compiler'); /// An interface to the dart2js compiler. A compiler object can process one /// compile at a time. @@ -41,10 +41,6 @@ class Compiler { maxWorkers: 1), _projectTemplates = ProjectTemplates.projectTemplates; - Future warmup() async { - return compile('void main() => print("hello");'); - } - /// Compile the given string and return the resulting [CompilationResults]. Future compile( String source, { @@ -162,7 +158,7 @@ class Compiler { _logger.fine('About to exec dartdevc worker: ${arguments.join(' ')}"'); final response = - await _ddcDriver.doWork(WorkRequest()..arguments.addAll(arguments)); + await _ddcDriver.doWork(WorkRequest(arguments: arguments)); if (response.exitCode != 0) { return DDCCompilationResults.failed([ diff --git a/pkgs/dart_services/lib/src/logging.dart b/pkgs/dart_services/lib/src/logging.dart index 95f3b1690..fc6319786 100644 --- a/pkgs/dart_services/lib/src/logging.dart +++ b/pkgs/dart_services/lib/src/logging.dart @@ -11,11 +11,17 @@ final _wsRegex = RegExp(r' \s+'); void emitLogsToStdout() { Logger.root.onRecord.listen((LogRecord record) { if (verboseLogging || record.level >= Level.INFO) { - var stackTrace = ''; - if (record.stackTrace != null) { - var lines = record.stackTrace!.toString().split('\n').take(5).join(' '); - lines = lines.replaceAll(_wsRegex, ' '); + final String stackTrace; + if (record.stackTrace case final recordStackTrace?) { + final lines = recordStackTrace + .toString() + .split('\n') + .take(5) + .join(' ') + .replaceAll(_wsRegex, ' '); stackTrace = ' $lines'; + } else { + stackTrace = ''; } print( diff --git a/pkgs/dart_services/lib/src/pub.dart b/pkgs/dart_services/lib/src/pub.dart index ab921844e..f21c26647 100644 --- a/pkgs/dart_services/lib/src/pub.dart +++ b/pkgs/dart_services/lib/src/pub.dart @@ -12,9 +12,7 @@ import 'package:yaml/yaml.dart'; import 'project_templates.dart' as project; /// Extract all imports from [dartSource] source code. -List getAllImportsFor(String? dartSource) { - if (dartSource == null) return []; - +List getAllImportsFor(String dartSource) { final unit = parseString(content: dartSource, throwIfDiagnostics: false).unit; return unit.directives.whereType().toList(); } diff --git a/pkgs/dart_services/lib/src/utils.dart b/pkgs/dart_services/lib/src/utils.dart index 835bb0738..d2403b00b 100644 --- a/pkgs/dart_services/lib/src/utils.dart +++ b/pkgs/dart_services/lib/src/utils.dart @@ -69,8 +69,7 @@ Future runWithLogging( } class TaskScheduler { - final Queue<_SchedulerTask> _taskQueue = - Queue<_SchedulerTask>(); + final Queue<_SchedulerTask> _taskQueue = Queue(); bool _isActive = false; int get queueCount => _taskQueue.length; @@ -112,16 +111,17 @@ class _SchedulerTask { // Public working data structure. abstract class SchedulerTask { - late Duration timeoutDuration; + final Duration timeoutDuration; + + SchedulerTask({required this.timeoutDuration}); + Future perform(); } class ClosureTask extends SchedulerTask { final Future Function() _closure; - ClosureTask(this._closure, {required Duration timeoutDuration}) { - this.timeoutDuration = timeoutDuration; - } + ClosureTask(this._closure, {required super.timeoutDuration}); @override Future perform() { @@ -136,7 +136,7 @@ class ClosureTask extends SchedulerTask { final _possiblePathPattern = RegExp(r'[a-zA-Z:]*\/\S*'); class Lines { - final _starts = []; + final List _starts = []; Lines(String source) { final units = source.codeUnits; diff --git a/pkgs/dart_services/pubspec.lock b/pkgs/dart_services/pubspec.lock index 927c38872..667713b9e 100644 --- a/pkgs/dart_services/pubspec.lock +++ b/pkgs/dart_services/pubspec.lock @@ -42,7 +42,7 @@ packages: source: hosted version: "1.5.0" async: - dependency: "direct dev" + dependency: transitive description: name: async sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" @@ -93,18 +93,18 @@ packages: dependency: transitive description: name: build_resolvers - sha256: "64e12b0521812d1684b1917bc80945625391cb9bdd4312536b1d69dcb6133ed8" + sha256: "339086358431fa15d7eca8b6a36e5d783728cf025e559b834f4609a1fcfb7b0a" url: "https://pub.dev" source: hosted - version: "2.4.1" + version: "2.4.2" build_runner: dependency: "direct dev" description: name: build_runner - sha256: "10c6bcdbf9d049a0b666702cf1cee4ddfdc38f02a19d35ae392863b47519848b" + sha256: "67d591d602906ef9201caf93452495ad1812bea2074f04e25dbd7c133785821b" url: "https://pub.dev" source: hosted - version: "2.4.6" + version: "2.4.7" build_runner_core: dependency: transitive description: @@ -125,10 +125,10 @@ packages: dependency: transitive description: name: built_value - sha256: "69acb7007eb2a31dc901512bfe0f7b767168be34cb734835d54c070bfa74c1b2" + sha256: c9aabae0718ec394e5bc3c7272e6bb0dc0b32201a08fe185ec1d8401d3e39309 url: "https://pub.dev" source: hosted - version: "8.8.0" + version: "8.8.1" checked_yaml: dependency: transitive description: @@ -162,7 +162,7 @@ packages: source: hosted version: "4.8.0" collection: - dependency: "direct main" + dependency: "direct dev" description: name: collection sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a @@ -170,7 +170,7 @@ packages: source: hosted version: "1.18.0" convert: - dependency: "direct main" + dependency: transitive description: name: convert sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592" @@ -181,12 +181,12 @@ packages: dependency: transitive description: name: coverage - sha256: ac86d3abab0f165e4b8f561280ff4e066bceaac83c424dd19f1ae2c2fcd12ca9 + sha256: "8acabb8306b57a409bf4c83522065672ee13179297a6bb0cb9ead73948df7c76" url: "https://pub.dev" source: hosted - version: "1.7.1" + version: "1.7.2" crypto: - dependency: "direct main" + dependency: transitive description: name: crypto sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab @@ -349,10 +349,10 @@ packages: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" meta: dependency: "direct main" description: @@ -389,10 +389,10 @@ packages: dependency: "direct main" description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" pointycastle: dependency: transitive description: @@ -493,10 +493,10 @@ packages: dependency: transitive description: name: source_gen - sha256: fc0da689e5302edb6177fdd964efcb7f58912f43c28c2047a808f5bfff643d16 + sha256: "14658ba5f669685cd3d63701d01b31ea748310f7ab854e471962670abcf57832" url: "https://pub.dev" source: hosted - version: "1.4.0" + version: "1.5.0" source_helper: dependency: transitive description: @@ -581,26 +581,26 @@ packages: dependency: "direct dev" description: name: test - sha256: a1f7595805820fcc05e5c52e3a231aedd0b72972cb333e8c738a8b1239448b6f + sha256: "3d028996109ad5c253674c7f347822fb994a087614d6f353e6039704b4661ff2" url: "https://pub.dev" source: hosted - version: "1.24.9" + version: "1.25.0" test_api: dependency: transitive description: name: test_api - sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b" + sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f" url: "https://pub.dev" source: hosted - version: "0.6.1" + version: "0.7.0" test_core: dependency: transitive description: name: test_core - sha256: a757b14fc47507060a162cc2530d9a4a2f92f5100a952c7443b5cad5ef5b106a + sha256: "2bc4b4ecddd75309300d8096f781c0e3280ca1ef85beda558d33fcbedc2eead4" url: "https://pub.dev" source: hosted - version: "0.5.9" + version: "0.6.0" test_descriptor: dependency: "direct dev" description: @@ -629,10 +629,10 @@ packages: dependency: transitive description: name: vm_service - sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 + sha256: a2662fb1f114f4296cf3f5a50786a2d888268d7776cf681aa17d660ffa23b246 url: "https://pub.dev" source: hosted - version: "13.0.0" + version: "14.0.0" watcher: dependency: transitive description: diff --git a/pkgs/dart_services/pubspec.yaml b/pkgs/dart_services/pubspec.yaml index 0aab3a224..92aa711a1 100644 --- a/pkgs/dart_services/pubspec.yaml +++ b/pkgs/dart_services/pubspec.yaml @@ -10,9 +10,6 @@ dependencies: analyzer: ^6.3.0 args: ^2.4.2 bazel_worker: ^1.0.2 - collection: ^1.18.0 - convert: ^3.1.1 - crypto: ^3.0.3 encrypt: ^5.0.2 http: ^1.0.0 json_annotation: any @@ -25,8 +22,8 @@ dependencies: yaml: ^3.1.2 dev_dependencies: - async: ^2.11.0 build_runner: ^2.4.5 + collection: ^1.18.0 dart_flutter_team_lints: ^2.1.1 grinder: ^0.9.5 json_serializable: any diff --git a/pkgs/dart_services/test/compiling_test.dart b/pkgs/dart_services/test/compiling_test.dart index 323b1b9ac..85d9e9f35 100644 --- a/pkgs/dart_services/test/compiling_test.dart +++ b/pkgs/dart_services/test/compiling_test.dart @@ -16,7 +16,6 @@ void defineTests() { setUpAll(() async { compiler = Compiler(Sdk(), storageBucket: 'nnbd_artifacts'); - await compiler.warmup(); }); tearDownAll(() async { diff --git a/pkgs/dart_services/test/pub_test.dart b/pkgs/dart_services/test/pub_test.dart index 0b7320fe2..03b704903 100644 --- a/pkgs/dart_services/test/pub_test.dart +++ b/pkgs/dart_services/test/pub_test.dart @@ -10,10 +10,6 @@ void main() => defineTests(); void defineTests() { group('pub', () { group('getAllImportsFor', () { - test('null', () { - expect(getAllImportsFor(null), isEmpty); - }); - test('empty', () { expect(getAllImportsFor(''), isEmpty); expect(getAllImportsFor(' \n '), isEmpty);