From bffc2c57d27178214d7b567c9abbf911a4f61801 Mon Sep 17 00:00:00 2001 From: Daniel Varga-Hali Date: Thu, 7 Mar 2024 15:09:43 +0100 Subject: [PATCH] fix(sentry-dart): remove transitive dart:io reference for web (#1898) * fix(sentry-dart): remove transitive dart:io reference for web TransportUtils imported sentry_io.dart which prevented web projects such as example_web from building when build_runner was used. Resolves getsentry/sentry-dart#1893 * docs(api): update CHANGELOG.md for unreleased fix CHANGELOG.md now includes information about unreleased fix for issue #1893. * test(dart-sentry): add compilation tests for example_web Added tests to check if example_web project can be compiled with build_runner. Resolves getsentry/sentry-dart#1893 * test(dart-sentry): fix failing tests for example_web compilation Test ordering should not have impact on example_web_comopile_test and path handling across platforms was simplified. Resolves getsentry/sentry-dart#1893 * docs(api): update CHANGELOG.md Place entry for #1893 under unreleased fix in CHANGELOG getsentry/sentry-dart#1893 * chore(code-style): code formatting for #1893 --- CHANGELOG.md | 1 + dart/lib/src/utils/transport_utils.dart | 4 +- dart/test/example_web_compile_test.dart | 86 +++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 dart/test/example_web_compile_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 354f875aba..d5fac9c752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fix transaction end timestamp trimming ([#1916](https://github.com/getsentry/sentry-dart/pull/1916)) - Transaction end timestamps are now correctly trimmed to the latest child span end timestamp +- remove transitive dart:io reference for web ([#1898](https://github.com/getsentry/sentry-dart/pull/1898)) ### Features diff --git a/dart/lib/src/utils/transport_utils.dart b/dart/lib/src/utils/transport_utils.dart index 388db8e8d5..fa2f20096a 100644 --- a/dart/lib/src/utils/transport_utils.dart +++ b/dart/lib/src/utils/transport_utils.dart @@ -1,7 +1,9 @@ import 'package:http/http.dart'; -import '../../sentry_io.dart'; import '../client_reports/discard_reason.dart'; +import '../protocol.dart'; +import '../sentry_envelope.dart'; +import '../sentry_options.dart'; import '../transport/data_category.dart'; class TransportUtils { diff --git a/dart/test/example_web_compile_test.dart b/dart/test/example_web_compile_test.dart new file mode 100644 index 0000000000..d327dcaed1 --- /dev/null +++ b/dart/test/example_web_compile_test.dart @@ -0,0 +1,86 @@ +@TestOn('vm') +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:test/test.dart'; + +// Tests for the following issue +// https://github.com/getsentry/sentry-dart/issues/1893 +void main() { + group('Compile example_web', () { + test( + 'dart pub get and compilation should run successfully', + () async { + final result = await _runProcess('dart pub get', + workingDirectory: _exampleWebWorkingDir); + expect(result.exitCode, 0, + reason: 'Could run `dart pub get` for example_web. ' + 'Likely caused by outdated dependencies'); + // running this test locally require clean working directory + final cleanResult = await _runProcess('dart run build_runner clean', + workingDirectory: _exampleWebWorkingDir); + expect(cleanResult.exitCode, 0); + final compileResult = await _runProcess( + 'dart run build_runner build -r web -o build --delete-conflicting-outputs', + workingDirectory: _exampleWebWorkingDir); + expect(compileResult.exitCode, 0, + reason: 'Could not compile example_web project'); + expect( + compileResult.stdout, + isNot(contains( + 'Skipping compiling sentry_dart_web_example|web/main.dart')), + reason: + 'Could not compile main.dart, likely because of dart:io import.'); + expect( + compileResult.stdout, + contains( + 'build_web_compilers:entrypoint on web/main.dart:Compiled')); + }, + timeout: Timeout(const Duration(minutes: 1)), // double of detault timeout + ); + }); +} + +/// Runs [command] with command's stdout and stderr being forwrarded to +/// test runner's respective streams. It buffers stdout and returns it. +/// +/// Returns [_CommandResult] with exitCode and stdout as a single sting +Future<_CommandResult> _runProcess(String command, + {String workingDirectory = '.'}) async { + final parts = command.split(' '); + assert(parts.isNotEmpty); + final cmd = parts[0]; + final args = parts.skip(1).toList(); + final process = + await Process.start(cmd, args, workingDirectory: workingDirectory); + // forward standard streams + unawaited(stderr.addStream(process.stderr)); + final buffer = []; + final stdoutCompleter = Completer.sync(); + process.stdout.listen( + (units) { + buffer.addAll(units); + stdout.add(units); + }, + cancelOnError: true, + onDone: () { + stdoutCompleter.complete(); + }, + ); + await stdoutCompleter.future; + final processOut = utf8.decode(buffer); + int exitCode = await process.exitCode; + return _CommandResult(exitCode: exitCode, stdout: processOut); +} + +String get _exampleWebWorkingDir { + return '${Directory.current.path}${Platform.pathSeparator}example_web'; +} + +class _CommandResult { + final int exitCode; + final String stdout; + + const _CommandResult({required this.exitCode, required this.stdout}); +}