-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
runtime: runtime.Caller returns always '/' separator in file paths on Windows #3335
Comments
Filenames returned by runtime.Caller do have / as path delimiter. They are extacted from symtab of compiled packages. They are written by compiler when it compiles package. As far as I remember, compiler rewrites all \ into / because all our runtime procedures expect / as path delimiter. I am not sure it is safe to change compiler to leave \ alone. Alternatively, we could change runtime.Caller to rewrite all / into \ if running on windows. I am not sure what the implications are. It could also be misleading sometimes, if, for example, some packages where compiled on unix and then copied onto windows. /home/my/file.go would then become \home\my\file.go. Alex |
Alex, your example of misleading paths is valid only in a cross-compiling case. I see two obvious options: 1. Leave it as is and add a relevant comment to the func's spec; 2. Force all path separators to be replaced with OS-specific ones indicating such behavior in the func's spec as well. If source code is available, it's correct 'ab incunabulis'. If not - file was cross-compiled - you just need to do little extra work to get right paths. Maybe, it would be nice to have automatic src-detection in this context... don't know. In either case, current version of function description lacks for clarifications. |
Alex, your example of misleading paths is valid only in cross-compiling case. I see two obvious options: 1. Leave it "as is" and add a relevant comment to the func's spec; 2. Force all path separators to be replaced with OS-specific ones indicating such behavior in the func's spec as well. If source code is available, it's correct 'ab incunabulis'. If not - file was cross-compiled - you just need to do little extra work to get the right paths. Maybe, it would be nice to have automatic src-detection in this context... don't know. In either case, the current version of the function description lacks for clarifications. |
See also #18151. On Unix, the file path separator is either '/' or '' depending on how the filename got set in the first place. |
Go stdlib runtime.Caller() currently returns forward slashes on Windows (see golang/go#3335) which causes EntryCaller.TrimmedPath() to return full paths instead of the expected trimmed paths on Windows. This is because EntryCaller.TrimmedPath() uses os.PathSeparator to trim the path which is '\' on Windows. According to the discussion on the Go bug, it seems like os.PathSeparator might be '' in some cases on Unix too so might cause issues on non-Windows platforms too. This PR replaces the two occurrences of os.PathSeparator with ''/' as that is what runtime.Caller() currently produces on all platforms. Fixes: uber-go#382 See also: golango/go#18151
Go stdlib runtime.Caller() currently returns forward slashes on Windows (see golang/go#3335) which causes EntryCaller.TrimmedPath() to return full paths instead of the expected trimmed paths on Windows. This is because EntryCaller.TrimmedPath() uses os.PathSeparator to trim the path which is '' on Windows. According to the discussion on the Go bug, it seems like os.PathSeparator might be '' in some cases on Unix too so might cause issues on non-Windows platforms too. This PR replaces the two occurrences of os.PathSeparator with ''/' as that is what runtime.Caller() currently produces on all platforms. Did not add tests, as the existing tests for TrimmedPath() failed on Windows with master and work with this PR. Fixes: #382 See also: golang/go#18151
My preference here is to simply document the current behavior of always using '/' in stack frame data. That's the way it's been implemented for years, and packages like github.com/go-stack/stack that have been stable for years expect it. Changing the behavior in a future version of Go will break working code without a clearly documented benefit. Documenting the behavior will help avoid new code from making the wrong assumptions. |
Change https://go.dev/cl/603275 mentions this issue: |
by pkorotkov:
The text was updated successfully, but these errors were encountered: