Skip to content

Logging Overhaul #2308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 85 commits into
base: master
Choose a base branch
from
Draft

Logging Overhaul #2308

wants to merge 85 commits into from

Conversation

patmuk
Copy link
Contributor

@patmuk patmuk commented Sep 20, 2024

Changes

Please list issues fixed by this PR here, using format "Fixes #the-issue-number".

Checklist

  • An issue to be fixed by this PR is listed above.
  • New tests are added to ensure new features are working. Please refer to this page to see how to add a test.
  • ./frb_internal precommit --mode slow (or fast) is run (it internal runs code generator, does auto formatting, etc).
  • If this PR adds/changes features, documentations (in the ./website folder) are updated.
  • CI is passing. Please refer to this page to see how to solve a failed CI.

Remark for PR creator

  • ./frb_internal --help shows utilities for development.
  • If fzyzcjy does not reply for a few days, maybe he just did not see it, so please ping him.

@patmuk
Copy link
Contributor Author

patmuk commented Sep 20, 2024

@fzyzcjy I cleaned up the branch ... that is, I needed to create a new branch. As I could not swap the branch from the old PR (#2303) I created this new one.

Will close the old one if this (macro) approach is working :)

@patmuk
Copy link
Contributor Author

patmuk commented Sep 20, 2024

I solved the dependency issue, but I have a code generation issue now:

In the macro code, in frb_rust/src/log_2_dart.rs I get:

  --> /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_rust/src/log_2_dart.rs:81:17
   |
81 |             use frb_generated::StreamSink;
   |                 ^^^^^^^^^^^^^ use of undeclared crate or module `frb_generated`

Clearly, in this code I have not generated code. But I need to refer to the StreamSink, which should be generated ...

Do you have an advice how to progress?

  1. I could copy the generated code for the StreamSink to the macro as well - but this way we are close (back?) to the non-macro way in PR Logging overhaul #2303
  2. I could probably inject it as a parameter of the macro ... but then the StreamSink needs to be defined in the users project - not a very clean solution
  3. statically calling code generation?

I am sure you have a better idea :) It is a bit cat-and-mouse: The logger code should be generated (actually integrated), but it needs the StreamSink to be generated to work ...

As for solving the log dependency:
For using log::info!() in the user's rust code, he needs to use flutter_rust_bridge::log_2_dart::log; (with no modification on the cargo.toml, or use log;, but then importing the crate in the cargo.toml. While the latter can be done when creating a project the first time (in the cargo.toml template you pointed me to), it is a breaking change that needs to be migrated for users with existing projects.

Nevertheless, I want to leave this decision open until I see if use flutter_rust_bridge::log_2_dart is needed anyways - maybe to solve the problem above.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Sep 21, 2024

In the macro code

Standard Rust would need crate::frb_generated::... to find the frb_geneated.rs

@patmuk
Copy link
Contributor Author

patmuk commented Sep 21, 2024

Standard Rust would need crate::frb_generated::... to find the frb_geneated.rs

The problem is, that there is no struct StreamSink declared yet. It is generated later.

error[E0432]: unresolved import `crate::frb_generated`
 --> /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_rust/src/log_2_dart.rs:4:12
  |
4 | use crate::frb_generated::StreamSink;
  |            ^^^^^^^^^^^^^ could not find `frb_generated` in the crate root
error[E0412]: cannot find type `StreamSink` in this scope
  --> /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_rust/src/log_2_dart.rs:87:30
   |
87 |                 stream_sink: StreamSink<Log2DartLogRecord>,
   |                              ^^^^^^^^^^ not found in this scope
   |
  ::: /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_rust/src/misc/user_utils.rs:10:5
   |
10 |     setup_logging!();
   |     ---------------- in this macro invocation
   |
   = note: this error originates in the macro `setup_logging` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0412]: cannot find type `StreamSink` in this scope
  --> /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_rust/src/log_2_dart.rs:92:29
   |
92 |                 log_stream: StreamSink<Log2DartLogRecord>,
   |                             ^^^^^^^^^^ not found in this scope
   |
  ::: /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_rust/src/misc/user_utils.rs:10:5
   |
10 |     setup_logging!();
   |     ---------------- in this macro invocation
   |
   = note: this error originates in the macro `setup_logging` (in Nightly builds, run with -Z macro-backtrace for more info)
Some errors have detailed explanations: E0412, E0432.
For more information about an error, try `rustc --explain E0412`.

@github-actions github-actions bot removed the Stale label Feb 28, 2025
@github-actions github-actions bot added the Stale label Apr 29, 2025
@github-actions github-actions bot closed this May 6, 2025
@patmuk
Copy link
Contributor Author

patmuk commented May 6, 2025

Still don't have time ... could you please again reopen this PR?

@fzyzcjy fzyzcjy reopened this May 6, 2025
@fzyzcjy
Copy link
Owner

fzyzcjy commented May 6, 2025

Sure and looking forward to it!

@github-actions github-actions bot removed the Stale label May 7, 2025
@patmuk patmuk force-pushed the logging_overhaul branch from 25328b2 to 56fc3e8 Compare May 15, 2025 09:10
@patmuk
Copy link
Contributor Author

patmuk commented May 15, 2025

@fzyzcjy I found some time to update this PR and finalize it :) Currently I am merging the latest master - but I ran into a problem testing the code :(

I am running
dart --enable-experiment=native-assets run lib/main.dart in frb_example/dart_minimal, but I am getting errors and wonder if you get them as well?

Since I am getting the same errors in the latest master and in my branch before the merge and upgrading the tooling, I am quite sure it is because of the tooling ... but I don't know why.
I am using

› dart --version                                          
Dart SDK version: 3.7.2 (stable) (Tue Mar 11 04:27:50 2025 -0700) on "macos_arm64"

The error I am getting is:

› dart --enable-experiment=native-assets run lib/main.dart
"/Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/hook/build.dart" file not found.
Building native assets for package:frb_example_dart_minimal failed.
Compilation of hook returned with exit code: 255.
To reproduce run:
/Users/patmuk/code/own/oss/frb/.toolchain/flutter-local/flutter/bin/cache/dart-sdk/bin/dart compile kernel --packages=/Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/.dart_tool/package_config.json --output=/Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/.dart_tool/native_assets_builder/frb_example_dart_minimal/36bad3131645e96b3e5f196e7b126c8d/hook.dill --depfile=/Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/.dart_tool/native_assets_builder/frb_example_dart_minimal/36bad3131645e96b3e5f196e7b126c8d/hook.dill.d /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/hook/build.dart
stderr:
"/Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/hook/build.dart" file not found.

stdout:

        
Error: Compiling native assets failed.

flutter/flutter#138727 mentions this problem with dart-lang/sdk#54111 solving it by having the build.dart in hook/build.dart.
But I don't know if this build file is intended to get cargo build running?

But when I remove the file I get

› dart --enable-experiment=native-assets run lib/main.dart
Unhandled exception:
Bad state: Content hash on Dart side (742269791) is different from Rust side (-2119384465), indicating out-of-sync code. This may happen when, for example, the Dart code is hot-restarted/hot-reloaded without recompiling Rust code. (Note: This is just a sanity check. Even if content hash does not change, the code may still change and needs to be recompiled)
#0      BaseEntrypoint._sanityCheckContentHash (package:flutter_rust_bridge/src/main_components/entrypoint.dart:108:7)
#1      BaseEntrypoint.initImpl (package:flutter_rust_bridge/src/main_components/entrypoint.dart:52:5)
<asynchronous suspension>
#2      RustLib.init (package:frb_example_dart_minimal/src/rust/frb_generated.dart:27:5)
<asynchronous suspension>
#3      main (package:frb_example_dart_minimal/main.dart:8:3)
<asynchronous suspension>

when I move it to hook/build.dart

 dart --enable-experiment=native-assets run lib/main.dart
> cargo build --release (pwd: /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/rust, env: {})
    Finished `release` profile [optimized] target(s) in 0.13s
> cargo build --release (pwd: /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/rust, env: {})
    Finished `release` profile [optimized] target(s) in 0.02s
> cargo build --release (pwd: /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/rust, env: {})
    Finished `release` profile [optimized] target(s) in 0.02s
> cargo build --release (pwd: /Users/patmuk/code/own/oss/frb/flutter_rust_bridge_fork_pat/frb_example/dart_minimal/rust, env: {})
    Finished `release` profile [optimized] target(s) in 0.02s

cargo build runs in an endless loop.

However, the dart issue tickets I mentioned are over a year old - I wonder how this is impacting now? Something seems very off with my setup, but I can't figure out what. Gemini (surprisingly good with supporting flutter coding) suggests to modify the build.dart file quite heavily - but that can't be the solution, if it runs fine for others.

@patmuk
Copy link
Contributor Author

patmuk commented May 16, 2025

It seems not to compile in CI either:
https://github.com/fzyzcjy/flutter_rust_bridge/actions/runs/15052130037/job/42309299553

[2025-05-15T18:50:23.075Z INFO frb_codegen/src/library/commands/fvm.rs:15] Has .fvmrc but no fvm binary installation, thus skip using fvm.
[2025-05-15T18:50:23.198Z WARN frb_codegen/src/library/commands/command_runner.rs:161] command=cd "/Users/runner/work/flutter_rust_bridge/flutter_rust_bridge/frb_example/dart_minimal/rust" && RUSTFLAGS="-C instrument-coverage --cfg=coverage --cfg=trybuild_no_target --cfg frb_expand" "cargo" "expand" "--lib" "--theme=none" "--ugly" stdout= stderr=error: no such command: `expand`

	View all installed commands with `cargo --list`
	Find a package to install `expand` with `cargo search cargo-expand`

[2025-05-15T18:50:23.198Z INFO frb_codegen/src/library/commands/cargo_expand/real.rs:111] Cargo expand is not installed. Automatically install and re-run.
[2025-05-15T18:55:15.769Z INFO frb_codegen/src/library/commands/fvm.rs:15] Has .fvmrc but no fvm binary installation, thus skip using fvm.
[2025-05-15T18:55:23.648Z INFO frb_codegen/src/library/commands/fvm.rs:15] Has .fvmrc but no fvm binary installation, thus skip using fvm.
[2025-05-15T18:55:24.652Z WARN frb_codegen/src/library/codegen/polisher/auto_upgrade.rs:27] Auto upgrader find wrong Dart/Rust flutter_rust_bridge dependency version, please enable `auto_upgrade_dependencies` flag or upgrade manually.
Done!

    Finished report saved to /Users/runner/work/flutter_rust_bridge/flutter_rust_bridge/target/coverage/GenerateRunFrbCodegenCommandGenerate/codecov.json
transformCodecovReport act on /Users/runner/work/flutter_rust_bridge/flutter_rust_bridge/target/coverage/GenerateRunFrbCodegenCommandGenerate/codecov.json
> /bin/sh -c git diff --exit-code  (pwd: /Users/runner/work/flutter_rust_bridge/flutter_rust_bridge/, env: {CARGO_TERM_COLOR: always})

@patmuk
Copy link
Contributor Author

patmuk commented May 16, 2025

Oh, I see that his is just running the code generation. I don't see any test for running the project.
https://github.com/fzyzcjy/flutter_rust_bridge/actions/runs/15052130037/job/42309302248 runs dart test successfully.

@patmuk patmuk force-pushed the logging_overhaul branch from 4227107 to 87a599b Compare May 16, 2025 09:39
@fzyzcjy
Copy link
Owner

fzyzcjy commented May 16, 2025

About hook file, could you please try to make Dart version same as the one on CI - iirc the newer dart changes hook mechanism and I have not had time to upgrade the examples to support that (note this only affects examples and not real users)

Copy link

codecov bot commented May 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.87%. Comparing base (8447c39) to head (3f1ab4e).

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #2308       +/-   ##
===========================================
- Coverage   98.84%   55.87%   -42.98%     
===========================================
  Files         469      465        -4     
  Lines       20500    19294     -1206     
===========================================
- Hits        20264    10781     -9483     
- Misses        236     8513     +8277     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@patmuk
Copy link
Contributor Author

patmuk commented May 16, 2025

About hook file, could you please try to make Dart version same as the one on CI - iirc the newer dart changes hook mechanism and I have not had time to upgrade the examples to support that (note this only affects examples and not real users)

Oh, yes, good suggestion! Somehow I did not think about that ... doing that right now.

@patmuk
Copy link
Contributor Author

patmuk commented May 16, 2025

downgrading dart worked :) It builds again locally. Will continue later.

@patmuk
Copy link
Contributor Author

patmuk commented May 17, 2025

I could run dart-minimal locally, and it looks like all tests in pure-dart, except the ones a socket, are passing locally.

But in CI I am getting errors in form of

> /bin/sh -c git diff --exit-code  (pwd: /Users/runner/work/flutter_rust_bridge/flutter_rust_bridge/, env: {CARGO_TERM_COLOR: always})
diff --git a/frb_example/pure_dart/rust/Cargo.lock b/frb_example/pure_dart/rust/Cargo.lock
index eef23f3..90669fe 100644
--- a/frb_example/pure_dart/rust/Cargo.lock
+++ b/frb_example/pure_dart/rust/Cargo.lock
@@ -17,15 +17,6 @@ version = "1.0.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"

(...)

Unhandled exception:
ProcessException: Bad exit code (1). If you want to see extra information, set FRB_DART_RUN_COMMAND_STDERR=1
  Command: /bin/sh -c git diff --exit-code 
#0      runCommand (package:flutter_rust_bridge/src/cli/run_command.dart:67:5)
<asynchronous suspension>
#1      SimpleExecutor.call (package:flutter_rust_bridge_internal/src/utils/makefile_dart_infra.dart:32:12)
<asynchronous suspension>
#2      _maybeSetExitIfChanged (package:flutter_rust_bridge_internal/src/makefile_dart/generate.dart:427:5)
<asynchronous suspension>
#3      wrapMaybeSetExitIfChangedRaw (package:flutter_rust_bridge_internal/src/makefile_dart/generate.dart:419:3)
<asynchronous suspension>
#4      testDartNative.<anonymous closure> (package:flutter_rust_bridge_internal/src/makefile_dart/test.dart:360:7)
<asynchronous suspension>
#5      withLlvmCovReport (package:flutter_rust_bridge_internal/src/makefile_dart/test.dart:405:15)
<asynchronous suspension>
#6      testDartNative (package:flutter_rust_bridge_internal/src/makefile_dart/test.dart:339:3)
<asynchronous suspension>
#7      SimpleConfigCommand.run (package:flutter_rust_bridge_internal/src/utils/makefile_dart_infra.dart:88:31)
<asynchronous suspension>
#8      CommandRunner.runCommand (package:args/command_runner.dart:212:13)
<asynchronous suspension>
#9      main (file:///Users/runner/work/flutter_rust_bridge/flutter_rust_bridge/tools/frb_internal/bin/flutter_rust_bridge_internal.dart:30:3)
<asynchronous suspension>
Error: Process completed with exit code 255.

What does this mean? How can I move further?

@patmuk
Copy link
Contributor Author

patmuk commented May 17, 2025

All tests of pure-dart are passing locally :)
(with

› dart --enable-experiment=native-assets --enable-vm-service  test 

)

Do you know what the CI errors mean?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants