Repository: lune-org/lune (Rust)

Issue:
Lune does not cache require results across files
The results of requiring a file is only cached in that specific file. This means that if two different scripts both require a script, it will run twice, which can be very problematic for modules that are supposed to contain a global state.

Simple reproduction:
test1.luau (file to run):
```lua
local result1 = require("./test3")
local result2 = require("./test3")
local result3 = require("./test2")
print(result1 == result2, result1 == result3) --> true, false
```
test2.luau:
```lua
return require("./test3")
```
test3.luau:
```lua
print("test3 ran") --> test3 ran (x2)

return {}
```


Discussion:
This bit me pretty hard recently. I was trying to create a Logging class which would be instantiated and returned in a `logging.luau` file to keep around a singleton. It took me a while to figure out that every time I required that file I was getting back a brand new Logging instance. I would really like to see support for module caching by default, with a way to bypass the cache in special situations (I think Jest would benefit from this in particular for its module mocking) 
Just ran into this trying to write a basic testing framework, is the only workaround to use true globals instead? 
I added a test that demonstrates the issue and does indeed fail, opened a draft PR with the test here: #170

I would not know how to even begin solving the issue, but hopefully the test is useful
Oh nice, looks like @kennethloeffler has a PR fixing this already, so speedy!

If for any reason someone is unable to use a lune version that has the fix in the future, I made a little workaround that is quite un-intrusive and still works with lsp (luau-lsp at least).

Gist for the workaround module here: https://gist.github.com/itsfrank/c3e193338612eb00324dc2340aaffa79

You can use it like so:
```lua
local some_module_state = {}
some_module_state.foo = "bar"

some_module_state = require("modulestate.luau").get_state("<unique name>", some_module_state)
-- from here on out, `some_module_state` will have the values from the global state
-- but luau-lsp still treats it like a local table, so typing `some_module_state.` will suggest `foo` with type string
```

When the fix is merged you should be able to just delete the `require("modulestate.luau").get_state` line and everything should work as intended



Patch:
diff --git a/src/lune/globals/require/context.rs b/src/lune/globals/require/context.rs
index 0c95d802..7f018c61 100644
--- a/src/lune/globals/require/context.rs
+++ b/src/lune/globals/require/context.rs
@@ -70,7 +70,7 @@ impl RequireContext {
             CWD.join(&rel_path)
         };
 
-        Ok((rel_path, abs_path))
+        Ok((abs_path, rel_path))
     }
 
     /**


Test patch:
diff --git a/src/tests.rs b/src/tests.rs
index f4af8874..fad6026b 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -95,6 +95,7 @@ create_tests! {
     require_nested: "require/tests/nested",
     require_parents: "require/tests/parents",
     require_siblings: "require/tests/siblings",
+    require_state: "require/tests/state",
 
     global_g_table: "globals/_G",
     global_version: "globals/_VERSION",
diff --git a/tests/require/tests/state.luau b/tests/require/tests/state.luau
new file mode 100644
index 00000000..d1b2b462
--- /dev/null
+++ b/tests/require/tests/state.luau
@@ -0,0 +1,14 @@
+-- the idea of this test is that state_module stores some state in one of its local
+-- variable
+local state_module = require("./state_module")
+
+-- we confirm that without anything happening, the initial value is what we expect
+assert(state_module.state == 10)
+
+-- this second file also requires state_module and calls a function that changes the local
+-- state to 11
+require("./state_second")
+
+-- with correct module caching, we should see the change done in state_secone reflected
+-- here
+assert(state_module.state == 11)
diff --git a/tests/require/tests/state_module.luau b/tests/require/tests/state_module.luau
new file mode 100644
index 00000000..9b6135e2
--- /dev/null
+++ b/tests/require/tests/state_module.luau
@@ -0,0 +1,9 @@
+local M = {}
+
+M.state = 10
+
+function M.set_state(n: number)
+    M.state = n
+end
+
+return M
diff --git a/tests/require/tests/state_second.luau b/tests/require/tests/state_second.luau
new file mode 100644
index 00000000..24ca3a45
--- /dev/null
+++ b/tests/require/tests/state_second.luau
@@ -0,0 +1,5 @@
+local state_module = require("./state_module")
+
+state_module.set_state(11)
+
+return {}