Skip to content

Use correct path to snap binary #17274

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,14 @@ func IsProd() bool {
}

func getAppPath() (string, error) {
var appPath string
appPath := AppPath
if giteaAppPath, ok := os.LookupEnv("GITEA_APP_PATH"); ok {
appPath = giteaAppPath
}
if appPath != "" {
return appPath, nil
}

var err error
if IsWindows && filepath.IsAbs(os.Args[0]) {
appPath = filepath.Clean(os.Args[0])
Expand All @@ -446,9 +453,24 @@ func getAppPath() (string, error) {
if err != nil {
return "", err
}

// Note: we don't use path.Dir here because it does not handle case
// which path starts with two "/" in Windows: "//psf/Home/..."
return strings.ReplaceAll(appPath, "\\", "/"), err
appPath = strings.ReplaceAll(appPath, "\\", "/")

// Fix issue 16209: When running in a snap and repositories are
// created, paths in the hooks include the snap revision. But as new
// revisions come out the older revisions are eventually deleted, and
// the hooks begin to fail.
// The code below replaces the snap revision with "current", which
// is a link to the current snap revision.
snapPath := os.Getenv("SNAP")
if snapPath != "" && strings.HasPrefix(appPath, snapPath) {
revision := os.Getenv("SNAP_REVISION")
appPath = strings.Replace(appPath, revision, "current", 1)
Copy link
Contributor

@wxiaoguang wxiaoguang Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be more careful about the string replacement. Snap document says that the SNAP_REVISION can be something like "x1" (https://snapcraft.io/docs/environment-variables) , a very short string. Maybe there will be other revision strings in future.

So maybe we should just split the appPath and check if the second last field is SNAP_REVISION and replace it, and check the gitea binary existence again. And print a log here to tell users we changed the app path.

Copy link

@OleHusgaard OleHusgaard Oct 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Then the code would be like what I proposed in PR 17157, but with an added check that the second last field is SNAP_REVISION. That would also include a check with exec.LookPath() that the modified path will actually work.

}

return appPath, nil
}

func getWorkPath(appPath string) string {
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ parts:
source: .
stage-packages: [ git, sqlite3, openssh-client ]
build-packages: [ git, libpam0g-dev, libsqlite3-dev]
build-snaps: [ go, node/14/stable ]
build-snaps: [ go, node/16/stable ]
build-environment:
- LDFLAGS: ""
override-pull: |
Expand Down