just icon indicating copy to clipboard operation
just copied to clipboard

Add module() function returning the current module's namepath

Open bluefing opened this issue 7 months ago • 6 comments

Hi @casey

Path introspection is supported via:

-justfile(), justfile_directory(), -module_file(), and module_directory()

but there's no way to get the current module's namepath

Please consider adding module()

Use case

I have shared recipes imported via import that need to know their module context to construct self-referencing just invocations. I am working around this by adding a manual _mod variable in every module:

  # _common.just — imported by all modules                                                                                                                                                                                                                                                 
  [default]                                                                                                                                                                                                                                                                              
  [private]                                                                                                                                                                                                                                                                                
  default:
    @just --list {{ _mod }} --unsorted                                                                                                                                                                                                                                                     
  # e.g. docker/compose/mod.just                                                                                                                                                                                                                                                                
  import '../_common.just'
  _mod := "docker::compose"   

This is one example, I have five (I think) common recipes that I have been able to avoid repeating in each module by using this approach.

With module(), the the common implementation could be self contained and I would not have to define _mod all over the place

  # _common.just                                                                                                                                                                                                                                                                         
  [default]   
  [private]
  default:
    @just --list {{ module() }} --unsorted

Behaviour

module() returns the namepath of the module in which the expression is evaluated. I believe this is consistent with module_file() which resolves relative to the executing module rather than the definition site.

Module module_file() returns module() would return
Root justfile ~/Justfile ""
mod foo "foo" ~/.just/foo/mod.just "foo"
mod bar "bar" in foo ~/.just/foo/bar/mod.just "foo::bar"
mod baz "baz" in foo::bar ~/.just/foo/bar/baz/mod.just "foo::bar::baz"
mod net "network" ~/.just/network/mod.just "net"

The last row illustrates that it is not always possible to derive the namepath from the module path

bluefing avatar Apr 06 '26 09:04 bluefing

@casey I'm conscious of the issue you raised re too many PRs, I hope you don't mind but I would like to have a go at implementing this one - I think it's an easy change - if it's not merged then no problem.

bluefing avatar Apr 06 '26 19:04 bluefing

@casey I can't create a PR - it's limited to collaborators

https://github.com/casey/just/compare/master...bluefing:just:feat/module-function

Adds a module() built-in function that returns the current module's namepath as a string:

  • Root justfile: "" (empty)
  • Module foo: "foo"
  • Submodule foo::bar: "foo::bar"

Complements existing module_file() and module_directory() functions. module() is useful for constructing module invocations without hardcoding module names.

Implementation

Single function in src/function.rs - reads modulepath from the execution context (same plumbing as module_file() module_directory() ).

Tests

Note: --evaluate without a variable name only dumps root-scope variables, not module-level ones - so I had to include multiple tests to evaluate each level by namepath individually. Docs suggest --evaluate should emit ALL variables so looks like --evaulate either needs doc update or needs a patch.

  • Extended module_paths test to include module() alongside module_file()/module_directory()
  • module_function_root - verifies root returns empty string via --evaluate
  • module_function_module - verifies single module returns "foo"
  • module_function_submodule - verifies nested module returns "foo::bar"
  • module_function_in_submodule - verifies echo output at module and submodule level

bluefing avatar Apr 07 '26 08:04 bluefing

created https://github.com/casey/just/issues/3263 to track --evaluate not emitting all variables

bluefing avatar Apr 07 '26 12:04 bluefing

This seems reasonable, I opened #3270 to add it. I'm uncertain about the name. module() is shorter, but possibly ambiguous. module_path() is maybe less ambiguous, but maybe people will think it's a file path.

casey avatar Apr 08 '26 00:04 casey

Thanks for implementing this.

Naming is hard, and hard to change later.

I don't have a strong feeling about the name, I'm not sure that the length matters as much as as clearly conveying behaviour/intent.

Given you use namepath elsewhere, maybe that would help disambiguate?

how about module_namepath()

bluefing avatar Apr 08 '26 11:04 bluefing

I went with module_path() it doesn't seem ideal, but I couldn't think of anything better. Namepath is really more of an internal name, used because each component of a Namepath is a Name which points to a source location.

casey avatar Apr 08 '26 23:04 casey