xmake-io / xmake

🔥 A cross-platform build utility based on Lua
https://xmake.io
Apache License 2.0
10.05k stars 787 forks source link

xmake lua 相对路径的脚本,执行报错 #4058

Closed Wzshun closed 1 year ago

Wzshun commented 1 year ago

Xmake 版本

2.8.1

操作系统版本和架构

windows10

描述问题

命令行xmake lua 指定的脚本(相对路径),执行报错。

# 目录结构
|- root
|- xmakeExt
|-- foo.lua
|- projectA
|-- xmake.lua

E:\root> cat ./xmakeExt/foo.lua
function main() 
    print("hello xmake!")
end

E:\root> xmake lua ./xmakeExt/foo.lua
error: cannot import module: ./xmakeExt/foo.lua, not found!

# 绝对路径执行是没问题的
E:\root> xmake lua E:\root/xmakeExt/foo.lua
hello xmake!

此外,我如果想在xmake.lua,import相对路径的脚本如何编写? 比如这里的xmake.lua 在 projectA目录下

import(../xmakeExt/foo) -- 这行在编译时会报错。

期待的结果

支持相对路径的lua脚本

工程配置

附加信息和错误日志

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


Title: xmake lua relative path script, execution error

waruqi commented 1 year ago

error: cannot import module: ./xmakeExt/myVersions.lua, not found!

贴全啊,myVersions.lua 在哪里配置的,你都没贴。。xmake 总不可能凭空蹦出这个文件吧。。

Wzshun commented 1 year ago

复制粘贴错了。但我寻思这也不是很难理解的事鸭。。。已在题目内编辑了

waruqi commented 1 year ago

我这里可以,没法复现

>xmake l ./testa/foo.lua
hello

import(../xmakeExt/foo) -- 这行在编译时会报错。

仔细看文档,里面说的很详细了,用法不对 https://xmake.io/#/zh-cn/manual/builtin_modules?id=import

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


Copy paste wrong. But I think this is not a difficult thing to understand. . .

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


I can do it here, but I can't reproduce it

>xmake l ./testa/foo.lua
hello

import(../xmakeExt/foo) -- This line will throw an error when compiling.

Look at the document carefully, it is very detailed, the usage is wrong https://xmake.io/#/zh-cn/manual/builtin_modules?id=import

Wzshun commented 1 year ago

image 我也不清楚怎么排除这个问题。。-v,-D,都没有额外信息打印。

waruqi commented 1 year ago
xmake lua os.isfile ./myVersions.lua

看下

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


xmake lua os.isfile ./myVersions.lua

look at

waruqi commented 1 year ago

或者自己调下代码。。https://github.com/xmake-io/xmake/blob/fdead41fed6b6d0d1d8c699f810bf8ffbf995a8b/xmake/plugins/lua/main.lua#L79

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


Or adjust the code yourself. . https://github.com/xmake-io/xmake/blob/fdead41fed6b6d0d1d8c699f810bf8ffbf995a8b/xmake/plugins/lua/main.lua#L79

Wzshun commented 1 year ago

xmake lua os.isfile ./myVersions.lua false

Wzshun commented 1 year ago

或者自己调下代码。。

https://github.com/xmake-io/xmake/blob/fdead41fed6b6d0d1d8c699f810bf8ffbf995a8b/xmake/plugins/lua/main.lua#L79

这个应该怎么调试?lua的教程我只大概看了下菜鸟教程的基本描述。然后xmake给lua做的扩展还不太理解。

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


Or adjust the code yourself. .

https://github.com/xmake-io/xmake/blob/fdead41fed6b6d0d1d8c699f810bf8ffbf995a8b/xmake/plugins/lua/main.lua#L79

How should this be debugged? For the lua tutorial, I only read the basic description of the rookie tutorial. Then the extensions made by xmake to lua are not well understood.

Wzshun commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.

I can do it here, but I can't reproduce it

>xmake l ./testa/foo.lua
hello

import(../xmakeExt/foo) -- This line will throw an error when compiling.

Look at the document carefully, it is very detailed, the usage is wrong https://xmake.io/#/zh-cn/manual/builtin_modules?id=import

这个import语法,我看了下,然后将xmake.lua改写成以下:

import("git.versions", {rootdir = "$(projectdir)/xmakeExt"})

目录 xmakeExt/git/versions.lua,是有文件的。 然后点击build,报错

error: .\src\ui\xmake.lua:2: global 'import' is not callable (a nil value)
stack traceback:
    [.\src\ui\xmake.lua:2]: in main chunk
waruqi commented 1 year ago

仔细看文档,区分下 描述域,脚本域:https://xmake.io/#/zh-cn/guide/syntax_description

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


Look at the document carefully and distinguish the description domain and the script domain: https://xmake.io/#/zh-cn/guide/syntax_description

Wzshun commented 1 year ago

仔细看文档,区分下 描述域,脚本域:https://xmake.io/#/zh-cn/guide/syntax_description

ok,明白了。

那个not found的,怎么排查呢?

waruqi commented 1 year ago

xmake lua os.isfile ./myVersions.lua false

仔细看文档,区分下 描述域,脚本域:https://xmake.io/#/zh-cn/guide/syntax_description

ok,明白了。

那个not found的,怎么排查呢?

不知道,访问不到这个文件,但我这无法复现,你只能自己调下 xmake 源码了。

可以看下自己的磁盘是否存在什么加密,权限控制之类的。。也可以换个磁盘 或者 系统,或者换台机器试试

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


xmake lua os.isfile ./myVersions.lua false

Look at the document carefully and distinguish between the description domain and the script domain: https://xmake.io/#/zh-cn/guide/syntax_description

ok, got it.

How to troubleshoot the one that was not found?

I don’t know, I can’t access this file, but I can’t reproduce it, you can only adjust the xmake source code yourself.

Wzshun commented 1 year ago

xmake lua os.isfile ./myVersions.lua false

仔细看文档,区分下 描述域,脚本域:https://xmake.io/#/zh-cn/guide/syntax_description

ok,明白了。 那个not found的,怎么排查呢?

不知道,访问不到这个文件,但我这无法复现,你只能自己调下 xmake 源码了。

可以看下自己的磁盘是否存在什么加密,权限控制之类的。。也可以换个磁盘 或者 系统,或者换台机器试试

os.isfile 这个函数定义,没在core/base/os.lua中看到,是lua原生的os库函数吗?看了lua文档好像也没这个函数。

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


xmake lua os.isfile ./myVersions.lua false

Look at the document carefully and distinguish the description domain and script domain: https://xmake.io/#/zh-cn/guide/syntax_description

ok, got it. How to troubleshoot the one that was not found?

I don’t know, I can’t access this file, but I can’t reproduce it, you can only adjust the xmake source code yourself.

You can see if there is any encryption, permission control, etc. on your disk. . You can also change the disk or system, or try another machine

The function definition of os.isfile is not seen in core/base/os.lua. Is it the native os library function of lua? After reading the lua documentation, it seems that there is no such function.

waruqi commented 1 year ago

https://github.com/xmake-io/xmake/blob/master/core/src/xmake/os/isfile.c https://github.com/tboox/tbox/blob/2e0cac8bae7a8bc921114b1e482fefd8f8a5d55e/src/tbox/platform/windows/file.c#L387

Wzshun commented 1 year ago

https://github.com/xmake-io/xmake/blob/master/core/src/xmake/os/isfile.c https://github.com/tboox/tbox/blob/2e0cac8bae7a8bc921114b1e482fefd8f8a5d55e/src/tbox/platform/windows/file.c#L387

找到问题了。 看了下tbox工程里的path.c中tb_path_absolute_to函数,其中在处理相对路径时,用的current directory。这个路径以前在项目中也遇到过,不可靠。

