Skip to content

Switch from x/net/context -> context#36904

Merged
thaJeztah merged 1 commit intomoby:masterfrom
kolyshkin:context
Apr 24, 2018
Merged

Switch from x/net/context -> context#36904
thaJeztah merged 1 commit intomoby:masterfrom
kolyshkin:context

Conversation

@kolyshkin
Copy link
Copy Markdown
Contributor

@kolyshkin kolyshkin commented Apr 19, 2018

Since Go 1.7, context is a standard package. Since Go 1.9, all x/net/context is a few aliases to types in context.

Many vendored packages still use x/net/context, so vendor entry remains for now.

The commit is generated by this ugly script (I'm bad at sed so had to use awk in between):

#!/bin/bash

for f in $(git ls-files \*.go | grep -v ^vendor/); do
	printf .
	sed -i 's|"golang.org/x/net/context"|"context"|' $f
# fix the position of "context" line
	goimports -w $f
# remove the empty line before "context"
	awk '/^$/ {e=1; next;}
		/^\t"context"$/ {e=0;}
		{if (e) {print ""; e=0}; print;}' < $f > $f.new && mv $f.new $f
# fix the position of "context" line again
	goimports -w $f
done
echo

Note that there is a "standard" way of doing this change -- by running the go tool fix -r context, what it does is the same as the sed above. In addition, I had to fix the "context" line placement which was tricky in case there are empty lines in the imports list (thus the two goimports with an awk in between.

@thaJeztah
Copy link
Copy Markdown
Member

We'll probably need something similar for swarmkit, containerd, libnetwork as well (assuming it works)

@thaJeztah
Copy link
Copy Markdown
Member

Looks like you need to (re-)vendor something

22:19:13 ---> Making bundle: test-integration (in bundles/test-integration)
22:19:13 Building test suite binary ./integration-cli/test.main
22:19:13 # github.com/docker/docker/integration-cli
22:19:13 package github.com/docker/docker/integration-cli (test)
22:19:13 	imports github.com/docker/docker/client
22:19:13 	imports context/ctxhttp: cannot find package "context/ctxhttp" in any of:
22:19:13 	/go/src/github.com/docker/docker/vendor/context/ctxhttp (vendor tree)
22:19:13 	/usr/local/go/src/context/ctxhttp (from $GOROOT)
22:19:13 	/go/src/context/ctxhttp (from $GOPATH)

@kolyshkin
Copy link
Copy Markdown
Contributor Author

We'll probably need something similar for swarmkit, containerd, libnetwork as well (assuming it works)

Looking into it...

Looks like you need to (re-)vendor something

Oops, the sed regex was too greedy. Fixed.

@kolyshkin
Copy link
Copy Markdown
Contributor Author

It looks like as long as Go >= 1.9 is used, we can mix x/net/context and context freely.

@kolyshkin
Copy link
Copy Markdown
Contributor Author

Nice, it looks like the switch revealed a couple of bugs in the code:

15:44:42 pkg/ioutils/readers_test.go:83::error: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (vet)
15:44:42 daemon/cluster/swarm.go:507::error: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (vet)

@dnephin
Copy link
Copy Markdown
Member

dnephin commented Apr 19, 2018

The commit is generated by this ugly script

FYI, you can do this migration with go tool fix -r context <paths> (using go1.10)

@kolyshkin
Copy link
Copy Markdown
Contributor Author

FYI, you can do this migration with go tool fix -r context (using go1.10)

Just found that out myself. The problems with the above tool are:

  1. It fixes stuff in vendor/ too.
  2. It doesn't fix the placement of the "context" line in imports list in case there are empty lines in it.

@kolyshkin
Copy link
Copy Markdown
Contributor Author

CI failure on experimental:

16:12:47 --- FAIL: TestCreateServiceConfigFileMode (12.36s)
16:12:47 	daemon.go:269: [d7e8cb005d1a9] waiting for daemon to start
16:12:47 	daemon.go:301: [d7e8cb005d1a9] daemon started
16:12:47 	create_test.go:295: timeout hit after 10s: task count at 1 waiting for 0
16:12:47 	daemon.go:259: [d7e8cb005d1a9] exiting daemon

Hmm, it does not look related but does not look like a flaky test either

@thaJeztah
Copy link
Copy Markdown
Member

I think I just saw that one fail on another PR as well

@dnephin
Copy link
Copy Markdown
Member

dnephin commented Apr 20, 2018

The problems with the above tool are:

I guess it would need a git reset HEAD vendor && goimports -w ./...

@thaJeztah
Copy link
Copy Markdown
Member

@kolyshkin needs a rebase 😅, also perhaps it's good to keep the fixes in a separate PR

@kolyshkin
Copy link
Copy Markdown
Contributor Author

I guess it would need a git reset HEAD vendor && goimports -w ./...

Unfortunately not -- in case there is an empty line between "standard" and "prefixed" packages, it still won't have "context" in the line it should be, this is why I have to do goimports -w twice in my list, with an empty line removal in between.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 20, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@5c233cf). Click here to learn what that means.
The diff coverage is n/a.

@@            Coverage Diff            @@
##             master   #36904   +/-   ##
=========================================
  Coverage          ?   35.06%           
=========================================
  Files             ?      614           
  Lines             ?    45694           
  Branches          ?        0           
=========================================
  Hits              ?    16024           
  Misses            ?    27565           
  Partials          ?     2105

@kolyshkin
Copy link
Copy Markdown
Contributor Author

kolyshkin commented Apr 20, 2018

@kolyshkin needs a rebase

done

also perhaps it's good to keep the fixes in a separate PR

will do

@kolyshkin
Copy link
Copy Markdown
Contributor Author

also perhaps it's good to keep the fixes in a separate PR

done (#36920), but this way CI here will fail as in #36904 (comment)

@kolyshkin
Copy link
Copy Markdown
Contributor Author

rebased again

Copy link
Copy Markdown
Member

@cpuguy83 cpuguy83 left a comment

Choose a reason for hiding this comment

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

If CI is happy I'm happy. LGTM!

@cpuguy83
Copy link
Copy Markdown
Member

06:28:53 daemon/cluster/swarm.go:507::error: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (vet)
06:28:53 pkg/ioutils/readers_test.go:83::error: the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak (vet)

@kolyshkin
Copy link
Copy Markdown
Contributor Author

@cpuguy83 the fix for this is separated to #36920 as per request in #36904 (comment)

@kolyshkin kolyshkin changed the title [WIP] Switch from x/net/context -> context Switch from x/net/context -> context Apr 23, 2018
Since Go 1.7, context is a standard package. Since Go 1.9, everything
that is provided by "x/net/context" is a couple of type aliases to
types in "context".

Many vendored packages still use x/net/context, so vendor entry remains
for now.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin
Copy link
Copy Markdown
Contributor Author

Rebased to master (with #36920 merged)

Copy link
Copy Markdown
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants