Rust编写的CLI工具op首次在fish中使用补全时报错
问题:WSL下fish shell中自定义Rust工具'op'补全触发报错
问题现象
每次打开新的WSL终端,在fish shell 3.6.0中输入op 并按Tab触发补全时,会弹出如下报错:
op error: the directory does not exist
Oops: command not found
- (line 1):
Oops "./completion"
~~
from sourcing file -
called on line 1 of file /usr/share/fish/completions/op.fish
from sourcing file /usr/share/fish/completions/op.fish
(注:文本'op'为彩色,其余为白色)
bash中无此问题(因无补全功能),且报错仅首次出现,之后op可正常工作。参考Rust官方教程编写的'minigrep'工具无此问题。
相关文件内容
补全脚本 /usr/share/fish/completions/op.fish
op completion fish | source
工具核心代码(main.rs)
// This is a minimal frame use std::env; use std::path::Path; use std::process::Command; fn main() { // 0. get env args let args: Vec<String> = env::args().collect(); // 1. convert args[1] to Path let path = Path::new(&args[1]); // 2. parse path to parent and file_name let parent = path.parent().unwrap(); let file_name = path.file_name().unwrap().to_str().unwrap(); // 3. cd parent path env::set_current_dir(parent).unwrap(); // 4. execute open (using explorer.exe) cmd let cmd_open = format!("explorer.exe {file_name}"); Command::new("sh").arg("-c").arg(cmd_open).output().unwrap(); }
问题根源与解决方案
问题根源与Rust代码无关:/usr/share/fish/completions目录下存在同名的op.fish补全脚本(通常由其他同名软件生成),但自定义的op工具并未生成该补全脚本,导致补全逻辑冲突。
两种解决方法:
- 重新编译并给自定义工具重命名(不能用别名);
- 在
~/.config/fish/completions目录下创建一个空的op.fish文件,让fish优先加载该本地文件,覆盖系统目录中的同名脚本。
实际采用第二种方法解决了问题。
内容的提问来源于stack exchange,提问作者Gleko Ma
相关产品推荐
相关产品推荐

