Notepad++ sets incorrect path when running script

1.2k views Asked by At

I have a simple script that I want to import into another with require, but when I run it from Notepad++ I get the usual error that require produces.

The funny thing is that it worked an hour ago and I did not restart the computer since then.

The files are in the same directory, so the simple file name (without .lua) worked and should still work. (relative path)

Lua runs the script just fine.

this is what I entered in Notepad: cmd /k lua "$(FULL_CURRENT_PATH)"

Earlier I also had a problem with Penlight, maybe there is some connection, so here it is: I tried to require"pl" but it failed to find the module. (ran from SciTE, worked prevously) I tried it in the Lua command line and it worked like a charm. Tried again in SciTE and voila it worked again.

I have no idea what causes any of them.

ps.: using the lfs module and os.execute("cd /d ...path...") did not work

1

There are 1 answers

2
Oliver On

Lua is searching for your required module in the folders of LUA_PATH. In the script you run via F5, put this statement:

print('current path is:')
os.execute('cd')
require 'someModuleThatDoesntExist'

After printing the "working" forlder (Program Files/Notepad++), it tries to find the required module and fails. The traceback shows that Lua looks through many different folders, none of them being the one containing FULL_CURRENT_PATH, so it can't find the module.

You have several choices:

  1. put your scripts in one of the listed paths
  2. set LUA_PATH in your environment to contain the folder name where your scripts are located
  3. change package.path from your script so it knows where to look for other modules. You could do this by either:
    • including an extra parameter to your F5, namely CURRENT_DIRECTORY, and make your script take its first command line param (CURRENT_DIRECTORY) to add it to package.path
    • parse arg[0] when your script starts, to get the folder containing script, and extend package.path

For example with #3, first option, you would use

cmd /k lua "$(FULL_CURRENT_PATH)" "$(CURRENT_DIRECTORY)"

in notepad++ and in your Lua module you would use

thisModuleDir = arg[1]
package.path = thisModuleDir .. ";" .. package.path
require 'yourmodule'