1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

internal/imports: make ApplyFixes work despite syntax errors

ApplyFixes is only used by gopls, which cares a lot about files with syntax
errors, and not at all about files that aren't structurally valid. Use
the standard ParseFile function instead of goimports' weird one.

Adding a test is impractical because it seems to break type checking of
whatever package it's in.

Fixes golang/go#35915.

Change-Id: Iaf0e331978415428a422d942a1e0c5f6e66dc8a1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/209579
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2019-12-02 14:46:06 -05:00
parent aa29eadba2
commit 5a103c92be

View File

@ -90,16 +90,25 @@ func ApplyFixes(fixes []*ImportFix, filename string, src []byte, opt *Options) (
return nil, err
}
// Don't use parse() -- we don't care about fragments or statement lists
// here, and we need to work with unparseable files.
fileSet := token.NewFileSet()
file, adjust, err := parse(fileSet, filename, src, opt)
if err != nil {
parserMode := parser.Mode(0)
if opt.Comments {
parserMode |= parser.ParseComments
}
if opt.AllErrors {
parserMode |= parser.AllErrors
}
file, err := parser.ParseFile(fileSet, filename, src, parserMode)
if file == nil {
return nil, err
}
// Apply the fixes to the file.
apply(fileSet, file, fixes)
return formatFile(fileSet, file, src, adjust, opt)
return formatFile(fileSet, file, src, nil, opt)
}
// GetAllCandidates gets all of the standard library candidate packages to import in