Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) enum Attribute<'src> {
value: Option<Expression<'src>>,
},
Cache {
environment: Option<Expression<'src>>,
extra: Option<Expression<'src>>,
inputs: Option<Expression<'src>>,
outputs: Option<Expression<'src>>,
Expand Down Expand Up @@ -228,6 +229,8 @@ impl<'src> Attribute<'src> {
AttributeKind::Arg => Self::new_arg(name, arguments, &mut keyword_arguments)?,
AttributeKind::Android => Self::Android,
AttributeKind::Cache => Self::Cache {
environment: Self::remove_required(&mut keyword_arguments, "environment")?
.map(|(_key, expression)| expression),
extra: Self::remove_required(&mut keyword_arguments, "extra")?
.map(|(_key, expression)| expression),
inputs: Self::remove_required(&mut keyword_arguments, "inputs")?
Expand Down Expand Up @@ -602,11 +605,15 @@ impl Display for Attribute<'_> {
| Self::Unix
| Self::Windows => {}
Self::Cache {
environment,
extra,
inputs,
outputs,
} => {
let mut arguments = Vec::new();
if let Some(environment) = environment {
arguments.push(format!("environment={environment}"));
}
if let Some(extra) = extra {
arguments.push(format!("extra={extra}"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/cache_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::*;
#[derive(Serialize)]
pub(crate) struct CacheKey<'a> {
pub(crate) body: &'a [String],
pub(crate) environment: &'a Environment,
pub(crate) environment: BTreeMap<String, Option<String>>,
pub(crate) executor: &'a Executor<'a>,
pub(crate) extension: Option<&'a str>,
pub(crate) extra: Option<Value>,
Expand Down
11 changes: 11 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ pub(crate) enum Error<'src> {
parameter: &'src str,
recipe: &'src str,
},
EnvVarUnicode {
name: String,
value: OsString,
},
EvalUnknownSubmodule {
component: String,
suggestion: Option<Suggestion<'src>>,
Expand Down Expand Up @@ -772,6 +776,13 @@ impl ColorDisplay for Error<'_> {
let editor = editor.to_string_lossy();
write!(f, "editor `{editor}` failed: {status}")?;
}
EnvVarUnicode { name, value } => {
write!(
f,
"environment variable `{name}` not unicode: `{}`",
value.to_string_lossy(),
)?;
}
EmptyListArgument { parameter, recipe } => {
write!(
f,
Expand Down
32 changes: 31 additions & 1 deletion src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ impl<'src> Recipe<'src> {

let (cache_lock, outputs) = if !config.no_cache
&& let Some(Attribute::Cache {
environment: environment_attribute,
extra,
inputs,
outputs,
Expand All @@ -578,6 +579,35 @@ impl<'src> Recipe<'src> {
None => env::current_dir().map_err(|source| Error::CurrentDirectory { source })?,
};

let environment_attribute = environment_attribute
.as_ref()
.map(|environment| evaluator.evaluate_value(environment))
.transpose()?;

let environment = if let Some(names) = environment_attribute {
let mut variables = BTreeMap::new();

for name in names {
let value = if let Some(value) = environment.variables.get(&name) {
value.clone()
} else {
match env::var(&name) {
Err(env::VarError::NotPresent) => None,
Err(env::VarError::NotUnicode(value)) => {
return Err(Error::EnvVarUnicode { name, value });
}
Ok(value) => Some(value),
}
};

variables.insert(name, value);
}

variables
} else {
environment.variables.clone()
};

let extra = extra
.as_ref()
.map(|extra| evaluator.evaluate_value(extra))
Expand Down Expand Up @@ -608,7 +638,7 @@ impl<'src> Recipe<'src> {

let key = CacheKey {
body: &evaluated_lines,
environment: &environment,
environment,
executor: &executor,
extension,
extra,
Expand Down
8 changes: 8 additions & 0 deletions src/unresolved_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,18 @@ impl<'src> UnresolvedRecipe<'src> {
}
}
Attribute::Cache {
environment,
extra,
inputs,
outputs,
} => {
if let Some(environment) = environment {
variable_resolver.resolve_expression(
environment,
&parameters,
&mut variable_references,
)?;
}
if let Some(extra) = extra {
variable_resolver.resolve_expression(extra, &parameters, &mut variable_references)?;
}
Expand Down
110 changes: 103 additions & 7 deletions tests/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ fn environment_invalidates_cache() {
",
)
.unstable()
.args(["value=bar", "foo"])
.arg("value=bar")
.stdout("bar\n")
.success()
.test()
.unstable()
.args(["value=baz", "foo"])
.arg("value=baz")
.stdout("baz\n")
.success();
}
Expand All @@ -204,12 +204,84 @@ fn unexported_variable_does_not_invalidate_cache() {
",
)
.unstable()
.args(["value=bar", "foo"])
.arg("value=bar")
.stdout("bar\n")
.success()
.test()
.unstable()
.args(["value=baz", "foo"])
.arg("value=baz")
.success();
}

#[test]
fn environment_variables_may_be_removed_from_cache_key() {
Test::new()
.justfile(
"
set lists

export value := 'default'

[cache(environment=[])]
[script]
foo:
echo $value
",
)
.unstable()
.arg("value=bar")
.stdout("bar\n")
.success()
.test()
.unstable()
.arg("value=baz")
.success();
}

#[test]
fn environment_variables_may_be_added_to_cache_key() {
Test::new()
.justfile(
"
set lists
[cache(environment=['value'])]
[script]
foo:
echo $value
",
)
.unstable()
.env("value", "bar")
.stdout("bar\n")
.success()
.test()
.unstable()
.env("value", "baz")
.stdout("baz\n")
.success();
}

#[test]
fn environment_may_be_expression() {
Test::new()
.justfile(
"
set lists
name := 'value'
[cache(environment=[name])]
[script]
foo:
echo $value
",
)
.unstable()
.env("value", "bar")
.stdout("bar\n")
.success()
.test()
.unstable()
.env("value", "baz")
.stdout("baz\n")
.success();
}

Expand Down Expand Up @@ -321,16 +393,16 @@ fn extra_invalidates_cache() {
",
)
.unstable()
.args(["value=a", "foo"])
.arg("value=a")
.stdout("bar\n")
.success()
.test()
.unstable()
.args(["value=a", "foo"])
.arg("value=a")
.success()
.test()
.unstable()
.args(["value=b", "foo"])
.arg("value=b")
.stdout("bar\n")
.success();
}
Expand Down Expand Up @@ -1131,6 +1203,30 @@ fn prints_cache_key() {
.success();
}

#[test]
fn cache_environment_variables_are_resolved() {
Test::new()
.justfile(
"
[cache(environment = undefined)]
[script('sh')]
foo:
echo bar
",
)
.unstable()
.stderr(
"
error: variable `undefined` not defined
——▶ justfile:1:22
1 │ [cache(environment = undefined)]
│ ^^^^^^^^^
",
)
.failure();
}

#[test]
fn cache_extra_variables_are_resolved() {
Test::new()
Expand Down