Skip to content

Commit c009a2b

Browse files
authored
bug fix: parse embeds in files that contain the double quote rune (#3672)
* test: embed parsing break on double quote rune rename fixture file * fix: embed parsing now handles double quote rune
1 parent f2d409b commit c009a2b

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

go/tools/builders/BUILD.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ go_test(
99
"filter.go",
1010
"filter_test.go",
1111
"read.go",
12+
"read_test.go",
13+
],
14+
data = [
15+
"read_test_fixture.go",
16+
],
17+
deps = [
18+
"//go/runfiles",
1219
],
1320
)
1421

go/tools/builders/read.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,28 @@ func (r *importReader) findEmbed(first bool) bool {
198198
case ' ', '\t':
199199
// leave startLine alone
200200

201+
case '\'':
202+
startLine = false
203+
for r.err == nil {
204+
if r.eof {
205+
r.syntaxError()
206+
}
207+
c = r.readByteNoBuf()
208+
if c == '\\' {
209+
_ = r.readByteNoBuf()
210+
if r.err != nil {
211+
r.syntaxError()
212+
return false
213+
}
214+
continue
215+
}
216+
if c == '\'' {
217+
c = r.readByteNoBuf()
218+
goto Reswitch
219+
}
220+
}
221+
goto Reswitch
222+
201223
case '"':
202224
startLine = false
203225
for r.err == nil {
@@ -206,7 +228,7 @@ func (r *importReader) findEmbed(first bool) bool {
206228
}
207229
c = r.readByteNoBuf()
208230
if c == '\\' {
209-
r.readByteNoBuf()
231+
_ = r.readByteNoBuf()
210232
if r.err != nil {
211233
r.syntaxError()
212234
return false

go/tools/builders/read_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Copyright 2016 The Bazel Authors. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package main
17+
18+
import (
19+
"go/build"
20+
"testing"
21+
22+
"github.com/bazelbuild/rules_go/go/runfiles"
23+
)
24+
25+
func TestReadFileInfoEmbeds(t *testing.T) {
26+
rf, err := runfiles.New()
27+
if err != nil {
28+
t.Fatalf("Error creating runfiles: %v", err)
29+
}
30+
31+
f, err := rf.Rlocation("io_bazel_rules_go/go/tools/builders/read_test_fixture.go")
32+
if err != nil {
33+
t.Fatalf("Unable to get test file: %v", err)
34+
}
35+
36+
fileInfo, err := readFileInfo(build.Default, f)
37+
if err != nil {
38+
t.Fatalf("readFileInfo: %v", err)
39+
}
40+
41+
numExpectedEmbeds := 2
42+
if len(fileInfo.embeds) != numExpectedEmbeds {
43+
t.Fatalf("did not find expected number of file embeds! found %d got %d", len(fileInfo.embeds), numExpectedEmbeds)
44+
}
45+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
_ "embed"
5+
)
6+
7+
//go:embed before.sql
8+
var beforeDoubleQuoteRune string
9+
10+
func doubleQuoteRune() {
11+
rune := '"'
12+
}
13+
14+
//go:embed after.sql
15+
var afterDoubleQuoteRune string

0 commit comments

Comments
 (0)