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

support Go data race detector #17510

Merged
merged 7 commits into from Nov 21, 2022
Merged

Conversation

tdyas
Copy link
Contributor

@tdyas tdyas commented Nov 9, 2022

Support the Go data race detector.

Adds a race field to the go_mod and go_binary target types and a test_race field to for go_package for tests. The --go-test-force-race option will enable the race detector for tests regardless of the fields. The "outermost" relevant field will take effect; that is, a race field on go_binary will override any race field on the relevant go_mod, and with tests a test_race field on the relevant go_package will override any race field on the relevant go_mod.

See https://go.dev/doc/articles/race_detector for additional information about the Go data race detector.

Fixes #17437.

@tdyas tdyas added backend: Go Go backend-related issues category:new feature labels Nov 9, 2022
@tdyas
Copy link
Contributor Author

tdyas commented Nov 9, 2022

Bike shed for naming the fields: #17517

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Thanks!

src/python/pants/backend/go/subsystems/gotest.py Outdated Show resolved Hide resolved
src/python/pants/backend/go/target_types.py Outdated Show resolved Hide resolved
src/python/pants/backend/go/target_types.py Outdated Show resolved Hide resolved
Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

I like the field renames and help messages. Thanks!

Comment on lines 102 to 115
# Extract the `with_race_detector` value for this target.
with_race_detector: bool | None = None
if go_test_subsystem.force_race:
with_race_detector = True
if test_target_fields is not None and test_target_fields.test_race.value is not None:
with_race_detector = test_target_fields.test_race.value
if with_race_detector is None:
if target_fields is not None and target_fields.race.value is not None:
with_race_detector = target_fields.race.value
if with_race_detector is None:
if go_mod_target_fields.race is not None:
with_race_detector = go_mod_target_fields.race.value
if with_race_detector is None:
with_race_detector = False
Copy link
Contributor

Choose a reason for hiding this comment

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

This may be valuable to extract into a helper function so that you can unit test. Lots of conditions here

Copy link
Contributor

Choose a reason for hiding this comment

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

Also how come not using elif?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each of those if with_race_detector is None clauses have an inner if clause that may not be triggered. Thus with_race_detector could continue to be None. elif would have different semantics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simplified the code by introducing a _first_non_none_value helper. Added tests as well of the various combinations.

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Great refactor and tests. Thank you!

@tdyas tdyas merged commit 1a1c35c into pantsbuild:main Nov 21, 2022
@tdyas tdyas deleted the golang_race_detector branch November 21, 2022 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: Go Go backend-related issues category:new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

go: support enabling data race detector
2 participants