Skip to content

testing: optionally include full (or relative) path name #37708

Closed

Description

@stapelberg

Previous discussion happened in issue #21119.

Currently, t.Error calls print only the base name of the source file.

This is fine when running a single test, but in larger projects, it is a significant productivity boost to have relative (or full) path names, which make the “jump to next compilation error” feature in popular editors work.

I understand the outcome of #21119 (can’t change the default), but I suggest adding a knob to opt into printing relative or full paths.

Perhaps this could be similar to log.SetFlags, where you can specify Llongfile or Lshortfile?

Let me know if this would be okay, and I’d be happy to send a CL.

Activity

  1. mvdan commented on Mar 6, 2020

    @mvdan
    Member

    @rogpeppe, @myitcv and myself discussed a very similar idea not too long ago.

    It's unfortunate that we can't change the default. However, there is some precedent for such an opt-in flag:

    $ go tool compile -h 2>&1 | grep -- -L
      -L	show full file names in error messages
    

    I also wonder if go test could transform filename paths transparently, when it buffers output from test binaries. The only case where the test binary output goes straight to the terminal is when go test is run without arguments - and, in that case, the paths are already relative to the current directory, as we're testing the package in the current directory too. Transforming paths should be possible to do, given that test2json does very similar parsing of plaintext test output.

  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Mar 9, 2020
  3. added this to the Backlog milestone on Mar 9, 2020
  4. toothrot commented on Mar 9, 2020

    @toothrot
    Contributor

    also ccing @mpvl

  5. aryman-glympse commented on Jun 11, 2021

    @aryman-glympse

    Is there anyway we can do this similar to how the log package sets flags for log outputs?

    // setting longfile in package log.
    log.SetFlags(log.Lmicroseconds | log.Llongfile)
    

    Proposed:

    testing.SetFlags(testing.Llongfile)
    
  6. dnephin commented on Dec 17, 2022

    @dnephin
    Contributor

    #55976 was closed as a duplicate of this issue, but this one seems to be stuck in the backlog. Can we turn this into a proposal?

    Moving my comment here for consideration:

    I've noticed occasionally GoLand links to the wrong file when tests are run from the terminal window inside GoLand. I suspect it's for exactly the reason described above. The filename is ambiguous. There can be multiple files with the same name in a project, and when tests are run from the terminal window, it doesn't know which of those files to link to. An absolute path would allow this feature to work reliably.

    This problem exists even when t.Helper is used correctly. The tooling that is reading the output doesn't necessarily know which directory is the current working directory because tests can be run from any directory.

    I suspect the absolute path is really only useful when the output is being read by another application, and the filename is better when the output is being read by a human. Instead of a new flag to enable this behaviour, the existing -json flag on go test could be used to enable output of absolute paths instead of filenames.

    1c72ee7 recently added -test.v=json, which I think allows testing to detect if -json is set or not.

  7. changed the title [-]testing: optionally include full (or relative) path name[/-] [+]proposal: testing: optionally include full (or relative) path name[/+] on Dec 21, 2022
  8. moved this to Incoming in Proposalson Dec 21, 2022
  9. moved this from Incoming to Active in Proposalson Dec 21, 2022
  10. rsc commented on Dec 21, 2022

    @rsc
    Contributor

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — rsc for the proposal review group

  11. jmhwang7 commented on Dec 22, 2022

    @jmhwang7

    Excited to see that the proposal will be reviewed at the weekly proposal review meeting!

    It would be great if there was an option to print the full/relative path of the source file even when the test succeeds.

    For my company's repos, I'm working on associating tests with owners (using a CODEOWNERS file within the repo). We have large packages that multiple teams contribute too, so it's impossible to assign ownership based on package unless we take on a large amount of refactoring work. It's fairly easy however to assign ownership by files.

    I'm able to assign ownership to a testcase when it fails based on the filename printed, however, I haven't been able to assign ownership for tests that pass. Printing the full/relative path of the source file for every test run would enable this.

  12. 14 remaining items

  13. empire commented on Jan 26, 2023

    @empire
    Contributor

    @stapelberg, I've prepared a CL for this, and I'll send it in the next few days.

  14. hyangah commented on Jan 26, 2023

    @hyangah
    Contributor

    This will help vscode go extension's job much easier. (vscode runs go test or dlv test from the open file's directory).
    Can we extend this flag apply to produce absolute path names for build errors? (-gcflags=all=-L should do it in theory, but it doesn't do it now)
    That way, when a test fails due to build error, the full path will be shown with the build errors.

  15. gopherbot commented on Jan 27, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/463837 mentions this issue: testing: add -fullpath to go test

  16. stapelberg commented on Feb 23, 2023

    @stapelberg
    ContributorAuthor

    I can confirm that setting -fullpath works as expected in Emacs’s compilation-mode :)

    % ~/upstream-go/bin/go test -fullpath -count=1 -run=/sqlite -v ./...
    […]
    === RUN   TestUpdate/sqlite
    2023/02/23 19:27:28 using database: txdb/sqlite
    2023/02/23 19:27:28 Ingested image "abcdefg" (matching "scan2drive")
    2023/02/23 19:27:28 Setting desired image for machine "scan2drive" to "abcdefg"
        /home/michael/go/src/github.com/gokrazy/gus/internal/gusserver/update_test.go:61: update: diff (-want +got):
              gusapi.UpdateResponse{
              	SbomHash:     "abcdefg",
            - 	RegistryType: "localdiska",
            + 	RegistryType: "localdisk",
              	DownloadLink: "/doesnotexist/disk.gaf",
              }
    

    Pressing M-g n brings me into the correct file and line 🎉

    Thanks for getting this implemented :)

  17. gopherbot commented on May 25, 2023

    @gopherbot
    Contributor

    Change https://go.dev/cl/498396 mentions this issue: doc/go1.21: mention new go test -fullpath option

  18. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on May 26, 2023
  19. modified the milestones: Backlog, Go1.21 on May 26, 2023
  20. removed this from Proposalson Mar 1, 2024
  21. locked and limited conversation to collaborators on May 25, 2024
  22. added a commit that references this issue on Sep 29, 2026
    81316ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions