nesbox / TIC-80

TIC-80 is a fantasy computer for making, playing and sharing tiny games.
https://tic80.com
MIT License
4.98k stars 479 forks source link

tic80 doesn't find my required lua files #2426

Open atesin opened 9 months ago

atesin commented 9 months ago

EDIT: i found a workaround in https://github.com/nesbox/TIC-80/issues/2426#issuecomment-1879215018 , please dont nerf it

hi, i am in the programming developing/learning stage

to keep some order while in developing/testing i pretend to separate the program in various lua files, and call them with require('my-lib') in my cart code... previously i created a subfolder inside tic80 carts folder, to keep order i pretend later to create a separate folder for each project, with cart and all needed files inside

currently my folder structure looks like

imagen

in tic80 console i must type these commands

imagen

ahd the code looks like this (for later production, i can simply concat all lua files, import it to cart and delete these require lines)

imagen

but when i try to run the cart, it crashes and these messages appear in console

imagen

... i can clearly see the line no file '.\oop.lua', but i swear this file exists as .\ stands for "current directory", and current directory inside tic80 is <cart folder>\001-oop as i recently cd'ed and console prompt shows

i think it must be a bug, that a required file exists in current directory, and current directory being expanded to tic80 console's current directory, by default, as console prompt shows .... but i see currently is not the case :( ... for user intuitiveness and ease it SHOULD be the default behavior

( see this issue also, is somewhat related: #2414 , maybe all these complications with files won't be necessary if editor would be multi-tabbed :) )

could you please fix this bug / correct this oddity / add this feature, in the future releases? ... thank you

joshgoebel commented 9 months ago

require simple isn't supported. All your code has to be placed in the single main file.

Skeptim commented 9 months ago

require simple isn't supported. All your code has to be placed in the single main file.

I may misunderstand, but I think it does, see #2352

Skeptim commented 9 months ago

I never used it, not sure I can help but I try anyway:

Did you look at https://github.com/nesbox/TIC-80/wiki/Using-require-to-load-external-code-into-your-cart ? From what I understand the libraries should be in TIC-80/lua/module.lua file.

Also, I think current directory in TIC-80 is always TIC-80 and not where your cart is or where you cd #775

However, note that the use of require could be disabled in future update: #2352

atesin commented 9 months ago

require simple isn't supported. All your code has to be placed in the single main file.

not true, i tried with some of dirs shown in error message and they worked... the problem reported here is require doesn't work with files in tic80 command console current working directory specifically

it seems like tic80 embedded lua only sees the folder where tic80.exe executable is, and doesn't know/care about tic80 console virtual environment, so the required files are not found

Did you look at https://github.com/nesbox/TIC-80/wiki/Using-require-to-load-external-code-into-your-cart ? From what I understand the libraries should be in TIC-80/lua/module.lua file.

yes i tried what that wiki page says and doesn't work (maybe worked in a previous tic80 version)... and for regular lua files other than modules or libraries (e.g. grouplng similar functions, etc.) should work in tic80 command console "cwd" (current working directory, shown in prompt synbol), or the dir where current cart were loaded

Also, I think current directory in TIC-80 is always TIC-80 and not where your cart is or where you cd #775

i agree with @moonlit in that this behavior is confusional, prone to provoke errors and should be fixed

However, note that the use of require could be disabled in future update: #2352

require lua function could be very useful, i think it should be fixed in its security issues (the right way) instead being disabled (the fast and lazy way)... check my comment there for more details: https://github.com/nesbox/TIC-80/issues/2352#issuecomment-1871701386

atesin commented 9 months ago

i am using this dirty workaround (it bothers, should load files in same directory without the need of doing these tricks):

joshgoebel commented 9 months ago

As i said originally:

require simply isn't supported.

not true, i tried with some of dirs shown in error message and they worked..

There is a huge difference between "it works" and "it is supported". I'll repeat: This is not supported - it should never work, the existing behavior is a bug. Some people wish it was [supported] (there is a ticket for this), but it's currently not. And there is also a ticket to "break" it fully as it's a security risk - and not supported. If require simply never worked period, all the confusion would be gone.

atesin commented 9 months ago

@joshgoebel ok

borbware commented 9 months ago

the behaviour indeed can be a bit erratic: for some computers, i've had to add the following line to my main.lua for require to work:

package.path = package.path..";C:/path/to/your/directory/?.lua"

It adds your working directory to Lua's package.path so Lua can find your files.

atesin commented 9 months ago

@borbware : as i thought config.cart is loaded frist any cartridge, i tried to set this permanently by typing config command and adding this line in code editor:

package.path='%APPDATA%/com.nesbox.tic/TIC-80/?.lua;%APPDATA%/com.nesbox.tic/TIC-80/?/init.lua;'..package.path

THEME=
{
    CODE =
    {
...
...

but didn't worked :( ... is there any way to do this permanently? (i think it should come from stock)


@TimotheeGreg : i think the instructions in the page you posted are wrong ( https://github.com/nesbox/TIC-80/wiki/Using-require-to-load-external-code-into-your-cart#where-to-locate-our-module-files )

there it says "Open up tic80 and at the console type folder" and create a lua subfolder there, but after using require() with a non existing file, the traceback shows me all paths lua tried, but doesn't show lua subfolder inside tic80 carts folder, but instead it shows a lua subfolder inside tic80.exe executable folder... changing the start folder in tic80 shorccut didn't worked either

atesin commented 9 months ago

(edited after replies to add images)

i found a better workaround that works:

as a response to my previous response @TimotheeGreg , i put together these 2 facts:

when i edit tic80.exe shortcut "start folder" as shown above, and add require('req-test') to cart code editor, external edition works for lua files (carts_folder)/req-test.lua and (carts_folder)/req-test/init.lua =D

imagen

imagen

imagen

imagen

imagen

... you can trace full expanded package.path by requiring a nonexistant file as shown above, so after analyzing we realize we can use .\?.lua to do nice things like group cart includes in folders

or just use .\?\init.lua to add require("cart-folder") in tic80 code editor, and have cart-folder\\init.lua to manage all dependencies and codes externally

obviously this should be made only in development stage, once the project has done we must put it all together inside cart

ephetic commented 2 months ago

TIC-80 should really support some form of multi-file support. It need not be the native require function, but I think should be something that would allow people to easily refactor their require hacks to use the new solution for their previous sandbox-friendly needs.

I don't think this violates the retro spirit of TIC-80 more than the editor outline and bookmarks views do already and I'm not sure what use the console (e.g. mkdir) is without this sort of feature.