Skip to content

Allow overriding when an optional import took place #2523

@redsun82

Description

@redsun82

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

# ./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:

    # ./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.

    # ./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.

    # ./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

    # ./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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions