在构建完成后删除.a文件 – GoLang

huangapple go评论81阅读模式
英文:

Delete .a files after build - GoLang

问题

我目前正在使用fresh来在项目的任何源文件更改时重新构建和重启Web服务器。

它运行得还可以。然而,一段时间后,pkg目录下会创建文件。这导致刷新不再发生,原因是它获取的是.a文件而不是源文件(.go文件)来构建二进制文件。我该如何避免这个问题?

添加信息:

文件:runner.conf与main.go位于同一级目录下

root: .
tmp_path: ./tmp
build_name: bin/app.bin
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
build_delay: 600
colors: 1
log_color_main: cyan
log_color_build: yellow
log_color_runner: green
log_color_watcher: magenta
log_color_app:
ignore_dirs: ./pkg

完全不起作用:

13:39:48 watcher     | Watching .
13:39:48 watcher     | Watching bin
13:39:48 watcher     | Watching pkg
13:39:48 watcher     | Watching pkg/darwin_amd64
13:39:48 watcher     | Watching public
13:39:48 watcher     | Watching src
13:39:48 watcher     | Watching src/site.org
13:39:48 watcher     | Watching src/site.org/application
13:39:48 watcher     | Watching src/site.org/application/controllers
13:39:48 watcher     | Watching src/site.org/application/controllers/web
13:39:48 watcher     | Watching src/site.org/system

修复方法

最终,我无法使其正常工作。然而,我使用了这个插件创建了一个解决方案:

https://github.com/alexnj/SublimeOnSaveBuild

它尝试在保存时进行构建,我创建了这个构建系统(称为GoLang):

{
    "shell_cmd": "sh /Users/acruz/go_projects/build_go.sh \"$project_path\"",
    "working_dir": "${project_path}"
}

而build_go.sh文件内容如下:

#!/bin/sh
echo "Removing PKG folder if exists"
rm -Rf pkg

echo "Building application"
go build -o bin/app.bin main.go

echo "Killing application"
killall app.bin

echo "Running application ./bin/app.bin"
echo ""
echo "Debug information:"
echo ""

./bin/app.bin

一个@TODO是在项目配置中定义二进制文件的名称...但对我来说,这样做还可以。这样做的好处之一是您不必添加-a标志,这会使编译过程变慢,并且调试信息将仅显示在构建窗口中...

在构建完成后删除.a文件 – GoLang

英文:

I'm currently using fresh to rebuild and restart the webserver on every change of any of the source files in my project.

It works ok. However, after some time files are created at pkg directory. This causes the refresh does not happens anymore, and the reason is instead of getting the source files (.go files) to build the binary, it gets the .a files. How can I avoid this feature?

Adding information:

File: runner.conf at the same level of main.go

root: .
tmp_path: ./tmp
build_name: bin/app.bin
build_log: runner-build-errors.log
valid_ext: .go, .tpl, .tmpl, .html
build_delay: 600
colors: 1
log_color_main: cyan
log_color_build: yellow
log_color_runner: green
log_color_watcher: magenta
log_color_app:
ignore_dirs: ./pkg

Is not working at all:

13:39:48 watcher     | Watching .
13:39:48 watcher     | Watching bin
13:39:48 watcher     | Watching pkg
13:39:48 watcher     | Watching pkg/darwin_amd64
13:39:48 watcher     | Watching public
13:39:48 watcher     | Watching src
13:39:48 watcher     | Watching src/site.org
13:39:48 watcher     | Watching src/site.org/application
13:39:48 watcher     | Watching src/site.org/application/controllers
13:39:48 watcher     | Watching src/site.org/application/controllers/web
13:39:48 watcher     | Watching src/site.org/system

FIXING

In the end, I was not able to make it work. However, I create a solution using this plugin:

https://github.com/alexnj/SublimeOnSaveBuild

That tries to build on save, and I've created this build system (called GoLang):

{
    "shell_cmd": "sh /Users/acruz/go_projects/build_go.sh \"$project_path\"",
    "working_dir": "${project_path}"
}

And the file build_go.sh is:

#!/bin/sh
echo "Removing PKG folder if exists"
rm -Rf pkg

echo "Building application"
go build -o bin/app.bin main.go

echo "Killing application"
killall app.bin

echo "Running application ./bin/app.bin"
echo ""
echo "Debug information:"
echo ""

./bin/app.bin

One @TODO will be defining in the project configuration the name of the binary... but for me this works ok. One of the benefits of doing this is you don't have to add the -a flag, which will make slow the compilation process, and the debug information will be showed just there in the build window...

在构建完成后删除.a文件 – GoLang

答案1

得分: 1

考虑到Pull Request 20是关于“添加ignore_dirs设置”的,你可以使用这个新设置来显式地忽略pkg文件夹。

ignore_dirs path1, path2, ...

查看如何检查ignore_dirs路径

if info.IsDir() && !isTmpDir(path) {
    if _, ignore := ignorePaths[info.Name()]; (len(path) > 1 && 
       strings.HasPrefix(filepath.Base(path), ".")) || ignore {
        return filepath.SkipDir
英文:

Considering Pull Request 20 was about "Added ignore_dirs setting", you could use that new setting to explicitly ignore the pkg folder.

ignore_dirs path1, path2, ...

Check how that ignore_dirs path is checked:

if info.IsDir() && !isTmpDir(path) {
    if _, ignore := ignorePaths[info.Name()]; (len(path) > 1 && 
       strings.HasPrefix(filepath.Base(path), ".")) || ignore {
        return filepath.SkipDir

答案2

得分: 1

你可以将 $GOPATH/src/github.com/pilu/fresh/runner/build.go#L13 中的代码进行修改:

cmd := exec.Command("go", "build", "-o", buildPath(), root())

修改为:

cmd := exec.Command("go", "build", "-a", "-o", buildPath(), root())

然后执行以下命令:

go install -a github.com/pilu/fresh

-a 标志强制重新编译 .a 文件。

英文:

You can change $GOPATH/src/github.com/pilu/fresh/runner/build.go#L13

cmd := exec.Command("go", "build", "-o", buildPath(), root())

to be

cmd := exec.Command("go", "build", "-a", "-o", buildPath(), root())

and then

go install -a github.com/pilu/fresh

flag -a force recompiling of .a files

huangapple
  • 本文由 发表于 2014年12月30日 03:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/27694779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定