Skip to content

Commit

Permalink
feat: added MISE_ORIGINAL_CWD to tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Nov 26, 2024
1 parent 5e8d801 commit d9bdc8a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/tasks/file-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,20 @@ Completions will now be enabled for your task. In this example, `mise run build
will show `debug` and `release` as options. The `--user` flag will also show completions generated by the output of `mycli users`.

(Note that cli and markdown help for tasks is not yet implemented in mise as of this writing but that is planned.)

## CWD

mise sets the current working directory to the directory of `mise.toml` before running tasks.
This can be overridden by setting `dir="{{cwd}}"` in the task header:

```bash
#!/usr/bin/env bash
#MISE dir="{{cwd}}"
```

Also, the original working directory is available in the `MISE_ORIGINAL_CWD` environment variable:

```bash
#!/usr/bin/env bash
cd "$MISE_ORIGINAL_CWD"
```
11 changes: 11 additions & 0 deletions docs/tasks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ as `~/src/work/myproject/mise.local.toml` and they will be used inside tasks of

As of this writing vars are only supported in TOML tasks. I want to add support for file tasks, but
I don't want to turn all file tasks into tera templates just for this feature.

## Environment variables passed to tasks

The following environment variables are passed to the task:

- `MISE_ORIGINAL_CWD`: The original working directory from where the task was run.
- `MISE_CONFIG_ROOT`: The directory containing the `mise.toml` file where the task was defined.
- `MISE_PROJECT_ROOT`: The root of the project.
- `MISE_TASK_NAME`: The name of the task being run.
- `MISE_TASK_DIR`: The directory containing the task script.
- `MISE_TASK_FILE`: The full path to the task script.
13 changes: 13 additions & 0 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ impl Run {
..Default::default()
})?;
let mut env = ts.env_with_path(&CONFIG)?;
if let Some(cwd) = &*dirs::CWD {
env.insert("MISE_ORIGINAL_CWD".into(), cwd.display().to_string());
}
if let Some(root) = &CONFIG.project_root {
env.insert("MISE_PROJECT_ROOT".into(), root.display().to_string());
env.insert("root".into(), root.display().to_string());
Expand Down Expand Up @@ -300,6 +303,16 @@ impl Run {
return Ok(());
}

let mut env = env.clone();
env.insert("MISE_TASK_NAME".into(), task.name.clone());
let task_file = task.file.as_ref().unwrap_or(&task.config_source);
env.insert("MISE_TASK_FILE".into(), task_file.display().to_string());
if let Some(dir) = task_file.parent() {
env.insert("MISE_TASK_DIR".into(), dir.display().to_string());
}
if let Some(config_root) = &task.config_root {
env.insert("MISE_CONFIG_ROOT".into(), config_root.display().to_string());
}
let string_env: Vec<(String, String)> = task
.env
.iter()
Expand Down

0 comments on commit d9bdc8a

Please sign in to comment.