Skip to content

runtime,os: add os.Executable() for Darwin #4878

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

Merged
merged 2 commits into from
May 1, 2025

Conversation

dgryski
Copy link
Member

@dgryski dgryski commented Apr 30, 2025

No description provided.

Copy link
Contributor

@eliasnaur eliasnaur left a comment

Choose a reason for hiding this comment

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

LGTM, other than a single question below.

Comment on lines +250 to +254
argString := _string{
length: length,
ptr: (*byte)(cstr),
}
executablePath := *(*string)(unsafe.Pointer(&argString))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not executablePath := unsafe.String((*byte)(cstr), length)?

Copy link
Member

Choose a reason for hiding this comment

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

We still support Go 1.19, and unsafe.String was introduced in Go 1.20.

Copy link
Member Author

Choose a reason for hiding this comment

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

We don't (yet) use unsafe.String() in the runtime package, although there is a comment to that effect:

// TODO: use unsafe.String instead once we require Go 1.20.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, thanks. I did in fact look up when unsafe.String was introduced but then went ahead and assumed 1.20 was more than safe. Out of curiosity, is there any reason for keeping support for such a old version of Go?

Copy link
Member

Choose a reason for hiding this comment

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

We could indeed drop support for this older version by now. One consideration might be that Debian bookworm (current stable) uses Go 1.19, dropping support for that means people will need to upgrade the Go version on Debian.

Copy link
Member

@aykevl aykevl left a comment

Choose a reason for hiding this comment

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

LGTM assuming you've tested this

@dgryski
Copy link
Member Author

dgryski commented Apr 30, 2025

Hmm..

~/go/src/github.com/dgryski/bug/exe $ cat main.go
package main

import "os"

func main() {
	p, err := os.Executable()
	if err != nil {
		println("error: ", err)
	} else {
		println("exe: ", p)
	}
}
~/go/src/github.com/dgryski/bug/exe $ tinygo run main.go
exe:  /var/folders/5b/_hr1d1fd3qn4t9vtfx5p61wm0000gp/T/tinygo2514738286/main
~/go/src/github.com/dgryski/bug/exe $ tinygo build
~/go/src/github.com/dgryski/bug/exe $ ./exe
exe:  ./exe
~/go/src/github.com/dgryski/bug/exe $ go run main.go
exe:  /var/folders/5b/_hr1d1fd3qn4t9vtfx5p61wm0000gp/T/go-build1161502719/b001/exe/main
~/go/src/github.com/dgryski/bug/exe $ go build
~/go/src/github.com/dgryski/bug/exe $ ./exe
exe:  /Users/dgryski/go/src/github.com/dgryski/bug/exe/exe

Big Go gives the full path but we only get the relative path for a full binary. I should probably fix that.

@deadprogram
Copy link
Member

Thanks for the addition @dgryski and to @eliasnaur and @aykevl for reviewing. Now merging.

@deadprogram deadprogram merged commit 4574340 into tinygo-org:dev May 1, 2025
19 checks passed
@aykevl
Copy link
Member

aykevl commented May 1, 2025

@dgryski I suspect the result is wrong if you call os.Chdir and then call os.Executable().

@dgryski
Copy link
Member Author

dgryski commented May 1, 2025

Oops, oh right. I guess we need to do the entire resolution at process startup instead. I'll rework this (although it means adding in a hook to main() to call any platform-specific argc/argv processing).

@dgryski
Copy link
Member Author

dgryski commented May 6, 2025

Fix for os.Chdir issue: #4881

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 this pull request may close these issues.

4 participants