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

bzltestutil: set importmap to fix run_dir #3679

Merged
merged 1 commit into from
Sep 5, 2023

Conversation

jayconrod
Copy link
Contributor

What type of PR is this?

Bug fix

What does this PR do? Why is it needed?

Go 1.21 clarified package initialization order and changed behavior: packages with lexicographically lower paths are now initialized earlier.

We need bzltestutil to initialize before user packages so it can change to the correct directory. To accomplish this, we set an importmap prefix that starts with '+', the lowest allowed character.

Which issues(s) does this PR fix?

Fixes #3675

Go 1.21 clarified package initialization order and changed behavior:
packages with lexicographically lower paths are now initialized earlier.

We need bzltestutil to initialize before user packages so it can
change to the correct directory. To accomplish this, we set an importmap
prefix that starts with '+', the lowest allowed character.

Fixes bazelbuild#3675
Copy link
Collaborator

@fmeum fmeum left a comment

Choose a reason for hiding this comment

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

Thanks, very neat!

@fmeum fmeum merged commit a8cb4b7 into bazelbuild:master Sep 5, 2023
2 checks passed
@fmeum
Copy link
Collaborator

fmeum commented Sep 6, 2023

@jayconrod I rebased #3643 onto your fix, but test_chdir fails (https://buildkite.com/bazel/rules-go-golang/builds/5510). Could you look into that?

@jayconrod
Copy link
Contributor Author

@fmeum Sure, will take a look today.

jayconrod added a commit to jayconrod/rules_go that referenced this pull request Sep 6, 2023
Sorry for missing this the first time in bazelbuild#3679. The package init algorithm
was a little different than I thought, and I imagined that Go 1.21
was already in use, so I didn't look into it deeply.

The problem was that even though "+initfirst/.../bzlutil" sorts lower than
other packages, it imports "sync" and a number of other std packages
that sort higher than "github.com/..." user packages. If a user package
has no dependencies, it's eligible to be initialized before bzltestutil,
even with the lower name.

I moved the init function with the os.Chdir call into a separate tiny package
that only imports "os" and "runtime". It should get initialized much earlier.

Fixes bazelbuild#3675
fmeum pushed a commit that referenced this pull request Sep 7, 2023
* bzltestutil: move os.Chdir call into new package

Sorry for missing this the first time in #3679. The package init algorithm
was a little different than I thought, and I imagined that Go 1.21
was already in use, so I didn't look into it deeply.

The problem was that even though "+initfirst/.../bzlutil" sorts lower than
other packages, it imports "sync" and a number of other std packages
that sort higher than "github.com/..." user packages. If a user package
has no dependencies, it's eligible to be initialized before bzltestutil,
even with the lower name.

I moved the init function with the os.Chdir call into a separate tiny package
that only imports "os" and "runtime". It should get initialized much earlier.

Fixes #3675

* address review feedback

* not
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

go_test: I/O in package init function fails after Go 1.21 due to initialization order change
2 participants