在powershell中修改当前工作路径,即可以正确执行了。 image

但这个问题应该怎么处理呢?

waruqi commented 1 year ago

这是你的环境问题,跟 xmake 无关,自己想办法解决。。这边总归要用到 GetCurrentDirectory 的。。到处都得要。。除非你能找到一个另外一个 windows 的替代 api。

或者提 issues 到微软那问问。

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


This is your environmental problem, which has nothing to do with xmake, and you can find a way to solve it yourself. . After all, GetCurrentDirectory is used here. . Everywhere has to be asked for. . Unless you can find an alternative api for another windows.

Wzshun commented 1 year ago

这是你的环境问题,跟 xmake 无关,自己想办法解决。。这边总归要用到 GetCurrentDirectory 的。。到处都得要。。除非你能找到一个另外一个 windows 的替代 api。

或者提 issues 到微软那问问。

这个和windows的api没关,主要是这个api是一个静态函数,然后到处可设;其次根据程序启动的位置不同,他都会不同。

我是觉得传递给xmake的相对路径参数,都应该以xmake.exe启动起来的当前路径,为这些参数的根路径。 这个逻辑才对。

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


This is your environment problem, it has nothing to do with xmake, you have to find a way to solve it yourself. . After all, GetCurrentDirectory is used here. . Everywhere has to be asked for. . Unless you can find an alternative api for another windows.

Or raise issues to Microsoft to ask.

This has nothing to do with the windows api, mainly because this api is a static function and can be set anywhere; secondly, it will be different depending on the location where the program is started.

I think the relative path parameters passed to xmake should use the current path where xmake.exe is started as the root path of these parameters. This logic is right.

waruqi commented 1 year ago

xmake 从 GetCurrentDirectory 取的相对路径,它返回的不对,xmake 怎么搞?谁传递给它?不从 GetCurrentDirectory 中取,xmake 怎么知道当前被启动的路径在哪?

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


The relative path taken by xmake from GetCurrentDirectory, it returns wrong, what to do with xmake? Who passes it on?

Wzshun commented 1 year ago

我的意思是说

> xmake lua relativePathArg

这个relativePathArg转变成绝对路径,他的根路径应该xmake.exe这个进程所在路径。

查了圈 C# .net framework 以前用的System.AppDomain.CurrentDomain.BaseDirectory .net 7 的话,用AppContext.BaseDirectory C# 当前路径,这个链接包含各种当前路径的api

windows api的话,看了stackoverflow c++ 当前路径,用的GetModuleFileName

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


i mean

xmake lua relativePathArg

This relativePathArg is transformed into an absolute path, and its root path should be the path where the xmake.exe process is located.

check the circle System.AppDomain.CurrentDomain.BaseDirectory previously used by C# .net framework For .net 7, use AppContext.BaseDirectory C# current path, this link contains apis of various current paths

For windows api, read stackoverflow c++ current path, use GetModuleFileName

waruqi commented 1 year ago

我的意思是说

> xmake lua relativePathArg

这个relativePathArg转变成绝对路径,他的根路径应该xmake.exe这个进程所在路径。

查了圈 C# .net framework 以前用的System.AppDomain.CurrentDomain.BaseDirectory .net 7 的话,用AppContext.BaseDirectory C# 当前路径,这个链接包含各种当前路径的api

windows api的话,看了stackoverflow c++ 当前路径,用的GetModuleFileName

GetModuleFileName 我原本就用了,但这个取的是 xmake.exe 所在的安装目录 ,也就是 C:\Program files\xmake,也不是你实际 shell 下的当前运行目录。。

https://github.com/xmake-io/xmake/blob/fdead41fed6b6d0d1d8c699f810bf8ffbf995a8b/core/src/xmake/engine.c#L637

取这个值,你也无法取到 relativePathArg 对应的绝对路径。。

你刚那个 link 里也有说

