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

Goland会为终端携带GOROOT环境变量,导致使用Goland打开的终端切换版本后GOROOT不会改变 #9

Closed
jan-bar opened this issue Apr 2, 2024 · 16 comments · Fixed by version-fox/vfox#256

Comments

@jan-bar
Copy link

jan-bar commented Apr 2, 2024

如图,使用Goland时需要选定一个版本

image

如下所示,通过Goland打开的终端会自动携带Goland的GOROOT环境变量,无论vfox如何切换这个环境变量不会变

image

我通过修改下面脚本,把GOROOT环境变量 unset 取消掉,最终达到我的需求。

function PLUGIN:EnvKeys(ctx)
    local mainPath = ctx.path
    return {
        {
            key = "PATH",
            value = mainPath .. "/bin"
        },
        {
            key = "GOROOT",
            value = ""
        }
    }
end

如下所示,现在通过Goland打开的终端切换版本时 GOROOT 环境变量也会根据 go.exe 所在目录自动切换。

image

我想问下,我这个方案是否可靠,有没有考虑不到的情况。如果没问题,可否考虑更新脚本额。

@jan-bar
Copy link
Author

jan-bar commented Apr 2, 2024

这是goland配置cmder终端。 可以参考 version-fox/vfox#129 配置window终端。其他平台我没环境测额。

image

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

两个问题:

  1. 这个插件没有正确配置GOROOT.
  2. version-fox/vfox@22c4e5c 这次变更导致的,导致无法及时更新env. 虽然过滤了PATH, 但是把非PATH也过滤了.

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

@aooohan 我刚试了下,去掉问题代码,测试时依旧没有设置GOROOT环境变量,问题依旧存在额。我暂时想不出好的解决方案, 我上面那个修改env_keys.lua感觉不一定是用户需要的,如果做到用户可选择是否会好点。

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

不设置GOROOT也能用, 我当前环境macos + zsh + idea正常的.

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

切换后,执行go env GOROOT会跟着版本切换变化么?我这边 windows + clink +idea 不行额,虽然执行go version版本号确实变了,where go 也指向切换后的路径,但是这个GOROOT环境变量始终是goland设置的那个路径额。不确定在go build时会不会存在某些问题额。

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

image

我用powershell测试也是相同问题,切换版本过后可执行程序已经切换,但是这个环境变量没有改变额。

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

image

注意看, 这个envKeys需要修改, 修改之后GOROOT就会变了. 如果不配GOROOT, 你切换之后GOROOT不会变, 但是go version是变的.

你powershell这没配置对hook, 跟这个没关系.

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

https://github.com/version-fox/vfox-golang/blob/main/hooks/env_keys.lua 这个脚本没加GOROOT额,我用vfox update golang下载的这个文件也是没有GOROOT的。

不过我测试了

function PLUGIN:EnvKeys(ctx)
    local mainPath = ctx.path
    return {
        {
            key = "GOROOT",
            value = mainPath
        },
        {
            key = "PATH",
            value = mainPath .. "/bin"
        }
    }
end

function PLUGIN:EnvKeys(ctx)
    local mainPath = ctx.path
    return {
        {
            key = "GOROOT",
            value = ""
        },
        {
            key = "PATH",
            value = mainPath .. "/bin"
        }
    }
end

这两个结果都是一致的。当环境变量不存在GOROOT时go.exe工具会自动设置GOROOT

我恢复了之前我的改动,并用上面两种方式的lua脚本,测试结果都没问题额。

image

我现在感觉有点懵啊,用你给截图所示的env_keys.lua0.3.0的版本用powershell测试,是没有问题的额。

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

https://github.com/version-fox/vfox-golang/blob/main/hooks/env_keys.lua 这个脚本没加GOROOT额,我用vfox update golang下载的这个文件也是没有GOROOT的。

... 就是没有啊, 我截图是我手改的.... 这个ISSUE还没关呢, 加上GOROOT我不就关了吗.....

不用纠结了. vfox呢边没问题了, 现在就就是插件这里还没加GOROOT. 😂

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

@aooohan 我现在纠结的是,可以不用撤回我那个改动额。因为我测试了用改后的env_keys.lua,和0.3.0的程序是没问题的。

因为我确实不希望每次敲命令都设置一遍环境变量额。

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

@aooohan 我现在纠结的是,可以不用撤回我那个改动额。因为我测试了用改后的env_keys.lua,和0.3.0的程序是没问题的。

因为我确实不希望每次敲命令都设置一遍环境变量额。

有问题的, 你可以先全局设置一个golang版本, 然后你在通过goland打开shell, 你会发现切换不了.....

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

有问题的, 你可以先全局设置一个golang版本, 然后你在通过goland打开shell, 你会发现切换不了.....

可能我复现的方式不对吧,我在clink_vfox.lua添加打印,打开goland终端前已经设置好全局版本,然后在goland打开的终端执行全局切换和会话切换,结果都可以成功。下面图演示的是 https://github.com/jan-bar/vfox/tree/feat_add_clink 这里的代码,和添加GOROOT设置的env_keys.lua插件。

GIF 2024-4-3, 星期三 13-45-27

@aooohan
Copy link
Member

aooohan commented Apr 3, 2024

我用windows也试一下吧, 正好试试你的这个clink脚本

@jan-bar
Copy link
Author

jan-bar commented Apr 3, 2024

好像破案了,我之前也遇到过发现不管怎么切换都不会改变,最终发现是项目当前目录存在.tool-versions文件的原因。当时我手动删除就没管了,所以上面那个动图才没问题。如下所示,只要.tool-versions文件存在,切换就不会成功。删除该文件就可以成功。

image

这个文件是Project作用域,但是里面的go版本我没有安装。我发现当我安装这个版本后再次用goland打开终端时,go的版本会锁死为.tool-versions文件的版本。

现在有2个问题:

  1. 当前目录存在.tool-versions文件,但里面的版本没安装时,要定义一种行为吧,是自动安装并切换还是切换到全局作用域。
  2. 我那个关于环境变量没有变动则不设置的改动是不是错误回滚了额,如果是的话,我希望加上额。

@aooohan
Copy link
Member

aooohan commented Apr 7, 2024

  1. 当前目录存在.tool-versions文件,但里面的版本没安装时,要定义一种行为吧,是自动安装并切换还是切换到全局作用域。

这个确实需要处理.

  1. 我那个关于环境变量没有变动则不设置的改动是不是错误回滚了额,如果是的话,我希望加上额。

没回滚错, 就算不存在.tool-versions. 我这个环境仍然不生效. 如果加上你呢段代码的话, 注掉就没事了.

@jan-bar
Copy link
Author

jan-bar commented Apr 7, 2024

没回滚错, 就算不存在.tool-versions. 我这个环境仍然不生效. 如果加上你呢段代码的话, 注掉就没事了.

我是windows + goland + clink测试了这个问题,那个注释加不加在删除.tool-versions文件后都正常,存在.tool-versions时都不正常额,不清楚是不是不同平台终端的差异导致额。

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

Successfully merging a pull request may close this issue.

2 participants