when spawned process during register plugin, pass env to child process (#6261)

* when spawned process during register plugin, pass env to child process

* tweak comment

* tweak comment

* remove trailing whitespace

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
WindSoilder 2022-08-08 20:26:49 +08:00 committed by GitHub
parent 2f0cb044a5
commit aaf5684f9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 2 deletions

1
Cargo.lock generated
View File

@ -2721,6 +2721,7 @@ dependencies = [
"itertools",
"log",
"miette 5.1.1",
"nu-engine",
"nu-path",
"nu-plugin",
"nu-protocol",

View File

@ -15,6 +15,7 @@ serde_json = "1.0"
nu-path = {path = "../nu-path", version = "0.66.3" }
nu-protocol = { path = "../nu-protocol", version = "0.66.3" }
nu-plugin = { path = "../nu-plugin", optional = true, version = "0.66.3" }
nu-engine = { path = "../nu-engine", version = "0.66.3" }
log = "0.4"
[features]

View File

@ -2623,7 +2623,8 @@ pub fn parse_register(
expand_aliases_denylist: &[usize],
) -> (Pipeline, Option<ParseError>) {
use nu_plugin::{get_signature, EncodingType, PluginDeclaration};
use nu_protocol::Signature;
use nu_protocol::{engine::Stack, Signature};
let cwd = working_set.get_cwd();
// Checking that the function is used with the correct name
@ -2784,6 +2785,11 @@ pub fn parse_register(
},
};
// We need the current environment variables for `python` based plugins
// Or we'll likely have a problem when a plugin is implemented in a virtual Python environment.
let stack = Stack::new();
let current_envs =
nu_engine::env::env_to_strings(working_set.permanent_state, &stack).unwrap_or_default();
let error = match signature {
Some(signature) => arguments.and_then(|(path, encoding)| {
signature.map(|signature| {
@ -2793,7 +2799,7 @@ pub fn parse_register(
})
}),
None => arguments.and_then(|(path, encoding)| {
get_signature(path.as_path(), &encoding, &shell)
get_signature(path.as_path(), &encoding, &shell, &current_envs)
.map_err(|err| {
ParseError::LabeledError(
"Error getting signatures".into(),

View File

@ -61,6 +61,10 @@ impl Command for PluginDeclaration {
// Create PipelineData
let source_file = Path::new(&self.filename);
let mut plugin_cmd = create_command(source_file, &self.shell);
// We need the current environment variables for `python` based plugins
// Or we'll likely have a problem when a plugin is implemented in a virtual Python environment.
let current_envs = nu_engine::env::env_to_strings(engine_state, stack).unwrap_or_default();
plugin_cmd.envs(current_envs);
let mut child = plugin_cmd.spawn().map_err(|err| {
let decl = engine_state.get_decl(call.decl_id);

View File

@ -1,6 +1,7 @@
mod declaration;
pub use declaration::PluginDeclaration;
use nu_engine::documentation::get_flags_section;
use std::collections::HashMap;
use crate::protocol::{CallInput, LabeledError, PluginCall, PluginData, PluginResponse};
use crate::EncodingType;
@ -117,9 +118,11 @@ pub fn get_signature(
path: &Path,
encoding: &EncodingType,
shell: &Option<PathBuf>,
current_envs: &HashMap<String, String>,
) -> Result<Vec<Signature>, ShellError> {
let mut plugin_cmd = create_command(path, shell);
plugin_cmd.envs(current_envs);
let mut child = plugin_cmd.spawn().map_err(|err| {
ShellError::PluginFailedToLoad(format!("Error spawning child process: {}", err))
})?;