无法从Rust执行Kotlin脚本

wkyowqbh  于 2023-03-30  发布在  Kotlin
关注(0)|答案(1)|浏览(115)

我尝试在Windows上从Rust中运行kotlinc。下面是我的代码的一小段:

let mut script_number: u32 = 0;
let script_path: String = format!("run/script-{script_number}.kts");

println!("Running Script: {script_number}");

let mut file = OpenOptions::new()
    .read(true)
    .write(true)
    .create(true)
    .open(script_path.clone())
    .expect("Could not create file");

file.write_all(
    self.editor
        .buffer()
        .expect("Couldn't read buffer.")
        .text()
        .as_bytes(),
)
.expect("Could not write to file");

let mut child = Command::new("./kotlinc")
    .arg("-script")
    .arg(script_path.clone())
    .stdout(Stdio::piped())
    .spawn()
    .expect("Could not execute script");

let stdout = child.stdout.take().expect("Could not read output");

let lines = BufReader::new(stdout).lines();

for line in lines {
    println!("{}", line.unwrap());
}

script_number += 1;

我得到以下输出和错误:

Running Script: 0

thread 'main' panicked at 'Could not execute script: Os { code: 5, kind: PermissionDenied, message: "Access is denied." }', src\main.rs:265:30
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library\std\src\panicking.rs:575      
   1: core::panicking::panic_fmt
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library\core\src\panicking.rs:65      
   2: core::result::unwrap_failed
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943/library\core\src\result.rs:1791       
   3: enum2$<core::result::Result<std::process::Child,std::io::error::Error> >::expect<std::process::Child,std::io::error::Error>
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943\library\core\src\result.rs:1070       
   4: gui_editor::App::launch
             at .\src\main.rs:260
   5: gui_editor::main
             at .\src\main.rs:291
   6: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at /rustc/69f9c33d71c871fc16ac445211281c6e7a340943\library\core\src\ops\function.rs:251  
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

我怀疑这是创建的文件没有执行权限,然而,在网上搜索后,我似乎无法找到一种方法,使文件执行权限。一些答案在线引用mode函数,但该函数似乎不存在,当我试图运行它。
只是为了上下文,run目录存储了计划由程序运行的脚本,它和kotlinc(Kotlin编译器的副本)目录都是Rust src目录的兄弟目录。

aor9mmx1

aor9mmx11#

我在kotlinc文件中找到了答案。需要运行的文件是./kotlinc/bin/kotlinc.bat,即:

std::process::Command::new("./kotlinc/bin/kotlinc.bat")
        .arg("-version")
        .stdout(Stdio::piped())
        .spawn()
        .expect("Could not execute script");

相关问题