casey / casey/just

Allow overriding when an optional import took place

Open
#2,523 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
35.8k
Forks
846
Avg merge
27m
Merged PRs (30d)
3

Description

Hi!

I would like to be able to detect within a justfile whether an optional import took place.

I found out a workaround to do that within recipe bodies, namely
```just
# ./maybe.just
set export

IMPORTED := "true"

# ./justfile
import? 'maybe.just'

imported:
${IMPORTED:-false} && echo yes || echo no
```
however I did not find a way to have that information available in expressions.

If there's no way currently, here's a couple of things I tried that might be good features in their own right, and would solve this problem:
* when `allow-duplicate-variables` is set, one could have the position of the import be relevant for overriding. Reading the documentation of `allow-duplicate-variables`, I actually expected this to work:
```just
# ./maybe.just
imported := "true"
# ./justfile
imported := ""
import? "maybe.just"
default:
echo {{ if imported == "true" { "yes" } else { "no" } }}
```
If imports are allowed anywhere in a just file, I would expect duplicates from imports to override previous definitions (if duplicates are allowed).
If this held for recipes as well, I would probably even not need to use an `if`, and would just overwrite some recipes in `maybe.just`.
```just
# ./maybe.just
default:
echo yes
# ./justfile
default:
echo no
import? "maybe.just"
```
Afterwards I could see this goes directly in contrast with the way imports are specified in the documentation... so maybe this route could be taken with a new directive like `include`/`include?`?

* a `?` postfix operator could allow using undefined variables: `var?` would evaluate to the value of `var` if `var` is defined, and to `""` otherwise.
```just
# ./maybe.just
imported := "true"
# ./justfile
import? "maybe.just"
default:
echo {{ if imported? != "" { "yes" } else { "no" } }}
```
* an `imported` function. It could be specified to return `"true"` for modules that were imported
```just
# ./maybe.just

# ./justfile
import? "maybe.just"
default:
echo {{ if imported('maybe.just') == "true" { "yes" } else { "no" } }}
```

* for completeness, though I don't particularly like such a solution, an optional variable name after an `import?` could be filled with `"true"` or `""` depending on whether the import took place
```just
# ./maybe.just

# ./justfile
import? "maybe.just" imported
default:
echo {{ if imported == "true" { "yes" } else { "no" } }}
```

Contributor guide

Open the contributing guide

Research direction

Start by examining how optional `import?` directives, duplicate variables, and expression evaluation are currently handled. Compare the proposed undefined-variable, imported-function, and import-order approaches, then determine which behavior should be specified and tested before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.