Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions src/cmd/go/internal/modindex/build_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"go/parser"
"go/scanner"
"go/token"
"internal/importpath"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)

Expand Down Expand Up @@ -325,7 +325,7 @@ func readGoInfo(f io.Reader, info *fileInfo) error {
if err != nil {
return fmt.Errorf("parser returned invalid quoted string: <%s>", quoted)
}
if !isValidImport(path) {
if !(importpath.IsValidImport(path)) {
// The parser used to return a parse error for invalid import paths, but
// no longer does, so check for and create the error here instead.
info.parseErr = scanner.Error{Pos: info.fset.Position(spec.Pos()), Msg: "invalid import path: " + path}
Expand Down Expand Up @@ -389,20 +389,6 @@ func readGoInfo(f io.Reader, info *fileInfo) error {
return nil
}

// isValidImport checks if the import is a valid import using the more strict
// checks allowed by the implementation restriction in https://go.dev/ref/spec#Import_declarations.
// It was ported from the function of the same name that was removed from the
// parser in CL 424855, when the parser stopped doing these checks.
func isValidImport(s string) bool {
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
for _, r := range s {
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return false
}
}
return s != ""
}

// parseGoEmbed parses a "//go:embed" to extract the glob patterns.
// It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings.
// This must match the behavior of cmd/compile/internal/noder.go.
Expand Down
28 changes: 28 additions & 0 deletions src/internal/importpath/importpath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package importpath

import (
"strings"
"unicode"
)

const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"

func isValidImportPathChar(r rune) bool {
return unicode.IsGraphic(r) && !unicode.IsSpace(r) && !strings.ContainsRune(illegalChars, r)
}

// IsValidImport checks if the import is a valid import using the more strict
// checks allowed by the implementation restriction in https://go.dev/ref/spec#Import_declarations.
// This logic was previously duplicated across several packages.
func IsValidImport(s string) bool {
for _, r := range s {
if !isValidImportPathChar(r) {
return false
}
}
return s != ""
}
37 changes: 37 additions & 0 deletions src/internal/importpath/importpath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package importpath

import (
"testing"
)

func TestImportPath(t *testing.T) {
tests := []string{
"net",
"net/http",
"a/b/c",
}
for _, tc := range tests {
if !IsValidImport(tc) {
t.Errorf("expected %q to be valid import path", tc)
}
}
}

func TestImportPathInvalid(t *testing.T) {
tests := []string{
"",
"foo bar",
"\uFFFD",
"hello!",
"世界",
}
for _, tc := range tests {
if IsValidImport(tc) {
t.Errorf("expected %q to be invalid import path", tc)
}
}
}