Skip to content

Convenient syntax for nested import #2148

Closed

Description

@ntjess

Description

It would be highly convenient to allow full folders to exist as local packages:

#import "@local/my-templates/proposal/nsf.typ": *

Currently, this results in the following error:

error: `templates/proposal/nsf` is not a valid package name

Use Case

I have one templates folder with several options:

templates
├───slides
|   ├───dark.typ
|   └───light.typ
└───proposal
    ├───nsf.typ
    └───sbir.typ

Currently, the best workaround is to use a relative import:

#import "../../../templates/slides/dark.typ": *

This is not ideal because (1) it is sensitive to the location of the main document and (2) requires typst to be run with --root to work.

I would love to symlink this folder to typst/packages/local and access it as a "package" to increase reusability. Even if I were to "publish" templates as a package to make this happen, it would still require "flattening" each template into something individually accessible since only one entrypoint can be specified:

#import "@local/templates:0.1.0": slides-dark
// Even so, this does not allow for star-imports of *only* the "slides/dark" template helpers

It seems like potential solutions are to:

  • Allow multiple entrypoints
  • Allow the entrypoint to be a keyword like "folder" or "none" or something similar that indicates there are subpackages
  • Allow arbitrary folders to be placed in local without complaining that they are nonconformant packages

Activity

  1. added
    feature request
    on Sep 15, 2023
  2. laurmaedje commented on Sep 15, 2023

    @laurmaedje
    Member

    It was an intentional design decision that a package only exposes its definitions and not its directory tree. Otherwise, pretty much any change to a package would be potentially breaking. The current syntax isn't super convenient for nested packages, but you could have something like this:

    #import "@local/templates:0.1.0": slides
    #import slides: dark
    #import dark: *

    We could think about a way to better support nested imports.

  3. changed the title [-]Feature request: directory walking in packages[/-] [+]Directly access package as directory[/+] on Sep 15, 2023
  4. added
    scripting
    packages
    on Sep 15, 2023
  5. ntjess commented on Sep 15, 2023

    @ntjess
    Author

    That makes sense; I was thinking since packages versions must be pinned on import there's not a large worry of breaking changes. my background is Python, where by default all files and objects are reachable but authors use underscores in the name to denote internal utilities / unstable things. I suppose backwards compatibility will be more of a concern if #1632 is added

    Alternatively, how do you feel about allowing entrypoints in the toml to be an array? It might involve the following specification:

    • When entrypoints is a string keep the current behavior
    • When entrypoints is an array,
      • #import package:0.1.0" is an error
      • #import package:0.1.0/foo" specifies the "foo.typ" entrypoint (any other separator could do)
    • When entrypoints is a dict,
      • {default=""} indicates which entrypoint is used in the case of #import "package:0.1.0"
      • {modules=[]} indicate candidates for array usage

    More food for thought: Perhaps a {datadir}/typst/path.conf file specifies additional directories that can be #imported.

    Of course these are mere suggestions; I don't know how much this increases complexity and you may have already eliminated these ideas in the past for reasons I didn't consider.

    My main objective is to increase reusability of company-specific templates and utilities vs. a relative import. Your solution is an acceptable workaround in the meantime, though

  6. laurmaedje commented on Sep 15, 2023

    @laurmaedje
    Member

    What I like about the current setup is that the internal package organization is very flexible. Assuming we had better syntax to allow something like the following:

    #import "@local/templates:0.1.0": slides.dark.*

    Would that satisfy your use case? Or do you still think direct file access has tangible benefits over this.

  7. ntjess commented on Sep 15, 2023

    @ntjess
    Author

    That works too! It has no verbosity overhead compared to raw file access 👍
    (in contrast to the triple-import that is currently required)

  8. laurmaedje commented on Sep 15, 2023

    @laurmaedje
    Member

    Okay, I think we can make this a thing!

  9. changed the title [-]Directly access package as directory[/-] [+]Convenient syntax for nested import[/+] on Sep 15, 2023
  10. added
    good contribution
    on Sep 18, 2023
  11. ntjess commented on Sep 20, 2023

    @ntjess
    Author

    If I were to use

    #import "@local/templates:0.1.0": slides.dark.my-dark-theme

    What would be the proper usage later?

    #show: my-dark-theme
    
    // OR
    
    #show: slides.dark.my-dark-theme

    In other words, is the variable bound to the last name in the chain or the fully qualified path? I have a preference toward the latter, since it matches what was specified by the user and they can simply combine it with an "as" import to get the other behavior:

    #import @local/templates:0.1.0": slides.dark.my-dark-theme as my-dark-theme
    #show: my-dark-theme

    This matches Python behavior FWIW.

  12. laurmaedje commented on Sep 20, 2023

    @laurmaedje
    Member

    I would say, it should definitely be the former, because if you want the latter you can just write #import "@local/templates:0.1.0": slides.

  13. ntjess commented on Sep 20, 2023

    @ntjess
    Author

    I am not a rust programmer, but through searching the repo I think this is where the change would occur? Is this something a novice can make reasonable progress on?

    Thanks for the quick replies!

  14. laurmaedje commented on Sep 20, 2023

    @laurmaedje
    Member

    On the parser side, you've found the right place. You would also need adjustments in ast.rs and maybe kind.rs on the syntax side. On the evaluation side, the relevant code is in eval/mod.rs in the apply_imports functions.

    Is this something a novice can make reasonable progress on?

    I believe yes. You might hit some tricky spots, but if you've got a bit of time I think it should be doable. I'm also happy to help out if you hit any roadblocks or design questions (and the same holds true for the people in #contributors on Discord).

  15. ramithuh commented on Nov 16, 2023

    @ramithuh

    Hi @ntjess! I noticed that you have a nsf.typ file (which I hopefully assume is a proposal template for NSF)
    I came across a tweet [1] that was searching for such template 🥹

    If it's a template that's sharable, perhaps it could be very helpful to the person searching for it on Twitter. Apologies if this comes off as prying (I tried searching "nsf" on discord and came across this issue)

    [1] https://twitter.com/nomad421/status/1723912916833804351

  16. ntjess commented on Nov 17, 2023

    @ntjess
    Author

    @ramithuh I was inspired by your request to open source my existing NSF templates: https://github.com/ntjess/typst-nsf-templates.

    I don't use twitter, but you are welcome to point them in that direction 🙂

  17. PgBiel commented on Apr 29, 2024

    @PgBiel
    Contributor

    Will be working on this! (Commenting so I can be assigned 🙂)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature requestgood contributionpackagesscripting

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions