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

proposal: runtime: add GetEntryInfo function #68583

Open
timandy opened this issue Jul 25, 2024 · 10 comments
Open

proposal: runtime: add GetEntryInfo function #68583

timandy opened this issue Jul 25, 2024 · 10 comments
Labels
Milestone

Comments

@timandy
Copy link

timandy commented Jul 25, 2024

Proposal Details

Some time we want to get the entry info like module name. entry file path, dpendent modules and so on.
before go1.23 these infos can be get from runtime.firstmoduledata, but after go1.23 runtime.firstmoduledata can not be access.

before go1.23

// get path of the entry file, may be a /xx/xx_test.go or /xx/main.go
func getEntryFilePath() string {
	mainFile := getMainFile()
	if !isTestMode(mainFile) {
		return mainFile
	}
	firstModule := unsafe.Pointer(&firstmoduledata)
	fileTab := *(*[]byte)(reflection.Add(firstModule, filetabField.Offset))
	lastFiles := getLastFiles(fileTab)
	for _, file := range lastFiles {
		if isTestFile(file) {
			return file
		}
	}
	panic("can not found entry file path")
}
@gopherbot gopherbot added this to the Proposal milestone Jul 25, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@tdakkota
Copy link

I feel like debug.ReadBuildInfo and runtime.Caller already solved that problem.

@timandy
Copy link
Author

timandy commented Jul 25, 2024

@tdakkota debug.ReadBuildInfo() can not get entry path, and it return nothing in test mode.

image

if invoke runtime.Caller() in init() also can not get the stack frame of main or test_main

@seankhliao
Copy link
Member

this describes the desired change, but not why this information should be exposed, what it would be used for, and how common a need for this is

@timandy
Copy link
Author

timandy commented Jul 25, 2024

@seankhliao One of the methods in our base library can read the specified configuration file in the root directory of the module where the entry file is located. The profile is not embedded into the binary. And test mod should be supoorted, so we need to know where the entry file is located at.

@jaloren
Copy link

jaloren commented Jul 25, 2024

@timandy you’ve described what you are doing but you haven’t explained why. could you explain what problem you are trying to solve?

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Jul 25, 2024
@ianlancetaylor
Copy link
Member

I don't understand how getting the path of the source file that contains main.main will tell you the root directory of the module. Many modules store commands, and main.main symbols, in subdirectories under cmd.

@timandy
Copy link
Author

timandy commented Jul 26, 2024

I don't understand how getting the path of the source file that contains main.main will tell you the root directory of the module. Many modules store commands, and main.main symbols, in subdirectories under cmd.

@ianlancetaylor Although many packages may contain the the _test.go file, I just need the first loaded at runtime;
That's why before go1.23, I needed to read firstmoduledata to get the list of loaded files and find the path to the first _test.go file; Then traverse the folder upwards until the go.mod file is found; Because our project definitely includes go.mod, there is no compatibility with older project sturcture;

@seankhliao
Copy link
Member

go test executes the tests from the working directory containing the test, you could just use os.Getwd to get the directory of the package. If you built the test to execute elsewhere with go test -c, then i don't see how knowing the source code path helps you find the data you need.

@timandy
Copy link
Author

timandy commented Jul 30, 2024

@seankhliao As you said that go test -c is used for us during test time. We use cross compilation and then run unit tests by qemu-user-static.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

7 participants