1

I'm writing a small CLI app to set up my development environment based on a Jira ticket, currently I'm trying to open VS Code but I get a command not found error even though I can open VS Code from withing my terminal.

// jira.rs
use std::fmt;

pub struct JiraTicket {
    pub key: String,
    pub number: u32,
}

impl JiraTicket {
    pub fn new(key: String, number: u32) -> JiraTicket {
        JiraTicket { key, number }
    }
}

impl fmt::Display for JiraTicket {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}-{}", self.key, self.number)
    }
}

// command.rs
pub fn open_vscode(ticket: Option<&JiraTicket>) {
    let path = match ticket {
        Some(path) => path.to_string(),
        None => ".".to_string(),
    };
    let vscode_command = Command::new("code")
        .arg(path)
        .status()
        .expect("An error ocurred while opening vs code");
    if vscode_command.success() {
        println!("{}", "Opened VSCode".green());
    } else {
        println!("{}", "Failed to open VSCode".red());
    }
}

I already looked at:

Jeremy
  • 1,447
  • 20
  • 40
  • 1
    you need to add `.arg("/C")` as the first arg to launch it with cmd. – pigeonhands Feb 02 '23 at 07:09
  • 1
    I'm not sure if this is true on Windows, but maybe you can skip "cmd" altogether – jthulhu Feb 02 '23 at 07:46
  • I received an error when using just "\C" suggesting me to use r"\C", though I still got the same error – Jeremy Feb 02 '23 at 15:28
  • Yeah, I updated the code, I was using "cmd" just to see what could solve it. I updated the code to reflect it's current status – Jeremy Feb 02 '23 at 15:28
  • `Command` not found, `.green()` and `.red()` do not exist. Please provide a proper [MRE]. Not even a `main` exists ... how do you expect us to reproduce your problem if your code is not executable? – Finomnis Feb 02 '23 at 17:27

1 Answers1

1

"code.cmd" seems to work:

let vscode_command = Command::new("code.cmd")
        .arg(path)
        .status()
        .expect("An error ocurred while opening vs code");

I'm not sure why "code.cmd" works but "code" doesn't, though.

Might be related to rust-lang issue #37519.

Finomnis
  • 18,094
  • 1
  • 20
  • 27