If you fetch the current directory when your program first starts, then you effectively have the directory your program was started from. Store the value in a variable and refer to it later in your program. This is distinct from the directory that holds the current executable program file. It isn't necessarily the same directory; if someone runs the program from a command prompt, then the program is being run from the command prompt's current working directory even though the program file lives elsewhere

既然,你 shell 都 cd 切到对应目录了,这个时候运行 xmake,xmake 取当前路径,就应该用 GetCurrentDirectory

Wzshun commented 1 year ago

GetModuleFileName 这api我的锅,没怎么用过windows api,都是用框架。那这个api就不合适这个场景了。 还是需要获取 xmake.exe 启动时所在进程的路径。(类似C#的AppDomain那些api,windows的api还没找到)

然后我的用法出问题,我是这么操作的 在文件浏览器中打开我的lua所在目录,空白位置,shift + 右键,打开powershell,随后进行操作。 这里我没做任何cd行为。GetCurrentDirectory他就返回System32了。

还有我在vscode中的终端操作,也是有类似的问题。在这个终端中,我操作过cd libs/src,又操作过cd ../..,类似的操作,他还是一样报错的。

总之GetCurrentDirectory,不太靠谱的这个api。。很多文章也说不要依赖这个路径,在程序的任何位置都可以修改他,并且启动位置不同,他也不同。想开机启动、菜单快捷启动等等,他都返回不同的路径。

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


GetModuleFileName This api is my fault. I haven't used the windows api much, and I use the framework. Then this API is not suitable for this scene. Still need to get the path of the process where xmake.exe starts. (Similar to C#'s AppDomain APIs, windows APIs haven't been found yet)

Then there was a problem with my usage, I did it like this Open the directory where my lua is located in the file browser, use shift + right key to open the powershell, and then operate. I didn't do any cd behavior here. GetCurrentDirectory returns System32.

And my terminal operation in vscode also has similar problems. In this terminal, I have operated cd libs/src, and also operated cd ../.., similar operations, he still reported the same error.

In short, GetCurrentDirectory is an unreliable API. . Many articles also say not to rely on this path, it can be modified anywhere in the program, and it is different from different startup locations. If you want to boot up, menu quick start, etc., he returns to a different path.

waruqi commented 1 year ago

但目前只能用 GetCurrentDirectory,总归要获取当前路径的,各种地方都需要,避免不了,也没有替代API。

当前路径当然可能会各种改,这是正常的,xmake 内部还会去修改

在文件浏览器中打开我的lua所在目录,空白位置,shift + 右键,打开powershell,随后进行操作。 这里我没做任何cd行为。GetCurrentDirectory他就返回System32了。

想开机启动、菜单快捷启动等等,他都返回不同的路径

这是正常的,但是如果打开的是终端,终端应该根据获取的 current directory ,显示 > 的时候,跟它保存同步。。

这是 powershell 的问题,如果它当前是 system32,那么打开就应该默认显示在 system32 目录下,只能说明它显示没跟 current dir 保持同步,这跟 xmake 也没关系,你可以到 powershell 那反馈 issues

waruqi commented 1 year ago

所以现在这个完全跟 xmake 无关,这边没法解决

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


But at present, only GetCurrentDirectory can be used. After all, to get the current path, it is needed in various places. It cannot be avoided, and there is no alternative API.

Of course, the current path may be changed in various ways. This is normal, and xmake will also modify it internally.

Open the directory where my lua is located in the file browser, press shift + right click in the blank location, open powershell, and then operate. I didn't do any cd behavior here. GetCurrentDirectory returns System32.

If you want to start at boot, menu quick start, etc., he will return to a different path

This is normal, but if you open a terminal, the terminal should save and synchronize with it when > is displayed according to the obtained current directory. .

This is a problem with powershell. If it is currently system32, it should be displayed in the system32 directory by default when it is opened. It can only mean that it is not synchronized with the current dir. This has nothing to do with xmake. You can report issues to powershell

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


So now this has nothing to do with xmake at all, there is no way to solve it here