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

Fix CallerInfo() source file paths #1288

Merged
merged 1 commit into from Jan 3, 2023

Conversation

bozaro
Copy link
Contributor

@bozaro bozaro commented Oct 26, 2022

Summary

Error Trace produce strange output with invalid file paths: it join current execution directory and base source filename, but source file can be located in another directory.

It is especially strange that these path conversions are not needed, since the desired path initially got from stacktrace.

Related issues

Fix #1230
Fix #1268

@twelvelabs
Copy link

👍 thank you so much for this - it's been driving me nuts.

@bozaro bozaro changed the title Fix output file path Fix source file path Oct 27, 2022
@bozaro bozaro changed the title Fix source file path Fix CallerInfo() source file paths Oct 27, 2022
@bozaro
Copy link
Contributor Author

bozaro commented Oct 28, 2022

I compare test output before and after this PR (source code: https://github.com/bozaro/testify-example).

go test ./example

Before

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    /home/bozaro/github/testify-example/1.8.1/example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

After

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    /home/bozaro/github/testify-example/1.8.1+1288/example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

go test -trimpath ./example

Before

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    /home/bozaro/github/testify-example/1.8.1/example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

After

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    github.com/bozaro/testify-example/example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

bazel test --test_output=errors //...

Before

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    /home/bozaro/.cache/bazel/_bazel_bozaro/b8360faabc5337403861613e54adf2d0/sandbox/linux-sandbox/122/execroot/__main__/bazel-out/k8-fastbuild/bin/example/example_test_/example_test.runfiles/__main__/example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

After

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

@bozaro
Copy link
Contributor Author

bozaro commented Oct 29, 2022

A create tree with two files:

  • example/helper/helper.go
  • example/example_test.go

And run go test ./example

Before

--- FAIL: TestHelper (0.00s)
    helper.go:9: 
        	Error Trace:	/home/bozaro/github/testify-example/1.8.1/example/helper.go:9
        	            	/home/bozaro/github/testify-example/1.8.1/example/example_test.go:14
        	Error:      	Hello from helper!
        	Test:       	TestHelper
FAIL

File /home/bozaro/github/testify-example/1.8.1/example/helper.go is not exists.

After

--- FAIL: TestHelper (0.00s)
    helper.go:9: 
        	Error Trace:	/home/bozaro/github/testify-example/1.8.1+1288/example/helper/helper.go:9
        	            	/home/bozaro/github/testify-example/1.8.1+1288/example/example_test.go:14
        	Error:      	Hello from helper!
        	Test:       	TestHelper

@bozaro
Copy link
Contributor Author

bozaro commented Nov 7, 2022

@boyan-soubachov, CallerInfo() is broken since 1.18 by printing path like filepath.Abs(path.Base(stacktracePath))

Copy link
Collaborator

@boyan-soubachov boyan-soubachov left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution :)

@boyan-soubachov boyan-soubachov merged commit 0ab3ce1 into stretchr:master Jan 3, 2023
@bozaro bozaro deleted the fix-test-path branch April 6, 2023 10:38
@hermanbanken
Copy link

This breaks the Terminal File Links detection in VSCode, if you enable trimpaths. If you use Go Modules at ~/Repos/myrepo and the module is "github.com/me/myrepo" then this prints:

--- FAIL: TestSimple (0.00s)
    example_test.go:9: 
            Error Trace:    github.com/me/myrepo/example/example_test.go:9
            Error:          Hello, world!
            Test:           TestSimple
FAIL

This is annoying, and to me it was not clear how to fix this. Apparently I had "trimpaths" enabled, but I will now disable it again. Leaving this comment for discoverability by others.

@boyan-soubachov
Copy link
Collaborator

@hermanbanken , is there a way to fix it so that it works for trimpaths as well?

@hermanbanken
Copy link

Currently not I think, except maybe only doing trimpaths for the non-current module (is that possible?). I've submitted this feature request: golang/vscode-go#2737.

I spend so much time debugging this debugging pulling my hair out that I want to avoid others from the same pain...

@dolmen dolmen added the bug label Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants