intel/executor: Add script directory to package.path

In Lua, modules (i.e. files with lua code) are loaded by using
the standard library require(), e.g.

```
local mylib = require("mylib")

mylib.do_something()
```

The require() will decide where to look by peeking at `package.path`
table.  By default it doesn't include the scripts directory, so running
executor from the script directory vs. from the root of the repo would
yield different results (require works vs. require fail to find the
module).  This patch includes the script directory to avoid this issue.

Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/37805>
This commit is contained in:
Caio Oliveira 2025-09-30 13:01:40 -07:00 committed by Marge Bot
parent 86947062e9
commit 62f07dc5e3

View file

@ -6,6 +6,7 @@
#include <ctype.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
@ -935,6 +936,8 @@ main(int argc, char *argv[])
return 1;
}
void *mem_ctx = ralloc_context(NULL);
const char *filename = argv[optind];
process_intel_debug_variable();
@ -969,6 +972,26 @@ main(int argc, char *argv[])
}
lua_setglobal(L, "arg");
/* Add the script's directory to package.path so scripts can require()
* files from the same directory.
*/
{
char *script = ralloc_strdup(mem_ctx, filename);
const char *script_dir = dirname(script);
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
const char *original_path = lua_tostring(L, -1);
const char *new_path =
ralloc_asprintf(mem_ctx, "%s/?.lua;%s", script_dir, original_path);
lua_pop(L, 1);
lua_pushstring(L, new_path);
lua_setfield(L, -2, "path");
lua_pop(L, 1);
}
lua_newtable(L);
{
lua_pushinteger(L, E.devinfo.ver);
@ -996,6 +1019,8 @@ main(int argc, char *argv[])
lua_close(L);
close(E.fd);
ralloc_free(mem_ctx);
return 0;
}