Skip to content
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

flutter_rust_bridge_codegen crashes - When Rust's Command executes a shell command, system complains can not find the stated file #112

Closed
smw-wagnerma opened this issue Oct 22, 2021 · 11 comments
Labels
bug Something isn't working

Comments

@smw-wagnerma
Copy link

smw-wagnerma commented Oct 22, 2021

Describe the bug
Running the command flutter_rust_bridge_codegen --rust-input rust\src\api.rs --dart-output rust\src\bridge_generated.dart crashes on my system, the C headerfile is empty

[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Picked config: Opts { rust_input_path: "E:\\projects\\smarwag\\build\\cargo_check_ng\\rust\\src\\api.rs", dart_output_path: "E:\\projects\\smarwag\\build\\cargo_check_ng\\rust\\src\\bridge_generated.dart", c_output_path: "C:\\Users\\mw.EFC\\AppData\\Local\\Temp\\.tmpvw9u0m.h", rust_crate_dir: "E:\\projects\\smarwag\\build\\cargo_check_ng\\rust", rust_output_path: "E:\\projects\\smarwag\\build\\cargo_check_ng\\rust\\src\\bridge_generated.rs", class_name: "CargoLog", dart_format_line_length: 80 }
[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Phase: Parse source code to AST
[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Phase: Parse AST to IR
[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Phase: Transform IR
[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Phase: Generate Rust code
[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Phase: Generate Dart code
[2021-10-22T20:15:16Z INFO  flutter_rust_bridge_codegen] Phase: Other things
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "Das System kann die angegebene Datei nicht finden." }', C:\Users\mw.EFC\.cargo\registry\src\github.com-1ecc6299db9ec823\flutter_rust_bridge_codegen-1.2.0\src\commands.rs:21:35

Codegen logs

Run code generator with `RUST_LOG` environment variable set to `debug`, and paste them here.
#![allow(
    non_camel_case_types,
    unused,
    clippy::redundant_closure,
    clippy::useless_conversion
)]
// AUTO GENERATED FILE, DO NOT EDIT.
// Generated by `flutter_rust_bridge`.

use crate::api::*;
use flutter_rust_bridge::*;

// Section: wire functions

#[no_mangle]
pub extern "C" fn wire_off_topic_memory_test_output_vec_of_object(port: i64, len: i32) {
    FLUTTER_RUST_BRIDGE_HANDLER.wrap(
        WrapInfo {
            debug_name: "off_topic_memory_test_output_vec_of_object",
            port,
            mode: FfiCallMode::Normal,
        },
        move || {
            let api_len = len.wire2api();
            move |task_callback| off_topic_memory_test_output_vec_of_object(api_len)
        },
    );
}

// Section: wire structs

#[repr(C)]
#[derive(Clone)]
pub struct wire_list_String {
    ptr: *mut wire_uint_8_list,
    len: i32,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_uint_8_list {
    ptr: *mut u8,
    len: i32,
}

// Section: allocate functions

#[no_mangle]
pub extern "C" fn new_list_String(len: i32) -> *mut wire_list_String {
    let wrap = wire_list_String {
        ptr: support::new_leak_vec_ptr(<wire_uint_8_list>::new_with_null_ptr(), len),
        len,
    };
    support::new_leak_box_ptr(wrap)
}

#[no_mangle]
pub extern "C" fn new_uint_8_list(len: i32) -> *mut wire_uint_8_list {
    let ans = wire_uint_8_list {
        ptr: support::new_leak_vec_ptr(Default::default(), len),
        len,
    };
    support::new_leak_box_ptr(ans)
}

// Section: impl Wire2Api

pub trait Wire2Api<T> {
    fn wire2api(self) -> T;
}

impl Wire2Api<i32> for i32 {
    fn wire2api(self) -> i32 {
        self
    }
}

impl Wire2Api<Vec<String>> for *mut wire_list_String {
    fn wire2api(self) -> Vec<String> {
        let vec = unsafe {
            let wrap = support::box_from_leak_ptr(self);
            support::vec_from_leak_ptr(wrap.ptr, wrap.len)
        };
        vec.into_iter().map(Wire2Api::wire2api).collect()
    }
}

impl Wire2Api<String> for *mut wire_uint_8_list {
    fn wire2api(self) -> String {
        let vec: Vec<u8> = self.wire2api();
        String::from_utf8_lossy(&vec).into_owned()
    }
}

impl Wire2Api<Vec<u8>> for *mut wire_uint_8_list {
    fn wire2api(self) -> Vec<u8> {
        unsafe {
            let wrap = support::box_from_leak_ptr(self);
            support::vec_from_leak_ptr(wrap.ptr, wrap.len)
        }
    }
}

impl Wire2Api<u8> for u8 {
    fn wire2api(self) -> u8 {
        self
    }
}

// Section: impl NewWithNullPtr

pub trait NewWithNullPtr {
    fn new_with_null_ptr() -> Self;
}

impl<T> NewWithNullPtr for *mut T {
    fn new_with_null_ptr() -> Self {
        std::ptr::null_mut()
    }
}

// Section: impl IntoDart

// Section: executor
support::lazy_static! {
    pub static ref FLUTTER_RUST_BRIDGE_HANDLER: support::DefaultHandler = Default::default();
}

    // ----------- DUMMY CODE FOR BINDGEN ----------
    
    // copied from: allo-isolate
    pub type DartPort = i64;
    pub type DartPostCObjectFnType = unsafe extern "C" fn(port_id: DartPort, message: *mut std::ffi::c_void) -> bool;
    #[no_mangle] pub unsafe extern "C" fn store_dart_post_cobject(ptr: DartPostCObjectFnType) { panic!("dummy code") }
    
    // ---------------------------------------------
    

Development environment (please complete the following information):

  • OS: Windows
  • Version 10
  • Version of flutter_rust_bridge_codegen: 1.2.0
  • Paste output of flutter doctor -v:
  • [√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19043.1288], locale de-DE)
    • Flutter version 2.5.3 at E:\projects\smarwag\env\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (7 days ago), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at C:\Users\mw.EFC\AppData\Local\Android\sdk
• Platform android-31, build-tools 31.0.0
• Java binary at: E:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
• All Android licenses accepted.

[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.5)
• Visual Studio at E:\Program Files\Microsoft Visual Studio\2019\Community
• Visual Studio Community 2019 version 16.11.31729.503
• Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2020.3)
• Android Studio at E:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[√] VS Code (version 1.61.2)
• VS Code at C:\Users\mw.EFC\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.27.0

[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19043.1288]
• Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.81
• Edge (web) • edge • web-javascript • Microsoft Edge 91.0.864.48

• No issues found!

  • Version of clang++: 13
  • Version of cbindgen:0.20
  • Version of ffigen: 4.1.0

Additional context
Add any other context about the problem here.

@smw-wagnerma smw-wagnerma added the bug Something isn't working label Oct 22, 2021
@welcome
Copy link

welcome bot commented Oct 22, 2021

Hi! Thanks for opening your first issue here! 😄

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 23, 2021

Hi let me have a look

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 23, 2021

Look at thread 'main' panicked at 'called Result::unwrap()on anErr value: Os { code: 2, kind: NotFound, message: "Das System kann die angegebene Datei nicht finden." }', C:\Users\mw.EFC\.cargo\registry\src\github.com-1ecc6299db9ec823\flutter_rust_bridge_codegen-1.2.0\src\commands.rs:21:35.

Google translate says that sentence means The system can not find the stated file.

Thus, it seems that the command cannot be found. It may be caused by wrong environment settings. Indeed I simply use Rust's builtin Command utility to call a shell command.

Possibly related: #81 Could you please have a try on what is mentioned there?

By the way, please paste logs with RUST_LOG=debug environment variable set, such that we can see more details.

@fzyzcjy fzyzcjy changed the title flutter_rust_bridge_codegen crashes flutter_rust_bridge_codegen crashes - When Rust's Command executes a shell command, system complains can not find the stated file Oct 23, 2021
@smw-wagnerma
Copy link
Author

THX for your fast response.

Ok I see, I didn't realize it is the dart command, that is not found. I thought something with /,\ or \

This seems a wired thing, on the command line, all fine, dart is executed:

PS E:\projects\smarwag\build\testbug> dart pub global run ffigen
Running in Directory: 'E:\projects\smarwag\build\testbug'
[SEVERE] : Error: pubspec.yaml not found, please run this tool from the root of your package.

Sure it missed the yaml file, but dart is found.

Your test code

execute_command( Command::new("dart") .arg("pub") .arg("global") .arg("run") .arg("ffigen") );

fails also:

Runningtarget\debug\testbug.exeexecute command: "dart" "pub" "global" "run" "ffigen" thread 'main' panicked at 'calledResult::unwrap()on anErrvalue: Os { code: 2, kind: NotFound, message: "Das System kann die angegebene Datei nicht finden." }', src\main.rs:21:35

same error...

with spaning first the windows shell all is fine:

execute_command( Command::new("cmd") .arg("/C") .arg("dart") .arg("pub") .arg("global") .arg("run") .arg("ffigen"), );

now all is fine, dart is found, the same error about the yaml

Runningtarget\debug\testbug.exeexecute command: "cmd" "/C" "dart" "pub" "global" "run" "ffigen" command="cmd" "/C" "dart" "pub" "global" "run" "ffigen" output=Output { status: ExitStatus(ExitStatus(1)), stdout: "Running in Directory: 'E:\\projects\\smarwag\\build\\testbug'\r\n[SEVERE] : Error: pubspec.yaml not found, please run this tool from the root of your package.\n", stderr: "" } thread 'main' panicked at 'command execution failed. command="cmd" "/C" "dart" "pub" "global" "run" "ffigen"', src\main.rs:32:9

There seems some wired search things happening on windows, or it mus be an exe, in the flutter install, the dart command is a bat file...
I found a rust bug report about this: rust-lang/rust#37519

Anyway, a handling of the command like in the rust docs seems fixing it for me:

let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("echo hello")
.output()
.expect("failed to execute process")
};

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 23, 2021

Sounds reasonable! Let me add your suggestion ("cmd /C", and "sh -c") and push to master branch. You may try this after I push the new code and it passes CI.

@smw-wagnerma
Copy link
Author

Some more investigations of the problem, it seems it is the exe / bat thing in my case. Command on windows only spawns exe correct, not bat, like the flutter install of the dart.bat.

There is a fix in the rust pipeline, there seems also security problems in the actual behaviour:
rust-lang/rust#87704

This will hopefully fix the problem in rust, but if you can change it, fantastic, I will try it.

THX!!!

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 23, 2021

You are welcome! Let me change the code.

fzyzcjy added a commit that referenced this issue Oct 23, 2021
@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 23, 2021

@smw-wagnerma

Well I gave up. I do not have a windows computer with Rust/Dart, so I have to use the GitHub CI to see whether a fix works. Thus it is very time-consuming, and I cannot figure out what is happening (since I cannot use the shell in the CI).

The code is in this branch: https://github.com/fzyzcjy/flutter_rust_bridge/tree/rust-command . Could you please checkout this branch and try to fix it on your own machine? I have added the cmd /C and sh -c as you have suggested, so I guess the code should work with very minor modification.

GitHub
High-level memory-safe binding generator for Flutter/Dart <-> Rust - GitHub - fzyzcjy/flutter_rust_bridge at rust-command

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 23, 2021

Feel free to make a PR if you find out the minor change to make it work, and I will add you to the all-contributors!

@fzyzcjy
Copy link
Owner

fzyzcjy commented Oct 25, 2021

Close since #119 is merged. Feel free to open issue if the problem is still not solved!

@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants