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

Changing imports removes comment at the top of a file #54

Closed
breml opened this issue Jun 8, 2022 · 1 comment · Fixed by #55
Closed

Changing imports removes comment at the top of a file #54

breml opened this issue Jun 8, 2022 · 1 comment · Fixed by #55

Comments

@breml
Copy link
Contributor

breml commented Jun 8, 2022

Given a Go source file with a comment at the top of the file (not a package comment):

// Copyright 2022 ...

package main

import (
	"cmd/internal/edit"
)

func main() {
	_ = edit.Buffer
}

and a patch to modify the imports:

@@
var randomIdentifier identifier
@@
-import "cmd/internal/edit"
+import "github.com/foo/bar/internal/edit"

edit.randomIdentifier

the comment at the top is removed by gopatch, resulting with the following file:

package main

import "github.com/foo/bar/internal/edit"

func main() {
	_ = edit.Buffer
}
@breml
Copy link
Contributor Author

breml commented Jun 8, 2022

The respective testdata file:

-- in.patch --
@@
var randomIdentifier identifier
@@
-import "cmd/internal/edit"
+import "github.com/foo/bar/internal/edit"

edit.randomIdentifier

-- comment.in.go --
// Copyright 2022 ...

package main

import (
	"cmd/internal/edit"
)

func main() {
	_ = edit.Buffer
}

-- comment.out.go --
// Copyright 2022 ...

package main

import "github.com/foo/bar/internal/edit"

func main() {
	_ = edit.Buffer
}

This returns:

$ go test .
--- FAIL: TestIntegration (0.04s)
    --- FAIL: TestIntegration/replace_import_with_top_level_comment (0.00s)
        --- FAIL: TestIntegration/replace_import_with_top_level_comment/comment (0.00s)
            e2e_test.go:127: 
                        Error Trace:    e2e_test.go:127
                        Error:          Not equal: 
                                        expected: "// Copyright 2022 ...\n\npackage main\n\nimport \"github.com/foo/bar/internal/edit\"\n\nfunc main() {\n\t_ = edit.Buffer\n}\n"
                                        actual  : "package main\n\nimport \"github.com/foo/bar/internal/edit\"\n\nfunc main() {\n\t_ = edit.Buffer\n}\n"
                                    
                                        Diff:
                                        --- Expected
                                        +++ Actual
                                        @@ -1,3 +1 @@
                                        -// Copyright 2022 ...
                                        -
                                         package main
                        Test:           TestIntegration/replace_import_with_top_level_comment/comment
FAIL
FAIL    github.com/uber-go/gopatch      0.047s
FAIL

breml added a commit to breml/gopatch that referenced this issue Jun 8, 2022
abhinav added a commit that referenced this issue Jul 26, 2022
Prepare a new release of gopatch with a fix for #54.

This will release a binary compiled against Go 1.18,
so it will be able to parse generics syntax.
Verify that we're able to parse generics syntax with two new integration
tests.
abhinav added a commit that referenced this issue Jul 26, 2022
Prepare a new release of gopatch with a fix for #54.

This will release a binary compiled against Go 1.18,
so it will be able to parse generics syntax.
Verify that we're able to parse generics syntax with two new integration
tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant