Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpret imports relative to the location of the file containing them rather than the current working directory #915

Open
kostmo opened this issue Dec 14, 2022 · 5 comments
Labels
G-Scenarios An issue having to do with scenario design, the way scenarios are described and loaded, etc. S-Critical This is an issue that seriously affects playability or user experience. Z-Developer Experience This issue seeks to make life easier for developers writing Scenarios or other Swarm code. Z-Feature A new feature to be added to the game. Z-Temporary workaround Short-term solution intended to be deprecated

Comments

@kostmo
Copy link
Member

kostmo commented Dec 14, 2022

I'd like to distribute a self-contained demo "Gosling simulator" as a GitHub gist (practically speaking, a .zip file). This demo includes both a scenario (.yaml) file and multiple swarm-lang (.sw) files.

However, there's not a clear way in the scenario file to specify a path to scripts that is "relative" to the scenario file, such that the scenario can be run outside of the swarm/ directory. Paths in the run command seem to be relative to the current working directory.

Describe the solution you'd like

For a file hierarchy like this:

~/Downloads
~/Downloads/myscenario.yaml
~/Downloads/myscript.sw

and when swarm is invoked like this:

stack run -- --scenario ~/Downloads/myscenario.yaml

Support a notation like this in the REPL/swarm-lang code:

run "@myscript.sw"

The @ sign indicates that myscript.sw exists in the same directory as the scenario currently being played (in this case, the myscenario.yaml file).

For a hierarchy like this:

~/Downloads
~/Downloads/myscenario.yaml
~/Downloads/_myscenario/myscript.sw

The command would be:

run "@_myscenario/myscript.sw"
@kostmo kostmo added the Z-Feature A new feature to be added to the game. label Dec 14, 2022
@xsebek
Copy link
Member

xsebek commented Dec 15, 2022

Where is this notation used? Also, how would players learn about this feature?

Instead of hiding it by magical character, why not have :cd REPL command like GHCi?

   :cd <dir>                   change directory to <dir>

Or just use a normal function like Python:

>>> from os import chdir
>>> chdir("/tmp/")

@kostmo
Copy link
Member Author

kostmo commented Dec 15, 2022

I had this feature in mind mainly (strictly?) for scenario authors. For one, it would allow running the Gosling simulator "out of the box" after simply unpacking the .zip download. And two, it would allow simplification of paths like this, which would instead be written as:

run "@hanoi-count.sw"

And after that, for even better organization, all of the hanoi-related files (both .sw and .yaml) could be placed in a subdirectory. With the @-notation, no changes would be required to the scenario file after performing this relocation.

@byorgey
Copy link
Member

byorgey commented Dec 15, 2022

I am trying to imagine how this would work, practically speaking. Currently, the text value passed to the run command is interpreted at runtime when the run command is executed. I think at that point it is possible to know what scenario is being played and what directory it is in. So this would require modifying the code in Swarm.Game.Step that interprets the run command to strip off the leading @ and look up the current scenario path.

With that said, at some point (hopefully soon!) the run command will go away and be replaced by a much better import mechanism (#495). In that brave new world order, the argument to import will be processed at typechecking time rather than at runtime (see e.g. this comment), and it will be entirely natural for import paths to be interpreted relative to the location of the file containing them, just like Dhall does. In which case we will get exactly the behavior you want without the need for any special syntax.

Personally, I would be inclined to just wait for #495 (I've actually been working on that, although it's tricky and I can't give any specific promises when it will be ready). But if someone gets impatient they could implement this first.

@xsebek
Copy link
Member

xsebek commented Dec 17, 2022

At this point we could have a tag Feature that works around missing feature. 😆

@kostmo kostmo added the Z-Temporary workaround Short-term solution intended to be deprecated label Dec 17, 2022
@byorgey byorgey added G-Scenarios An issue having to do with scenario design, the way scenarios are described and loaded, etc. Z-Developer Experience This issue seeks to make life easier for developers writing Scenarios or other Swarm code. labels Sep 15, 2023
@kostmo kostmo changed the title Scenario-relative path notation for "run" command Scenario distribution format: Scenario-relative path notation for "run" command Jan 5, 2024
@byorgey byorgey changed the title Scenario distribution format: Scenario-relative path notation for "run" command Interpret imports relative to the location of the file containing them rather than the current working directory Oct 2, 2024
@byorgey byorgey added Bug The observed behaviour is incorrect or unexpected. and removed Bug The observed behaviour is incorrect or unexpected. labels Oct 2, 2024
@byorgey
Copy link
Member

byorgey commented Oct 2, 2024

Just an update --- I have been working in earnest on #495 and I can confirm that it will definitely have this behavior (interpreting import locations relative to the location of their containing file).

@byorgey byorgey added the S-Critical This is an issue that seriously affects playability or user experience. label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
G-Scenarios An issue having to do with scenario design, the way scenarios are described and loaded, etc. S-Critical This is an issue that seriously affects playability or user experience. Z-Developer Experience This issue seeks to make life easier for developers writing Scenarios or other Swarm code. Z-Feature A new feature to be added to the game. Z-Temporary workaround Short-term solution intended to be deprecated
Projects
None yet
Development

No branches or pull requests

3 participants