Skip to content

[flutter_tools] Forward encoding in ErrorHandlingFile.readAsStringSync - #196136

Merged
auto-submit[bot] merged 1 commit into
flutter:masterfrom
bkonyi:fix-error-handling-read-as-string-encoding
Nov 12, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:masterfrom
bkonyi:fix-error-handling-read-as-string-encoding

Conversation

@bkonyi

@bkonyi bkonyi commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Previously, ErrorHandlingFile.readAsStringSync dropped its encoding parameter when calling delegate.readAsStringSync(), forcing UTF-8 decoding even when callers requested another encoding (unlike readAsString, readAsLines, and readAsLinesSync, which already forwarded encoding). Because flutter_tools wraps LocalFileSystem in ErrorHandlingFileSystem, callers such as ScreenshotCommand.ensureOutputIsNotJsonRpcError (which passes AsciiCodec(allowInvalid: true) when checking small screenshot files for JSON-RPC error payloads) crashed with a FileSystemException.

Forward encoding to delegate.readAsStringSync(encoding: encoding) and add regression tests covering ErrorHandlingFile.readAsStringSync backed by LocalFileSystem and ScreenshotCommand.ensureOutputIsNotJsonRpcError on ErrorHandlingFileSystem.

Fixes #196225

Previous output:

Screenshot written to flutter_01.skp (0kB).

FileSystemException: Failed to decode data using encoding 'utf-8', path = '/tmp/repro_screenshot_skp.QPQNLT/flutter_01.skp'
#0      _File._tryDecode (dart:io/file_impl.dart:704:7)
#1      _File.readAsStringSync (dart:io/file_impl.dart:715:7)
#2      ForwardingFile.readAsStringSync (package:file/src/forwarding/forwarding_file.dart:99:16)
#3      ErrorHandlingFile.readAsStringSync.<anonymous closure> (package:flutter_tools/src/base/error_handling_io.dart:258:22)
#4      _runSync (package:flutter_tools/src/base/error_handling_io.dart:1116:16)
#5      ErrorHandlingFile.readAsStringSync (package:flutter_tools/src/base/error_handling_io.dart:257:12)
#6      ScreenshotCommand.ensureOutputIsNotJsonRpcError (package:flutter_tools/src/commands/screenshot.dart:198:39)
#7      ScreenshotCommand.runSkia (package:flutter_tools/src/commands/screenshot.dart:180:5)
<asynchronous suspension>
#8      ScreenshotCommand.runCommand (package:flutter_tools/src/commands/screenshot.dart:123:19)
<asynchronous suspension>
#9      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:876:27)
<asynchronous suspension>
#10     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
#11     CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#12     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:689:9)
<asynchronous suspension>
#13     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
#14     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:619:5)
<asynchronous suspension>
#15     FlutterCommandRunner.run (package:flutter_tools/src/runner/flutter_command_runner.dart:379:5)
<asynchronous suspension>
#16     run.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:419:19)
<asynchronous suspension>
#17     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
#18     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
#19     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
#20     main (package:flutter_tools/executable.dart:133:3)
<asynchronous suspension>

Feature flags enabled: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-record-use, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration, enable-riscv64, enable-hcpp

Current output:

Screenshot written to flutter_01.skp (0kB).

Previously, `ErrorHandlingFile.readAsStringSync` dropped its `encoding` parameter when calling `delegate.readAsStringSync()`, forcing UTF-8 decoding even when callers requested another encoding (unlike `readAsString`, `readAsLines`, and `readAsLinesSync`, which already forwarded `encoding`). Because `flutter_tools` wraps `LocalFileSystem` in `ErrorHandlingFileSystem`, callers such as `ScreenshotCommand.ensureOutputIsNotJsonRpcError` crashed with a `FileSystemException` whenever a binary screenshot file under 1000 bytes was read with `AsciiCodec(allowInvalid: true)`.

Forward `encoding` to `delegate.readAsStringSync(encoding: encoding)` and add regression tests covering `ErrorHandlingFile.readAsStringSync` backed by `LocalFileSystem` and `ScreenshotCommand.ensureOutputIsNotJsonRpcError` on `ErrorHandlingFileSystem`.

Previous output:
```
Screenshot written to flutter_01.skp (0kB).

FileSystemException: Failed to decode data using encoding 'utf-8', path = '/tmp/repro_screenshot_skp.QPQNLT/flutter_01.skp'
#0      _File._tryDecode (dart:io/file_impl.dart:704:7)
#1      _File.readAsStringSync (dart:io/file_impl.dart:715:7)
#2      ForwardingFile.readAsStringSync (package:file/src/forwarding/forwarding_file.dart:99:16)
#3      ErrorHandlingFile.readAsStringSync.<anonymous closure> (package:flutter_tools/src/base/error_handling_io.dart:258:22)
#4      _runSync (package:flutter_tools/src/base/error_handling_io.dart:1116:16)
#5      ErrorHandlingFile.readAsStringSync (package:flutter_tools/src/base/error_handling_io.dart:257:12)
#6      ScreenshotCommand.ensureOutputIsNotJsonRpcError (package:flutter_tools/src/commands/screenshot.dart:198:39)
flutter#7      ScreenshotCommand.runSkia (package:flutter_tools/src/commands/screenshot.dart:180:5)
<asynchronous suspension>
flutter#8      ScreenshotCommand.runCommand (package:flutter_tools/src/commands/screenshot.dart:123:19)
<asynchronous suspension>
flutter#9      FlutterCommand.run.<anonymous closure> (package:flutter_tools/src/runner/flutter_command.dart:876:27)
<asynchronous suspension>
flutter#10     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
flutter#11     CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
flutter#12     FlutterCommandRunner.runCommand.<anonymous closure> (package:flutter_tools/src/runner/flutter_command_runner.dart:689:9)
<asynchronous suspension>
flutter#13     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
flutter#14     FlutterCommandRunner.runCommand (package:flutter_tools/src/runner/flutter_command_runner.dart:619:5)
<asynchronous suspension>
flutter#15     FlutterCommandRunner.run (package:flutter_tools/src/runner/flutter_command_runner.dart:379:5)
<asynchronous suspension>
flutter#16     run.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter_tools/runner.dart:419:19)
<asynchronous suspension>
flutter#17     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
flutter#18     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
flutter#19     AppContext.run.<anonymous closure> (package:flutter_tools/src/base/context.dart:174:19)
<asynchronous suspension>
flutter#20     main (package:flutter_tools/executable.dart:133:3)
<asynchronous suspension>

Feature flags enabled: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-record-use, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration, enable-riscv64, enable-hcpp
```

Current output:
```
Screenshot written to flutter_01.skp (0kB).
```
@bkonyi bkonyi added the CICD Run CI/CD label Nov 6, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Nov 6, 2026
@bkonyi
bkonyi requested a review from chingjun November 9, 2026 18:57
@bkonyi
bkonyi marked this pull request as ready for review November 9, 2026 18:57

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates ErrorHandlingFile.readAsStringSync to forward the encoding parameter to its delegate, and adds corresponding tests. The review feedback suggests refactoring the unit tests to use MemoryFileSystem instead of LocalFileSystem to avoid physical disk I/O and simplify test setup.

Comment on lines +2075 to +2091
testWithoutContext('forwards $encodingName to LocalFileSystem delegate', () {
final localFileSystem = LocalFileSystem.test(signals: FakeSignals());
addTearDown(localFileSystem.dispose);
final Directory tempDir = localFileSystem.systemTempDirectory.createTempSync(
'flutter_tools_read_as_string_sync_test.',
);
addTearDown(() => tryToDelete(tempDir));

final fileSystem = ErrorHandlingFileSystem(
delegate: localFileSystem,
platform: linuxPlatform,
);
final File file = fileSystem.file(fileSystem.path.join(tempDir.path, 'binary.bin'))
..writeAsBytesSync(invalidUtf8Bytes);

expect(file.readAsStringSync(encoding: encoding), expected);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using LocalFileSystem in unit tests introduces unnecessary physical disk I/O, which can slow down test execution and lead to flakiness or leftover temporary directories if cleanup fails. Since ErrorHandlingFileSystem is a wrapper that behaves identically regardless of the underlying file system implementation, we can use MemoryFileSystem instead. This simplifies the test setup by removing the need for temporary directory creation, manual cleanup, and teardown registration.

      testWithoutContext('forwards $encodingName to MemoryFileSystem delegate', () {
        final fileSystem = ErrorHandlingFileSystem(
          delegate: MemoryFileSystem.test(),
          platform: linuxPlatform,
        );
        final File file = fileSystem.file('binary.bin')
          ..writeAsBytesSync(invalidUtf8Bytes);

        expect(file.readAsStringSync(encoding: encoding), expected);
      });
References
  1. Suggest simplification and refactoring to enhance readability and maintainability, and prefer hermetic tests that avoid unnecessary physical disk I/O. (link)

@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 11, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 11, 2026
Merged via the queue into flutter:master with commit 0e1f50f Nov 12, 2026
29 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 12, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 12, 2026
flutter/flutter@b98afb5...9c427b5

2026-11-12 [email protected] Roll Skia from d67d0f22e3ec to 9aa2513a4ac9 (2 revisions) (flutter/flutter#196429)
2026-11-12 [email protected] [flutter_tools] Move CpuArch to flutter_tools_core and define HostPlatform by osName and cpuArch (flutter/flutter#195841)
2026-11-12 [email protected] Roll Skia from 23e7e63789b8 to d67d0f22e3ec (2 revisions) (flutter/flutter#196424)
2026-11-12 [email protected] Uber compactify (flutter/flutter#196392)
2026-11-12 [email protected] Roll Skia from 3d7e335da165 to 23e7e63789b8 (12 revisions) (flutter/flutter#196422)
2026-11-12 [email protected] Roll Dart SDK from 13af329ea4f2 to e5add7a25b32 (1 revision) (flutter/flutter#196413)
2026-11-12 [email protected] Don't terminate the shared EGL display when an engine is disposed (flutter/flutter#194520)
2026-11-11 [email protected] Adds manifest flag to turn on SDFs for Android. (flutter/flutter#196056)
2026-11-11 [email protected] Remove bringup from Linux_pixel_7pro integration_ui_keyboard_inset_jump (flutter/flutter#196334)
2026-11-11 [email protected] Uses system default locale for text rendering when preferred locale isn't supported (flutter/flutter#195997)
2026-11-11 [email protected] [android]: prewarms shaders when the context is created (flutter/flutter#196069)
2026-11-11 [email protected] iOS: Recomposite platform views only if their parameters change (flutter/flutter#196287)
2026-11-11 [email protected] [flutter_tools] Forward encoding in ErrorHandlingFile.readAsStringSync (flutter/flutter#196136)
2026-11-11 [email protected] Roll Skia from b89c24bbab8c to 3d7e335da165 (8 revisions) (flutter/flutter#196390)
2026-11-11 [email protected] Add Flutter Gardener instructions on how to disable a test (flutter/flutter#196312)
2026-11-11 [email protected] [Impeller] Enable malioc analysis for framebuffer_blend shaders (flutter/flutter#196343)
2026-11-11 [email protected] [flutter_tools] Migrate general.shard Android, asset, and build tests off globals (flutter/flutter#196125)
2026-11-11 [email protected] Roll Dart SDK from 8f15c4ca2d41 to 13af329ea4f2 (1 revision) (flutter/flutter#196386)
2026-11-11 [email protected] [Impeller] Do not clamp the local coverage limit to the root render target (flutter/flutter#195860)
2026-11-11 [email protected] [Impeller] Use separate uniform buffers on VMware SVGA3D (flutter/flutter#196022)
2026-11-11 [email protected] [flutter_tools] Introduce AndroidSdkProvider for lazy native_assets SDK lookup (flutter/flutter#196122)
2026-11-11 [email protected] Moves linux to ThinLTO. (flutter/flutter#196314)
2026-11-11 [email protected] [flutter_tools] Migrate bin/fuchsia_tester.dart off globals and runInContext (flutter/flutter#196074)
2026-11-11 [email protected] Changes the tolerances for frame_rate_main_test.dart. (flutter/flutter#196304)
2026-11-11 [email protected] Roll Packages from 08faf64 to 653628e (16 revisions) (flutter/flutter#196381)
2026-11-11 [email protected] Allow SliverCrossAxisGroup children to lay out with zero cross-axis extent instead of asserting (flutter/flutter#191287)
2026-11-11 [email protected] Roll Skia from cc4c54f752ba to b89c24bbab8c (4 revisions) (flutter/flutter#196375)
2026-11-11 [email protected] [Linux] Fix layer offset when compositing with glBlitFramebuffer (flutter/flutter#196189)
2026-11-11 [email protected] [flutter_tools] Inject CrashReportSender into SwiftPM migration (flutter/flutter#196315)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[flutter_tools] ErrorHandlingFile.readAsStringSync drops encoding parameter

2 participants