Skip to content

Commit

Permalink
Removes export env command (#6468)
Browse files Browse the repository at this point in the history
* remove export_env command

* remove several export env usage in test code

* adjust hiding relative test case

* fix clippy

* adjust tests

* update tests

* unignore these tests to expose ut failed

* using `use` instead of `overlay use` in some tests

* Revert "using `use` instead of `overlay use` in some tests"

This reverts commit 2ae24b2.

* Revert "adjust hiding relative test case"

This reverts commit 4369af6.

* Bring back module example

* Revert "update tests"

This reverts commit 6ae94ef.

* Fix tests

* "Fix" a test

* Remove remaining deprecated env functionality

* Re-enable environment hiding for `hide`

To not break virtualenv since the overlay update is not merged yet

* Fix hiding env in `hide` and ignore some tests

Co-authored-by: kubouch <kubouch@gmail.com>
  • Loading branch information
WindSoilder and kubouch committed Sep 25, 2022
1 parent 7f21b7f commit b47bd22
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 539 deletions.
62 changes: 0 additions & 62 deletions crates/nu-command/src/core_commands/export_env.rs

This file was deleted.

84 changes: 8 additions & 76 deletions crates/nu-command/src/core_commands/hide.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};

#[derive(Clone)]
Expand Down Expand Up @@ -40,12 +40,15 @@ This command is a parser keyword. For details, check:
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let import_pattern = if let Some(Expression {
let env_var_name = if let Some(Expression {
expr: Expr::ImportPattern(pat),
..
}) = call.positional_nth(0)
{
pat
Spanned {
item: String::from_utf8_lossy(&pat.head.name).to_string(),
span: pat.head.span,
}
} else {
return Err(ShellError::GenericError(
"Unexpected import".into(),
Expand All @@ -56,78 +59,7 @@ This command is a parser keyword. For details, check:
));
};

let head_name_str = if let Ok(s) = String::from_utf8(import_pattern.head.name.clone()) {
s
} else {
return Err(ShellError::NonUtf8(import_pattern.head.span));
};

if let Some(module_id) = engine_state.find_module(&import_pattern.head.name, &[]) {
// The first word is a module
let module = engine_state.get_module(module_id);

let env_vars_to_hide = if import_pattern.members.is_empty() {
module.env_vars_with_head(&import_pattern.head.name)
} else {
match &import_pattern.members[0] {
ImportPatternMember::Glob { .. } => module.env_vars(),
ImportPatternMember::Name { name, span } => {
let mut output = vec![];

if let Some((name, id)) =
module.env_var_with_head(name, &import_pattern.head.name)
{
output.push((name, id));
} else if !(module.has_alias(name) || module.has_decl(name)) {
return Err(ShellError::EnvVarNotFoundAtRuntime(
String::from_utf8_lossy(name).into(),
*span,
));
}

output
}
ImportPatternMember::List { names } => {
let mut output = vec![];

for (name, span) in names {
if let Some((name, id)) =
module.env_var_with_head(name, &import_pattern.head.name)
{
output.push((name, id));
} else if !(module.has_alias(name) || module.has_decl(name)) {
return Err(ShellError::EnvVarNotFoundAtRuntime(
String::from_utf8_lossy(name).into(),
*span,
));
}
}

output
}
}
};

for (name, _) in env_vars_to_hide {
let name = if let Ok(s) = String::from_utf8(name.clone()) {
s
} else {
return Err(ShellError::NonUtf8(import_pattern.span()));
};

if stack.remove_env_var(engine_state, &name).is_none() {
return Err(ShellError::NotFound(
call.positional_nth(0)
.expect("already checked for present positional")
.span,
));
}
}
} else if !import_pattern.hidden.contains(&import_pattern.head.name)
&& stack.remove_env_var(engine_state, &head_name_str).is_none()
{
// TODO: we may want to error in the future
}
stack.remove_env_var(engine_state, &env_var_name.item);

Ok(PipelineData::new(call.head))
}
Expand Down
2 changes: 0 additions & 2 deletions crates/nu-command/src/core_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod export;
mod export_alias;
mod export_def;
mod export_def_env;
mod export_env;
mod export_extern;
mod export_use;
mod extern_;
Expand Down Expand Up @@ -43,7 +42,6 @@ pub use export::ExportCommand;
pub use export_alias::ExportAlias;
pub use export_def::ExportDef;
pub use export_def_env::ExportDefEnv;
pub use export_env::ExportEnvModule;
pub use export_extern::ExportExtern;
pub use export_use::ExportUse;
pub use extern_::Extern;
Expand Down
4 changes: 2 additions & 2 deletions crates/nu-command/src/core_commands/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl Command for Module {
}),
},
Example {
description: "Define an environment variable in a module and evaluate it",
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
description: "Define an environment variable in a module",
example: r#"module foo { export-env { let-env FOO = "BAZ" } }; use foo; $env.FOO"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
Expand Down
31 changes: 9 additions & 22 deletions crates/nu-command/src/core_commands/overlay/hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ impl Command for OverlayHide {
.optional("name", SyntaxShape::String, "Overlay to hide")
.switch(
"keep-custom",
"Keep all newly added symbols within the next activated overlay",
"Keep all newly added commands and aliases in the next activated overlay",
Some('k'),
)
.named(
"keep-env",
SyntaxShape::List(Box::new(SyntaxShape::String)),
"List of environment variables to keep from the hidden overlay",
"List of environment variables to keep in the next activated overlay",
Some('e'),
)
.category(Category::Core)
Expand Down Expand Up @@ -67,23 +67,7 @@ impl Command for OverlayHide {
let keep_env: Option<Vec<Spanned<String>>> =
call.get_flag(engine_state, stack, "keep-env")?;

let env_vars_to_keep = if call.has_flag("keep-custom") {
if let Some(overlay_id) = engine_state.find_overlay(overlay_name.item.as_bytes()) {
let overlay_frame = engine_state.get_overlay(overlay_id);
let origin_module = engine_state.get_module(overlay_frame.origin);

stack
.get_overlay_env_vars(engine_state, &overlay_name.item)
.into_iter()
.filter(|(name, _)| !origin_module.has_env_var(name.as_bytes()))
.collect()
} else {
return Err(ShellError::OverlayNotFoundAtRuntime(
overlay_name.item,
overlay_name.span,
));
}
} else if let Some(env_var_names_to_keep) = keep_env {
let env_vars_to_keep = if let Some(env_var_names_to_keep) = keep_env {
let mut env_vars_to_keep = vec![];

for name in env_var_names_to_keep.into_iter() {
Expand All @@ -110,10 +94,13 @@ impl Command for OverlayHide {
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Hide an overlay created from a module",
description: "Keep a custom command after hiding the overlay",
example: r#"module spam { export def foo [] { "foo" } }
overlay use spam
overlay hide spam"#,
def bar [] { "bar" }
overlay hide spam --keep-custom
bar
"#,
result: None,
},
Example {
Expand All @@ -125,7 +112,7 @@ impl Command for OverlayHide {
},
Example {
description: "Hide the last activated overlay",
example: r#"module spam { export env FOO { "foo" } }
example: r#"module spam { export-env { let-env FOO = "foo" } }
overlay use spam
overlay hide"#,
result: None,
Expand Down
24 changes: 1 addition & 23 deletions crates/nu-command/src/core_commands/overlay/use_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,6 @@ impl Command for OverlayUse {

let module = engine_state.get_module(module_id);

for (name, block_id) in module.env_vars() {
let name = if let Ok(s) = String::from_utf8(name.clone()) {
s
} else {
return Err(ShellError::NonUtf8(call.head));
};

let block = engine_state.get_block(block_id);

let val = eval_block(
engine_state,
caller_stack,
block,
PipelineData::new(call.head),
false,
true,
)?
.into_value(call.head);

caller_stack.add_env_var(name, val);
}

// Evaluate the export-env block (if any) and keep its environment
if let Some(block_id) = module.env_block {
let maybe_path = find_in_dirs_env(&name_arg.item, engine_state, caller_stack)?;
Expand Down Expand Up @@ -212,7 +190,7 @@ impl Command for OverlayUse {
},
Example {
description: "Create an overlay from a file",
example: r#"echo 'export env FOO { "foo" }' | save spam.nu
example: r#"echo 'export-env { let-env FOO = "foo" }' | save spam.nu
overlay use spam.nu
$env.FOO"#,
result: None,
Expand Down
70 changes: 1 addition & 69 deletions crates/nu-command/src/core_commands/use_.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use nu_engine::{eval_block, find_in_dirs_env, redirect_env};
use nu_protocol::ast::{Call, Expr, Expression, ImportPatternMember};
use nu_protocol::ast::{Call, Expr, Expression};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Expand Down Expand Up @@ -58,66 +58,6 @@ impl Command for Use {
if let Some(module_id) = import_pattern.head.id {
let module = engine_state.get_module(module_id);

let env_vars_to_use = if import_pattern.members.is_empty() {
module.env_vars_with_head(&import_pattern.head.name)
} else {
match &import_pattern.members[0] {
ImportPatternMember::Glob { .. } => module.env_vars(),
ImportPatternMember::Name { name, span } => {
let mut output = vec![];

if let Some(id) = module.get_env_var_id(name) {
output.push((name.clone(), id));
} else if !module.has_decl(name) && !module.has_alias(name) {
return Err(ShellError::EnvVarNotFoundAtRuntime(
String::from_utf8_lossy(name).into(),
*span,
));
}

output
}
ImportPatternMember::List { names } => {
let mut output = vec![];

for (name, span) in names {
if let Some(id) = module.get_env_var_id(name) {
output.push((name.clone(), id));
} else if !module.has_decl(name) && !module.has_alias(name) {
return Err(ShellError::EnvVarNotFoundAtRuntime(
String::from_utf8_lossy(name).into(),
*span,
));
}
}

output
}
}
};

for (name, block_id) in env_vars_to_use {
let name = if let Ok(s) = String::from_utf8(name.clone()) {
s
} else {
return Err(ShellError::NonUtf8(import_pattern.head.span));
};

let block = engine_state.get_block(block_id);

let val = eval_block(
engine_state,
caller_stack,
block,
PipelineData::new(call.head),
false,
true,
)?
.into_value(call.head);

caller_stack.add_env_var(name, val);
}

// Evaluate the export-env block if there is one
if let Some(block_id) = module.env_block {
let block = engine_state.get_block(block_id);
Expand Down Expand Up @@ -184,14 +124,6 @@ impl Command for Use {
span: Span::test_data(),
}),
},
Example {
description: "Define an environment variable in a module and evaluate it",
example: r#"module foo { export env FOO_ENV { "BAZ" } }; use foo FOO_ENV; $env.FOO_ENV"#,
result: Some(Value::String {
val: "BAZ".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Define a custom command that participates in the environment in a module and call it",
example: r#"module foo { export def-env bar [] { let-env FOO_BAR = "BAZ" } }; use foo bar; bar; $env.FOO_BAR"#,
Expand Down
1 change: 0 additions & 1 deletion crates/nu-command/src/default_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub fn create_default_context() -> EngineState {
ExportCommand,
ExportDef,
ExportDefEnv,
ExportEnvModule,
ExportExtern,
ExportUse,
Extern,
Expand Down

0 comments on commit b47bd22

Please sign in to comment.