Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Find solution to slow "format on save" impacting tests #786

Closed
atombender opened this issue Feb 11, 2017 · 8 comments
Closed

Find solution to slow "format on save" impacting tests #786

atombender opened this issue Feb 11, 2017 · 8 comments

Comments

@atombender
Copy link

With Go, the goimports formatter is not super fast. It can take a second for it to run on a large file — and it's almost always slower than I work. I frequently save and then immediately hit my keyboard shortcut to run the tests, and as a result, I constantly find myself in a situation where the tests are run before imports have been fixed.

For example, I might add a log.Printf() call, save, run tests, which then immediately fail with:

./functions.go:643: undefined: log in log.Printf

A solution to this would for the test runner to wait with the run until the file is fully saved. I don't know if VSCode has the necessary APIs to even do this, however.

Would it perhaps also make sense that the test runner saves a file first if it's dirty?

@atombender
Copy link
Author

I should add that this isn't just about imports. My changes don't seem to take effect until the formatter has run. I frequently end up running tests on the unsaved versions because of this latency.

@wader
Copy link

wader commented Jun 18, 2017

I have the same issue when using https://github.com/cortesi/modd. A delay work but feels hackish. Would it be possible to format the code before writing it to the file?

@ramya-rao-a
Copy link
Contributor

A solution to this would for the test runner to wait with the run until the file is fully saved. I don't know if VSCode has the necessary APIs to even do this, however.

This would just complicate things adding dependencies around, I wouldn't vote for this.

Would it perhaps also make sense that the test runner saves a file first if it's dirty?

Either that or notify the user that tests cannot be run if there are dirty files.

My changes don't seem to take effect until the formatter has run. I frequently end up running tests on the unsaved versions because of this latency.

You mean you end up running tests on the unformatted versions? Because the tests cannot run on dirty files. go test always looks at the file on disk.

Would it be possible to format the code before writing it to the file?

Work tracked in #540 will do this. Though with this, the slow formatters will be ignored and the file will be saved unformatted.

@atombender
Copy link
Author

You mean you end up running tests on the unformatted versions?

Yes, I think that's what's happening (it's been a while since I filed this issue). Since I'm using goimports as the formatter, tests will sometimes run without imports that haven't been added yet.

@ramya-rao-a
Copy link
Contributor

The one way around that I can think of is for the tests not to run if there are any build errors.

If you have buildOnSave feature on, and the test starts before formatting completes (or after #540 when the slow formatters get ignored, and file is saved unformatted), there would be build errors if there are missing imports. We could use that to notify users that there are build errors and so tests will not be run.

@bsr203
Copy link

bsr203 commented Oct 17, 2017

Hi. I don't have a lot of custom settings, but currently my source files are properly auto formatted on save, but test ("*_test.go") files are not . Is this by design? any settings to allow formatting test files as well.

@bsr203
Copy link

bsr203 commented Oct 17, 2017

Changing to

   "go.formatTool": "goimports"

makes formatting works again

could be this issue sqs/goreturns#30

@ramya-rao-a
Copy link
Contributor

With the work in #540 completed, formatting will be applied on the file before it gets saved in the latest update to the Go extension (0.6.70). Downside is that slow formatters (that which take > 750ms) are skipped and would require manual formatting. Upside is that such slow processes will not affect tests as described in this issue.

@vscodebot vscodebot bot locked and limited conversation to collaborators Feb 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants