Skip to content

x/tools/gopls/internal/analysis/modernize: stringsbuilder breaks code where string is reassigned #75283

@jakebailey

Description

@jakebailey

We have code that looks like this:

func getLibraryNameFromLibFileName(libFileName string) string {
	// Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
	//                      lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
	//                      lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
	components := strings.Split(libFileName, ".")
	var path string
	if len(components) > 1 {
		path = components[1]
	}
	i := 2
	for i < len(components) && components[i] != "" && components[i] != "d" {
		path += core.IfElse(i == 2, "/", "-") + components[i]
		i++
	}
	return "@typescript/lib-" + path
}

Note the path = components[1]

But, the new modernizer converts this to:

func getLibraryNameFromLibFileName(libFileName string) string {
	// Support resolving to lib.dom.d.ts -> @typescript/lib-dom, and
	//                      lib.dom.iterable.d.ts -> @typescript/lib-dom/iterable
	//                      lib.es2015.symbol.wellknown.d.ts -> @typescript/lib-es2015/symbol-wellknown
	components := strings.Split(libFileName, ".")
	var path strings.Builder
	if len(components) > 1 {
		path = components[1]
	}
	i := 2
	for i < len(components) && components[i] != "" && components[i] != "d" {
		path.WriteString(core.IfElse(i == 2, "/", "-") + components[i])
		i++
	}
	return "@typescript/lib-" + path.String()
}

Which is incorrect as you can't assign to a strings.Builder. I think the analysis needs to check to verify that the string is never assigned to, only starts as some constant.

cc @alandonovan

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions