fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.29k stars 301 forks source link

Format the dart output #2411

Closed yukibtc closed 2 days ago

yukibtc commented 2 days ago

Description

Hi, I'm using flutter_rust_bridge with the build.rs to automatically generate the dart output. I noticed that also if the rust APIs aren't changed and the dart output is already updated, it's re-genereated a new output "unformatted". If I commit those changes after few time it's re-generated formatted.

// build.rs

use std::process::{Command, Stdio};

use lib_flutter_rust_bridge_codegen::codegen;
use lib_flutter_rust_bridge_codegen::codegen::Config;

fn main() {
    println!("cargo:rerun-if-changed=src/api");

    if is_flutter_installed() {
        // Execute code generator with auto-detected config
        codegen::generate(
            Config::from_config_file("../flutter_rust_bridge.yaml")
                .unwrap()
                .unwrap(),
            Default::default(),
        )
        .unwrap();
    } else {
        eprintln!("Warning: flutter not installed.");
    }
}

fn is_flutter_installed() -> bool {
    let output = Command::new("flutter")
        .arg("--version")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status();
    matches!(output, Ok(status) if status.success())
}

The final output (formatted) that is re-generated with many whitespaces:

// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.6.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'protocol/event.dart';

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<_Client>>
abstract class Client implements RustOpaqueInterface {
  Future<bool> addRelay({required String url});

  Future<void> connect();

  static Client default_() =>
      RustLib.instance.api.crateApiClientClientDefault();

  Future<String> sendEvent({required Event event});
}
// This file is automatically generated, so please do not edit it.
// @generated by `flutter_rust_bridge`@ 2.6.0.

// ignore_for_file: invalid_use_of_internal_member, unused_import, unnecessary_import

import '../frb_generated.dart';
import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';
import 'protocol/event.dart';

                // Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<_Client>>
                abstract class Client implements RustOpaqueInterface {
                     Future<bool>  addRelay({required String url });

 Future<void>  connect();

static Client  default_()=>RustLib.instance.api.crateApiClientClientDefault();

 Future<String>  sendEvent({required Event event });

                }

Describe the solution you'd like

Always format the dart output or add an field in Config to choose if format the code.

welcome[bot] commented 2 days ago

Hi! Thanks for opening your first issue here! :smile:

fzyzcjy commented 2 days ago

Firstly, build.rs approach has some limitations. For example, macro expansions may not work (because expanding macros need compilation thus a naive implementation will lead to infinite loop).

Secondly, could you please collect and show the logs? I suspect it is because the codegen somehow fails in the middle of generation, otherwise formatting should be always done.

yukibtc commented 2 days ago

2024-11-19.log

fzyzcjy commented 2 days ago

Ok looks like it is

Error details: Err(Command execution failed: sh: 1: dart: not found

more full logs:

``` [2024-11-19T13:04:35.894Z WARN /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/commands/command_runner.rs:153] command=cd "/home/user/Documents/rust-nostr/nostr/bindings/nostr-sdk-flutter/lib/src/rust" && "sh" "-c" "\"dart\" \"fix\" \"--apply\" \".\"" stdout= stderr=sh: 1: dart: not found [2024-11-19T13:04:35.894Z WARN /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/codegen/polisher/mod.rs:91] Fail to execute_dart_fix, but continue to run. Error details: Err(Command execution failed: sh: 1: dart: not found ) [2024-11-19T13:04:35.895Z DEBUG /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/commands/dart_format.rs:24] execute dart_format paths=["lib/src/rust/api/protocol/event.dart", "lib/src/rust/frb_generated.io.dart", "lib/src/rust/api/client.dart", "lib/src/rust/frb_generated.dart", "lib/src/rust/api/protocol/key/public_key.dart", "lib/src/rust/api/protocol/key/secret_key.dart", "lib/src/rust/api/protocol/key.dart", "lib/src/rust/frb_generated.web.dart"] line_length=80 [2024-11-19T13:04:35.895Z DEBUG /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/commands/command_runner.rs:129] execute command: bin=sh args="-c \"dart\" \"format\" \"--line-length\" \"80\" \"lib/src/rust/api/protocol/event.dart\" \"lib/src/rust/frb_generated.io.dart\" \"lib/src/rust/api/client.dart\" \"lib/src/rust/frb_generated.dart\" \"lib/src/rust/api/protocol/key/public_key.dart\" \"lib/src/rust/api/protocol/key/secret_key.dart\" \"lib/src/rust/api/protocol/key.dart\" \"lib/src/rust/frb_generated.web.dart\"" current_dir=Some("/home/user/Documents/rust-nostr/nostr/bindings/nostr-sdk-flutter") cmd=cd "/home/user/Documents/rust-nostr/nostr/bindings/nostr-sdk-flutter" && "sh" "-c" "\"dart\" \"format\" \"--line-length\" \"80\" \"lib/src/rust/api/protocol/event.dart\" \"lib/src/rust/frb_generated.io.dart\" \"lib/src/rust/api/client.dart\" \"lib/src/rust/frb_generated.dart\" \"lib/src/rust/api/protocol/key/public_key.dart\" \"lib/src/rust/api/protocol/key/secret_key.dart\" \"lib/src/rust/api/protocol/key.dart\" \"lib/src/rust/frb_generated.web.dart\"" [2024-11-19T13:04:35.896Z WARN /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/commands/command_runner.rs:153] command=cd "/home/user/Documents/rust-nostr/nostr/bindings/nostr-sdk-flutter" && "sh" "-c" "\"dart\" \"format\" \"--line-length\" \"80\" \"lib/src/rust/api/protocol/event.dart\" \"lib/src/rust/frb_generated.io.dart\" \"lib/src/rust/api/client.dart\" \"lib/src/rust/frb_generated.dart\" \"lib/src/rust/api/protocol/key/public_key.dart\" \"lib/src/rust/api/protocol/key/secret_key.dart\" \"lib/src/rust/api/protocol/key.dart\" \"lib/src/rust/frb_generated.web.dart\"" stdout= stderr=sh: 1: dart: not found [2024-11-19T13:04:35.896Z WARN /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/codegen/polisher/mod.rs:91] Fail to execute_dart_format, but continue to run. Error details: Err(Command execution failed: sh: 1: dart: not found ) [2024-11-19T13:04:35.896Z DEBUG /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/commands/format_rust.rs:9] execute format_rust paths=["src/frb_generated.rs"] [2024-11-19T13:04:35.897Z DEBUG /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bridge_codegen-2.6.0/src/library/commands/command_runner.rs:129] execute command: bin=sh args="-c \"rustfmt\" \"--edition\" \"2018\" \"src/frb_generated.rs\"" current_dir=Some("/home/user/Documents/rust-nostr/nostr/bindings/nostr-sdk-flutter/rust") cmd=cd "/home/user/Documents/rust-nostr/nostr/bindings/nostr-sdk-flutter/rust" && "sh" "-c" "\"rustfmt\" \"--edition\" \"2018\" \"src/frb_generated.rs\"" [2024-11-19T13:04:36.000Z DEBUG /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/flutter_rust_bri ```
fzyzcjy commented 2 days ago

I guess this is related to how you setup the dart binary. i.e. sh seems to be confused and cannot find that binary when executed from build.rs.

yukibtc commented 2 days ago

Ah, thanks! Weird, dart is installed. I guess again an issue with RustRover, when it auto check the crates (the last time not found flutter binary). Thanks!

fzyzcjy commented 2 days ago

You are welcome and happy to see it is solved!