Repository: lapce/lapce
File: lapce-proxy/src/plugin.rs (Rust)

Code before:
            .stdin(Box::new(input))
            .stdout(Box::new(output))
            .envs(env)
            .finalize()?;
        let wasi = wasi_env.import_object(&module)?;

        let plugin_env = PluginEnv {
            wasi_env,
            desc: plugin_desc.clone(),
            dispatcher,
        };
        let lapce = lapce_exports(&self.store, &plugin_env);
        let instance = wasmer::Instance::new(&module, &lapce.chain_back(wasi))?;
        let plugin = PluginNew {
            instance,
            env: plugin_env,
        };

        let local_plugin = plugin.clone();
        thread::spawn(move || {
            let initialize = local_plugin
                .instance
                .exports
                .get_function("initialize")
                .unwrap();
            wasi_write_object(
                &local_plugin.env.wasi_env,
                &PluginInfo {
                    os: std::env::consts::OS.to_string(),
                    arch: std::env::consts::ARCH.to_string(),
                    configuration: plugin_desc.configuration,
                },
            );
            initialize.call(&[]).unwrap();
        });

        Ok(plugin)
    }

    pub fn next_plugin_id(&mut self) -> PluginId {
        PluginId(self.id_counter.next())
    }
}

impl Default for PluginCatalog {
    fn default() -> Self {
        Self::new()
    }
}

pub(crate) fn lapce_exports(store: &Store, plugin_env: &PluginEnv) -> ImportObject {

Review comment (suggestion):
```suggestion
        let env = plugin_desc.get_plugin_env()?;
```

Assuming this compiles, of course :)

Code after:
        for (_, plugin) in self.items.clone().iter() {
            if let Ok(p) = self.start_plugin(dispatcher.clone(), plugin.clone()) {
                self.plugins.insert(plugin.name.clone(), p);
            }
        }
    }

    fn start_plugin(
        &mut self,
        dispatcher: Dispatcher,
        plugin_desc: PluginDescription,
    ) -> Result<Plugin> {
        let module = wasmer::Module::from_file(
            &self.store,
            plugin_desc
                .wasm
                .as_ref()
                .ok_or_else(|| anyhow!("no wasm in plugin"))?,
        )?;

        let output = Pipe::new();
        let input = Pipe::new();
        let env = plugin_desc.get_plugin_env()?;
        let mut wasi_env = WasiState::new("Lapce")
            .map_dir("/", plugin_desc.dir.clone().unwrap())?
            .stdin(Box::new(input))
            .stdout(Box::new(output))
            .envs(env)
            .finalize()?;
        let wasi = wasi_env.import_object(&module)?;

        let plugin_env = PluginEnv {
            wasi_env,
            desc: plugin_desc.clone(),
            dispatcher,
        };
        let lapce = lapce_exports(&self.store, &plugin_env);
        let instance = wasmer::Instance::new(&module, &lapce.chain_back(wasi))?;
        let plugin = Plugin {
            instance,
            env: plugin_env,
        };

        let local_plugin = plugin.clone();
        thread::spawn(move || {
            let initialize = local_plugin
                .instance
                .exports
                .get_function("initialize")
                .unwrap();
            wasi_write_object(