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

Fix file name reporting for Go 1.11 [v2] #31

Closed
wants to merge 1 commit into from

Conversation

kolyshkin
Copy link

@kolyshkin kolyshkin commented Jul 30, 2018

unconvert output for cgo-preprocessed files is broken when using Go 1.11beta. A quick example:

With Go 1.10:

go run unconvert.go github.com/docker/docker/daemon/graphdriver/copy
/home/kir/go/src/github.com/docker/docker/daemon/graphdriver/copy/copy.go:242:28: unnecessary conversion

With Go 1.11 beta2:

go run unconvert.go github.com/docker/docker/daemon/graphdriver/copy
copy.go:242:0: unnecessary conversion

Note that there are two issues:

  • column is 0;
  • file names have path stripped.

The issue with column is filed as golang/go#26692;
there is no way to fix it in unconvert.

The second issue is currently discussed in
golang/go#26671, but it might not be
fixed since the new documentation says:

The filenames in line directives now remain untouched by the scanner;
there is no cleanup or conversion of relative into absolute paths
anymore, in sync with what the compiler's scanner/parser are doing.
Any kind of filename transformation has to be done by a client. This
makes the scanner code simpler and also more predictable.

So, here's the alternative: if the file name is not absolute, prepend
the path as we know it.

Fixes #29

@kolyshkin
Copy link
Author

@mdempsky @jirfag PTAL

unconvert.go Outdated
@@ -43,6 +44,7 @@ func apply(file string, edits editSet) {
}

fset := token.NewFileSet()
fmt.Printf("file: %q\n", file)
Copy link

Choose a reason for hiding this comment

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

Debug?

Copy link
Author

Choose a reason for hiding this comment

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

Oops, I've missed it, thanks for catching this. Removed.

@@ -425,7 +427,13 @@ func (v *visitor) unconvert(call *ast.CallExpr) {
return
}

v.edits[v.file.Position(call.Lparen)] = struct{}{}
pos := v.file.Position(call.Lparen)
if !filepath.IsAbs(pos.Filename) { // go 1.11+, see https://github.com/golang/go/issues/26671
Copy link

Choose a reason for hiding this comment

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

For go<1.11 filename will be a/b.go, but in go1.11 filename will be b.go. Absolute path checking doesn’t help.
IMO it should be fixed in go/scanner: it’s an incompatible change and we need to fix dozens of tools because of it

Copy link
Author

@kolyshkin kolyshkin Jul 31, 2018

Choose a reason for hiding this comment

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

For go<1.11 filename will be a/b.go

I don't think so. See golang/go#26671, the code (removed by golang/go@546bab8) was:

	if filename != "" {
 		filename = filepath.Clean(filename)
 		if !filepath.IsAbs(filename) {
 			// make filename relative to current directoryfilename = filepath.Join(s.dir, filename)

Here s.dir is the path from the original file name supplied to scanner.Init(), and it could either be relative or absolute. Due to a way unconvert handles input (using golang.org/x/tools/go/loader) file names supplied are absolute.

The above it theory; my tests confirm it.

unconvert output is broken when using Go 1.11. A quick example:

With Go 1.10:

> go run unconvert.go github.com/docker/docker/daemon/graphdriver/copy
> /home/kir/go/src/github.com/docker/docker/daemon/graphdriver/copy/copy.go:242:28: unnecessary conversion

With Go 1.11 beta 2:
> go run unconvert.go github.com/docker/docker/daemon/graphdriver/copy
> copy.go:242:0: unnecessary conversion

Note that there are two issues:
 - column is 0;
 - file names have path stripped.

The issue with column is filed as golang/go#26692;
there is no way to fix it in unconvert.

The second issue is currently discussed in
golang/go#26671, but it might not be
fixed since the new documentation says:

> The filenames in line directives now remain untouched by the scanner;
> there is no cleanup or conversion of relative into absolute paths
> anymore, in sync with what the compiler's scanner/parser are doing.
> Any kind of filename transformation has to be done by a client. This
> makes the scanner code simpler and also more predictable.

So, here's the alternative: if the file name is not absolute, prepend
the path as we know it.

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

OK, since the golang/go#26671 is now closed, looks like this patch is the way to go forward.

@kolyshkin
Copy link
Author

golang/go#26671 is fixed; closing this one

@kolyshkin kolyshkin closed this Aug 3, 2018
@mdempsky
Copy link
Owner

mdempsky commented Aug 3, 2018

Thanks all for engaging upstream on this issue.

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.

file names and column info broken by go1.11beta
3 participants