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

tic80 doesn't find my required lua files #2426

Open
atesin opened this issue Dec 28, 2023 · 12 comments
Open

tic80 doesn't find my required lua files #2426

atesin opened this issue Dec 28, 2023 · 12 comments

Comments

@atesin
Copy link

atesin commented Dec 28, 2023

EDIT: i found a workaround in #2426 (comment) , 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
Copy link
Collaborator

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

@Skeptim
Copy link
Contributor

Skeptim commented Dec 28, 2023

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
Copy link
Contributor

Skeptim commented Dec 28, 2023

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
Copy link
Author

atesin commented Dec 29, 2023

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: #2352 (comment)

@atesin
Copy link
Author

atesin commented Dec 29, 2023

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

  • create the subdirectory <tic80-install>/project-name
  • place all lua files here, ex.: project-main.lua, functions.lua
    • project-main.lua is the file that has header comments, TIC() function, and others
  • create the cartridge file <tic80-carts>/project-cart.tic with just this content:
require('project-name.functions') -- will map to "<tic80-install>/project-name/functions.lua"
require('project-name.project-main') -- main file at LAST because include also execute files
  • edit files in <tic80-install>/project-name with zerobrane or another external editor
  • when pass to production, concatenate all lua files and run in tic console:; import code joined-files.lua, and save

@joshgoebel
Copy link
Collaborator

joshgoebel commented Dec 31, 2023

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
Copy link
Author

atesin commented Dec 31, 2023

@joshgoebel ok

@borbware
Copy link
Contributor

borbware commented Jan 5, 2024

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
Copy link
Author

atesin commented Jan 5, 2024

@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
Copy link
Author

atesin commented Jan 5, 2024

(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:

  • edit "start folder" in tic80 shortcut, to %APPDATA%/com.nesbox.tic/TIC-80
  • traceback show package.path with .\?.lua and .\?\init.lua (universally ".\" stands for "current folder")

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

  • cart-libs\greet -> %START_FOLDER%\cart-libs\greet.lua
  • cart-libs\farewell -> %START_FOLDER%\cart-libs\farewell.lua

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
Copy link

ephetic commented Jul 16, 2024

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.

  • require -like syntax
  • edit <otherfile> support, or similar
  • automatically bundle/concat files on export

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.

@scambier
Copy link

scambier commented Oct 7, 2024

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.

You can already use multiple files: https://github.com/nesbox/TIC-80/wiki/tools#bundling

I hear you though, it would be nice if TIC-80 supported that directly, but:

  • There are many languages supported by TIC
  • They all manage modules differently
  • They already all require external tools for bundling

That would IMO greatly bloat TIC-80, for a problem that is already mostly solved by one of the tools listed in the wiki.

Granted, they only work with the PRO version (to my knowledge), but I think it's fine. The fact that require() works with non-pro is a quirk that only works with Lua.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants