Skip to content

Commit fd3aacf

Browse files
authored
Merge pull request #12271 from quarto-dev/execution-engine-env/add-filename
FR: set `QUARTO_DOCUMENT_FILE` to the path of the input file
2 parents f7076fe + d2cd890 commit fd3aacf

File tree

15 files changed

+197
-1
lines changed

15 files changed

+197
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
- `QUARTO_PROJECT_ROOT` points to the root of the project, or the directory of the file is not in project mode
22
- `QUARTO_DOCUMENT_PATH` points to the directory of the document being rendered
3+
- `QUARTO_DOCUMENT_FILE` points to the source filename of the document being rendered
34
- `QUARTO_PROFILE` is set to profile used, e.g `QUARTO_PROFILE=advanced,production` for `quarto render --profile advanced,production`

news/changelog-1.7.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,4 @@ All changes included in 1.7:
138138
- ([#12264](https://github.com/quarto-dev/quarto-cli/issues/12264)): Upgrade `dart-sass` to 1.85.1.
139139
- ([#11803](https://github.com/quarto-dev/quarto-cli/pull/11803)): Added a new CLI command `quarto call`. First users of this interface are the new `quarto call engine julia ...` subcommands.
140140
- A new folder `quarto-session-temp` can be created in `.quarto` to store temporary files created by Quarto during a rendering. Reminder: `.quarto` is for internal use of Quarto and should not be versioned (thus added to `.gitignore`).
141+
- ([#11606](https://github.com/quarto-dev/quarto-cli/discussions/11606)): Added a new `QUARTO_DOCUMENT_FILE` env var available to computation engine to the name of the file currently being rendered.

src/execute/environment.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2024 Posit Software, PBC
55
*/
66

7-
import { dirname } from "../deno_ral/path.ts";
7+
import { basename, dirname } from "../deno_ral/path.ts";
88
import { ExecuteOptions } from "./types.ts";
99
import { InternalError } from "../core/lib/error.ts";
1010

@@ -14,13 +14,17 @@ export const setExecuteEnvironment: (options: ExecuteOptions) => void = (
1414
if (options.projectDir) {
1515
Deno.env.set("QUARTO_PROJECT_ROOT", options.projectDir);
1616
Deno.env.set("QUARTO_DOCUMENT_PATH", dirname(options.target.source));
17+
Deno.env.set("QUARTO_DOCUMENT_FILE", basename(options.target.source));
1718
} else {
19+
// FIXME: This should not be passthrough anymore as singleFileProjectContext always set `options.projectDir`
20+
// https://github.com/quarto-dev/quarto-cli/pull/8771
1821
if (!options.cwd) {
1922
throw new InternalError(
2023
"No project directory or current working directory",
2124
);
2225
}
2326
Deno.env.set("QUARTO_PROJECT_ROOT", options.cwd);
2427
Deno.env.set("QUARTO_DOCUMENT_PATH", options.cwd);
28+
Deno.env.set("QUARTO_DOCUMENT_FILE", basename(options.target.source));
2529
}
2630
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project:
2+
type: default
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: "Testing env var available in execution in project context"
3+
format: markdown
4+
_quarto:
5+
tests:
6+
markdown:
7+
ensureFileRegexMatches:
8+
-
9+
- 'QUARTO_PROJECT_ROOT: \S+smoke-all[\\/]julia[\\/]execution-env-project'
10+
- 'QUARTO_DOCUMENT_PATH: \S+smoke-all[\\/]julia[\\/]execution-env-project'
11+
- 'QUARTO_DOCUMENT_FILE: execution-env-var.qmd'
12+
- ['UNDEFINED']
13+
---
14+
15+
#### Show execution env var
16+
17+
````{julia}
18+
#| echo: false
19+
#| output: asis
20+
function get_env(name, default)
21+
haskey(ENV, name) ? ENV[name] : default
22+
end
23+
24+
# Create the markdown output with the same formatting
25+
env_vars = """```{=markdown}
26+
QUARTO_PROJECT_ROOT: $(get_env("QUARTO_PROJECT_ROOT", "UNDEFINED"))
27+
QUARTO_DOCUMENT_PATH: $(get_env("QUARTO_DOCUMENT_PATH", "UNDEFINED"))
28+
QUARTO_DOCUMENT_FILE: $(get_env("QUARTO_DOCUMENT_FILE", "UNDEFINED"))
29+
```
30+
"""
31+
32+
print(env_vars)
33+
````
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: "Testing env var available in execution in single file project context"
3+
engine: julia
4+
format: markdown
5+
_quarto:
6+
tests:
7+
markdown:
8+
ensureFileRegexMatches:
9+
-
10+
- 'QUARTO_PROJECT_ROOT: \S+[\\/]smoke-all[\\/]julia'
11+
- 'QUARTO_DOCUMENT_PATH: \S+[\\/]smoke-all[\\/]julia'
12+
- 'QUARTO_DOCUMENT_FILE: execution-env-var.qmd'
13+
- ['UNDEFINED']
14+
---
15+
16+
#### Show execution env var
17+
18+
````{julia}
19+
#| echo: false
20+
#| output: asis
21+
function get_env(name, default)
22+
haskey(ENV, name) ? ENV[name] : default
23+
end
24+
25+
# Create the markdown output with the same formatting
26+
env_vars = """```{=markdown}
27+
QUARTO_PROJECT_ROOT: $(get_env("QUARTO_PROJECT_ROOT", "UNDEFINED"))
28+
QUARTO_DOCUMENT_PATH: $(get_env("QUARTO_DOCUMENT_PATH", "UNDEFINED"))
29+
QUARTO_DOCUMENT_FILE: $(get_env("QUARTO_DOCUMENT_FILE", "UNDEFINED"))
30+
```
31+
"""
32+
33+
print(env_vars)
34+
````
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project:
2+
type: default
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "Testing env var available in execution in project context"
3+
format: markdown
4+
_quarto:
5+
tests:
6+
markdown:
7+
ensureFileRegexMatches:
8+
-
9+
- 'QUARTO_PROJECT_ROOT: \S+smoke-all[\\/]jupyter[\\/]execution-env-project'
10+
- 'QUARTO_DOCUMENT_PATH: \S+smoke-all[\\/]jupyter[\\/]execution-env-project'
11+
- 'QUARTO_DOCUMENT_FILE: execution-env-var.qmd'
12+
- ['UNDEFINED']
13+
---
14+
15+
#### Show execution env var
16+
17+
```{python}
18+
#| echo: false
19+
#| output: asis
20+
import os
21+
from IPython.display import Markdown
22+
23+
env_vars = (
24+
"```{=markdown}\n"
25+
f"QUARTO_PROJECT_ROOT: {os.environ.get('QUARTO_PROJECT_ROOT', 'UNDEFINED')}\n"
26+
f"QUARTO_DOCUMENT_PATH: {os.environ.get('QUARTO_DOCUMENT_PATH', 'UNDEFINED')}\n"
27+
f"QUARTO_DOCUMENT_FILE: {os.environ.get('QUARTO_DOCUMENT_FILE', 'UNDEFINED')}\n"
28+
"```\n"
29+
)
30+
31+
Markdown(env_vars)
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: "Testing env var available in execution in single file project context"
3+
format: markdown
4+
_quarto:
5+
tests:
6+
markdown:
7+
ensureFileRegexMatches:
8+
-
9+
- 'QUARTO_PROJECT_ROOT: \S+[\\/]smoke-all[\\/]jupyter'
10+
- 'QUARTO_DOCUMENT_PATH: \S+[\\/]smoke-all[\\/]jupyter'
11+
- 'QUARTO_DOCUMENT_FILE: execution-env-var.qmd'
12+
- ['UNDEFINED']
13+
---
14+
15+
#### Show execution env var
16+
17+
```{python}
18+
#| echo: false
19+
#| output: asis
20+
import os
21+
from IPython.display import Markdown
22+
23+
env_vars = (
24+
"```{=markdown}\n"
25+
f"QUARTO_PROJECT_ROOT: {os.environ.get('QUARTO_PROJECT_ROOT', 'UNDEFINED')}\n"
26+
f"QUARTO_DOCUMENT_PATH: {os.environ.get('QUARTO_DOCUMENT_PATH', 'UNDEFINED')}\n"
27+
f"QUARTO_DOCUMENT_FILE: {os.environ.get('QUARTO_DOCUMENT_FILE', 'UNDEFINED')}\n"
28+
"```\n"
29+
)
30+
31+
Markdown(env_vars)
32+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project:
2+
type: default
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "Testing env var available in execution in project context"
3+
format: markdown
4+
_quarto:
5+
tests:
6+
markdown:
7+
ensureFileRegexMatches:
8+
-
9+
- 'QUARTO_PROJECT_ROOT: \S+smoke-all[\\/]knitr[\\/]execution-env-project'
10+
- 'QUARTO_DOCUMENT_PATH: \S+smoke-all[\\/]knitr[\\/]execution-env-project'
11+
- 'QUARTO_DOCUMENT_FILE: execution-env-var.qmd'
12+
- ['UNDEFINED']
13+
---
14+
15+
#### Show execution env var
16+
17+
```{r}
18+
#| echo: false
19+
#| output: asis
20+
knitr::raw_block(c(
21+
sprintf("QUARTO_PROJECT_ROOT: %s;\n",Sys.getenv('QUARTO_PROJECT_ROOT', 'UNDEFINED')),
22+
sprintf("QUARTO_DOCUMENT_PATH: %s\n",Sys.getenv('QUARTO_DOCUMENT_PATH', 'UNDEFINED')),
23+
sprintf("QUARTO_DOCUMENT_FILE: %s\n",Sys.getenv('QUARTO_DOCUMENT_FILE', 'UNDEFINED'))
24+
), type = "markdown")
25+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "Testing env var available in execution in single file Project context"
3+
format: markdown
4+
_quarto:
5+
tests:
6+
markdown:
7+
ensureFileRegexMatches:
8+
-
9+
- 'QUARTO_PROJECT_ROOT: \S+[\\/]smoke-all[\\/]knitr'
10+
- 'QUARTO_DOCUMENT_PATH: \S+[\\/]smoke-all[\\/]knitr'
11+
- 'QUARTO_DOCUMENT_FILE: execution-env-var.qmd'
12+
- ['UNDEFINED']
13+
---
14+
15+
#### Show execution env var
16+
17+
```{r}
18+
#| echo: false
19+
#| output: asis
20+
knitr::raw_block(c(
21+
sprintf("QUARTO_PROJECT_ROOT: %s;\n",Sys.getenv('QUARTO_PROJECT_ROOT', 'UNDEFINED')),
22+
sprintf("QUARTO_DOCUMENT_PATH: %s\n",Sys.getenv('QUARTO_DOCUMENT_PATH', 'UNDEFINED')),
23+
sprintf("QUARTO_DOCUMENT_FILE: %s\n",Sys.getenv('QUARTO_DOCUMENT_FILE', 'UNDEFINED'))
24+
), type = "markdown")
25+
```

0 commit comments

Comments
 (0)