Skip to content

Commit

Permalink
Don't export constants (#2449)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 2, 2024
1 parent 8cdff48 commit 67034cb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use super::*;
/// A binding of `name` to `value`
#[derive(Debug, Clone, PartialEq, Serialize)]
pub(crate) struct Binding<'src, V = String> {
#[serde(skip)]
pub(crate) constant: bool,
pub(crate) export: bool,
#[serde(skip)]
pub(crate) file_depth: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/command_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl CommandExt for Command {
}

for binding in scope.bindings() {
if settings.export || binding.export {
if binding.export || (settings.export && !binding.constant) {
self.env(binding.name.lexeme(), &binding.value);
}
}
Expand Down
37 changes: 23 additions & 14 deletions src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ impl<'src, 'run> Evaluator<'src, 'run> {

for (name, value) in overrides {
if let Some(assignment) = module.assignments.get(name) {
scope.bind(
assignment.export,
assignment.name,
assignment.private,
value.clone(),
);
scope.bind(Binding {
constant: false,
export: assignment.export,
file_depth: 0,
name: assignment.name,
private: assignment.private,
value: value.clone(),
});
} else {
unknown_overrides.push(name.clone());
}
Expand Down Expand Up @@ -68,12 +70,14 @@ impl<'src, 'run> Evaluator<'src, 'run> {

if !self.scope.bound(name) {
let value = self.evaluate_expression(&assignment.value)?;
self.scope.bind(
assignment.export,
assignment.name,
assignment.private,
self.scope.bind(Binding {
constant: false,
export: assignment.export,
file_depth: 0,
name: assignment.name,
private: assignment.private,
value,
);
});
}

Ok(self.scope.value(name).unwrap())
Expand Down Expand Up @@ -340,9 +344,14 @@ impl<'src, 'run> Evaluator<'src, 'run> {
rest = &rest[1..];
value
};
evaluator
.scope
.bind(parameter.export, parameter.name, false, value);
evaluator.scope.bind(Binding {
constant: false,
export: parameter.export,
file_depth: 0,
name: parameter.name,
private: false,
value,
});
}

Ok((evaluator.scope, positional))
Expand Down
5 changes: 3 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,12 @@ impl<'run, 'src> Parser<'run, 'src> {
}

Ok(Assignment {
file_depth: self.file_depth,
constant: false,
export,
file_depth: self.file_depth,
name,
value,
private: private || name.lexeme().starts_with('_'),
value,
})
}

Expand Down
24 changes: 10 additions & 14 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ impl<'src, 'run> Scope<'src, 'run> {
};

for (key, value) in constants() {
root.bind(
false,
Name {
root.bind(Binding {
constant: true,
export: false,
file_depth: 0,
name: Name {
token: Token {
column: 0,
kind: TokenKind::Identifier,
Expand All @@ -34,22 +36,16 @@ impl<'src, 'run> Scope<'src, 'run> {
src: key,
},
},
false,
(*value).into(),
);
private: false,
value: (*value).into(),
});
}

root
}

pub(crate) fn bind(&mut self, export: bool, name: Name<'src>, private: bool, value: String) {
self.bindings.insert(Binding {
export,
file_depth: 0,
name,
private,
value,
});
pub(crate) fn bind(&mut self, binding: Binding<'src>) {
self.bindings.insert(binding);
}

pub(crate) fn bound(&self, name: &str) -> bool {
Expand Down
16 changes: 16 additions & 0 deletions tests/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ fn constants_can_be_redefined() {
.stdout("foo")
.run();
}

#[test]
fn constants_are_not_exported() {
Test::new()
.justfile(
"
set export
foo:
echo $HEXUPPER
",
)
.stderr_regex(".*HEXUPPER: unbound variable.*")
.status(127)
.run();
}

0 comments on commit 67034cb

Please sign in to comment.