1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
#!/bin/bash
#
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Load the test setup defined in the parent directory
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${CURRENT_DIR}/../integration_test_setup.sh" \
|| { echo "integration_test_setup.sh not found!" >&2; exit 1; }
export JAVA_RUNFILES=$BAZEL_RUNFILES
function setup_repo() {
mkdir -p $1
touch $1/WORKSPACE
echo $2 > $1/thing
cat > $1/BUILD <<EOF
genrule(
name = "x",
srcs = ["thing"],
cmd = "cat \$(location thing) > \$@",
outs = ["out"],
)
EOF
}
function test_workspace_changes() {
repo_a=$TEST_TMPDIR/a
repo_b=$TEST_TMPDIR/b
setup_repo $repo_a hi
setup_repo $repo_b bye
cat > WORKSPACE <<EOF
local_repository(
name = "x",
path = "$repo_a",
)
EOF
bazel build @x//:x || fail "build failed"
assert_contains "hi" bazel-genfiles/external/x/out
cat > WORKSPACE <<EOF
local_repository(
name = "x",
path = "$repo_b",
)
EOF
bazel build @x//:x || fail "build failed"
assert_contains "bye" bazel-genfiles/external/x/out
}
function test_path_with_spaces() {
ws="a b"
mkdir "$ws"
cd "$ws"
touch WORKSPACE
bazel info &> $TEST_log && fail "Info succeeeded"
bazel help &> $TEST_log || fail "Help failed"
}
# Tests for middleman conflict when using workspace repository
function test_middleman_conflict() {
local test_repo1=$TEST_TMPDIR/repo1
local test_repo2=$TEST_TMPDIR/repo2
mkdir -p $test_repo1
mkdir -p $test_repo2
echo "1" >$test_repo1/test.in
echo "2" >$test_repo2/test.in
echo 'filegroup(name="test", srcs=["test.in"], visibility=["//visibility:public"])' \
>$test_repo1/BUILD
echo 'filegroup(name="test", srcs=["test.in"], visibility=["//visibility:public"])' \
>$test_repo2/BUILD
touch $test_repo1/WORKSPACE
touch $test_repo2/WORKSPACE
cat > WORKSPACE <<EOF
local_repository(name = 'repo1', path='$test_repo1')
local_repository(name = 'repo2', path='$test_repo2')
EOF
cat > BUILD <<'EOF'
genrule(
name = "test",
srcs = ["@repo1//:test", "@repo2//:test"],
outs = ["test.out"],
cmd = "cat $(SRCS) >$@"
)
EOF
bazel fetch //:test || fail "Fetch failed"
bazel build //:test || echo "Expected build to succeed"
check_eq "12" "$(cat bazel-genfiles/test.out | tr -d '[:space:]')"
}
# Regression test for issue #724: NullPointerException in WorkspaceFile
function test_error_in_workspace_file() {
# Create a buggy workspace
cat >WORKSPACE <<'EOF'
/
EOF
# Try to refer to the workspace.
bazel --batch build @r//:rfg &>$TEST_log \
&& fail "Failure expected" || true
expect_not_log "Exception"
}
function test_no_select() {
cat > WORKSPACE <<EOF
new_local_repository(
name = "foo",
path = "/path/to/foo",
build_file = select({
"//x:y" : "BUILD.1",
"//conditions:default" : "BUILD.2"}),
)
EOF
bazel build @foo//... &> $TEST_log && fail "Failure expected" || true
expect_log "select() cannot be used in WORKSPACE files"
}
function test_macro_select() {
cat > WORKSPACE <<EOF
load('//:foo.bzl', 'foo_repo')
foo_repo()
EOF
touch BUILD
cat > foo.bzl <<EOF
def foo_repo():
native.new_local_repository(
name = "foo",
path = "/path/to/foo",
build_file = select({
"//x:y" : "BUILD.1",
"//conditions:default" : "BUILD.2"}),
)
EOF
bazel build @foo//... &> $TEST_log && fail "Failure expected" || true
expect_log "select() cannot be used in macros called from WORKSPACE files"
}
function test_clean() {
mkdir x
cd x
cat > WORKSPACE <<EOF
workspace(name = "y")
EOF
cat > BUILD <<'EOF'
genrule(name = "z", cmd = "echo hi > $@", outs = ["x.out"], srcs = [])
EOF
bazel build //:z &> $TEST_log || fail "Expected build to succeed"
[ -L bazel-x ] || fail "bazel-x should be a symlink"
bazel clean
[ ! -L bazel-x ] || fail "bazel-x should have been removed"
}
function test_skylark_flags_affect_workspace() {
cat > WORKSPACE <<EOF
load("//:macro.bzl", "macro")
print("In workspace: ")
macro()
EOF
cat > macro.bzl <<EOF
def macro():
print("In workspace macro: ")
EOF
cat > BUILD <<'EOF'
genrule(name = "x", cmd = "echo hi > $@", outs = ["x.out"], srcs = [])
EOF
MARKER="<== skylark flag test ==>"
# Sanity check.
bazel build //:x &>"$TEST_log" \
|| fail "Expected build to succeed"
expect_log "In workspace: " "Did not find workspace print output"
expect_log "In workspace macro: " "Did not find workspace macro print output"
expect_not_log "$MARKER" \
"Marker string '$MARKER' was seen even though \
--internal_skylark_flag_test_canary wasn't passed"
# Build with the special testing flag that appends a marker string to all
# print() calls.
bazel build //:x --internal_skylark_flag_test_canary &>"$TEST_log" \
|| fail "Expected build to succeed"
expect_log "In workspace: $MARKER" \
"Skylark flags are not propagating to workspace evaluation"
expect_log "In workspace macro: $MARKER" \
"Skylark flags are not propagating to workspace macro evaluation"
}
function test_workspace_name() {
mkdir -p foo
mkdir -p bar
cat > foo/WORKSPACE <<EOF
workspace(name = "foo")
local_repository(
name = "bar",
path = "$PWD/bar",
)
EOF
cat > foo/BUILD <<EOF
exports_files(glob(["*"]))
EOF
touch foo/baz
cat > bar/WORKSPACE <<EOF
workspace(name = "bar")
EOF
cat > bar/BUILD <<EOF
genrule(
name = "depend-on-foo",
srcs = ["@foo//:baz"],
cmd = "cat \$(SRCS) > \$@",
outs = ["baz.out"],
)
EOF
cd foo
bazel build @bar//:depend-on-foo || fail "Expected build to succeed"
}
function test_workspace_override() {
mkdir -p original
touch original/WORKSPACE
cat > original/BUILD <<'EOF'
genrule(
name = "gen",
cmd = "echo 'original' > $@",
outs = ["gen.out"],
)
EOF
mkdir -p override
touch override/WORKSPACE
cat > override/BUILD <<'EOF'
genrule(
name = "gen",
cmd = "echo 'override' > $@",
outs = ["gen.out"],
)
EOF
cat > WORKSPACE <<EOF
local_repository(
name = "o",
path = "original",
)
EOF
bazel build --override_repository="o=$PWD/override" @o//:gen &> $TEST_log \
|| fail "Expected build to succeed"
assert_contains "override" bazel-genfiles/external/o/gen.out
bazel build @o//:gen &> $TEST_log \
|| fail "Expected build to succeed"
assert_contains "original" bazel-genfiles/external/o/gen.out
bazel build --override_repository="o=$PWD/override" @o//:gen &> $TEST_log \
|| fail "Expected build to succeed"
assert_contains "override" bazel-genfiles/external/o/gen.out
}
function test_workspace_addition_change() {
mkdir -p repo_one
mkdir -p repo_two
cat > repo_one/BUILD <<EOF
genrule(
name = "gen",
cmd = "echo 'This is repo_one' > \$@",
outs = ["gen.out"],
)
EOF
cat > repo_two/BUILD <<EOF
genrule(
name = "gen",
cmd = "echo 'This is repo_two' > \$@",
outs = ["gen.out"],
)
EOF
touch WORKSPACE
cat > repo_one/WORKSPACE <<EOF
workspace(name = "new_repo")
EOF
cat > repo_two/WORKSPACE <<EOF
workspace(name = "new_repo")
EOF
bazel build @new_repo//:gen \
&& fail "Failure expected" || true
bazel build --override_repository="new_repo=$PWD/repo_one" @new_repo//:gen \
|| fail "Expected build to succeed"
assert_contains "This is repo_one" bazel-genfiles/external/new_repo/gen.out
bazel build --override_repository="new_repo=$PWD/repo_two" @new_repo//:gen \
|| fail "Expected build to succeed"
assert_contains "This is repo_two" bazel-genfiles/external/new_repo/gen.out
}
function test_package_loading_with_remapping_changes() {
# structure is
# workspace/
# WORKSPACE (name=main)
# flower/
# WORKSPACE (name=flower)
# daisy/
# BUILD (:daisy)
# tree/
# WORKSPACE (name=tree, local_repository(flower))
# oak/
# BUILD (:oak)
mkdir -p flower/daisy
echo 'workspace(name="flower")' > flower/WORKSPACE
echo 'sh_library(name="daisy")' > flower/daisy/BUILD
mkdir -p tree/oak
cat > tree/WORKSPACE <<EOF
workspace(name="tree")
local_repository(
name = "flower",
path="../flower",
repo_mapping = {"@tulip" : "@rose"}
)
EOF
echo 'sh_library(name="oak")' > tree/oak/BUILD
cd tree
# Do initial load of the packages
bazel query --experimental_enable_repo_mapping --noexperimental_ui \
//oak:all >& "$TEST_log" || fail "Expected success"
expect_log "Loading package: oak"
expect_log "//oak:oak"
bazel query --experimental_enable_repo_mapping --noexperimental_ui \
@flower//daisy:all >& "$TEST_log" || fail "Expected success"
expect_log "Loading package: @flower//daisy"
expect_log "@flower//daisy:daisy"
# Change mapping in tree/WORKSPACE
cat > WORKSPACE <<EOF
workspace(name="tree")
local_repository(
name = "flower",
path="../flower",
repo_mapping = {"@tulip" : "@daffodil"}
)
EOF
# Test that packages in the tree workspace are not affected
bazel query --experimental_enable_repo_mapping --noexperimental_ui \
//oak:all >& "$TEST_log" || fail "Expected success"
expect_not_log "Loading package: oak"
expect_log "//oak:oak"
# Test that packages in the flower workspace are reloaded
bazel query --experimental_enable_repo_mapping --noexperimental_ui \
@flower//daisy:all >& "$TEST_log" || fail "Expected success"
expect_log "Loading package: @flower//daisy"
expect_log "@flower//daisy:daisy"
}
function test_repository_mapping_in_build_file_load() {
# Main repo assigns @x to @y within @a
mkdir -p main
cat > main/WORKSPACE <<EOF
workspace(name = "main")
local_repository(name = "a", path="../a", repo_mapping = {"@x" : "@y"})
local_repository(name = "y", path="../y")
EOF
touch main/BUILD
# Repository y is a substitute for x
mkdir -p y
touch y/WORKSPACE
touch y/BUILD
cat > y/symbol.bzl <<EOF
Y_SYMBOL = "y_symbol"
EOF
# Repository a refers to @x
mkdir -p a
touch a/WORKSPACE
cat > a/BUILD<<EOF
load("@x//:symbol.bzl", "Y_SYMBOL")
genrule(name = "a",
outs = ["result.txt"],
cmd = "echo %s > \$(location result.txt);" % (Y_SYMBOL)
)
EOF
cd main
bazel build --experimental_enable_repo_mapping @a//:a || fail "Expected build to succeed"
cat bazel-genfiles/external/a/result.txt
grep "y_symbol" bazel-genfiles/external/a/result.txt \
|| fail "expected 'y_symbol' in $(cat bazel-genfiles/external/a/result.txt)"
}
function test_repository_reassignment_label_in_build() {
# Repository a refers to @x
mkdir -p a
touch a/WORKSPACE
cat > a/BUILD<<EOF
genrule(name = "a",
srcs = ["@x//:x.txt"],
outs = ["result.txt"],
cmd = "echo hello > \$(location result.txt)"
)
EOF
# Repository b is a substitute for x
mkdir -p b
touch b/WORKSPACE
cat >b/BUILD <<EOF
exports_files(srcs = ["x.txt"])
EOF
echo "Hello from @b//:x.txt" > b/x.txt
# Main repo assigns @x to @b within @a
mkdir -p main
cat > main/WORKSPACE <<EOF
workspace(name = "main")
local_repository(name = "a", path="../a", repo_mapping = {"@x" : "@b"})
local_repository(name = "b", path="../b")
EOF
touch main/BUILD
cd main
bazel query --experimental_enable_repo_mapping --output=build @a//:a | grep "@b//:x.txt" \
|| fail "Expected srcs to contain '@b//:x.txt'"
}
function test_repository_reassignment_location() {
# Repository a refers to @x
mkdir -p a
touch a/WORKSPACE
cat > a/BUILD<<EOF
genrule(name = "a",
srcs = ["@x//:x.txt"],
outs = ["result.txt"],
cmd = "echo \$(location @x//:x.txt) > \$(location result.txt); \
cat \$(location @x//:x.txt)>> \$(location result.txt);"
)
EOF
# Repository b is a substitute for x
mkdir -p b
touch b/WORKSPACE
cat >b/BUILD <<EOF
exports_files(srcs = ["x.txt"])
EOF
echo "Hello from @b//:x.txt" > b/x.txt
# Main repo assigns @x to @b within @a
mkdir -p main
cat > main/WORKSPACE <<EOF
workspace(name = "main")
local_repository(name = "a", path="../a", repo_mapping = {"@x" : "@b"})
local_repository(name = "b", path="../b")
EOF
touch main/BUILD
cd main
bazel build --experimental_enable_repo_mapping @a//:a || fail "Expected build to succeed"
grep "external/b/x.txt" bazel-genfiles/external/a/result.txt \
|| fail "expected external/b/x.txt in $(cat bazel-genfiles/external/a/result.txt)"
}
function test_workspace_addition_change_aspect() {
mkdir -p repo_one
mkdir -p repo_two
touch foo.c
cat > BUILD <<EOF
cc_library(
name = "lib",
srcs = ["foo.c"],
)
EOF
touch WORKSPACE
touch repo_one/BUILD
touch repo_two/BUILD
cat > repo_one/WORKSPACE <<EOF
workspace(name = "new_repo")
EOF
cat > repo_two/WORKSPACE <<EOF
workspace(name = "new_repo")
EOF
cat > repo_one/aspects.bzl <<EOF
def _print_aspect_impl(target, ctx):
# Make sure the rule has a srcs attribute.
if hasattr(ctx.rule.attr, 'srcs'):
# Output '1' for each file in srcs.
for src in ctx.rule.attr.srcs:
for f in src.files:
print(1)
return []
print_aspect = aspect(
implementation = _print_aspect_impl,
attr_aspects = ['deps'],
)
EOF
cat > repo_two/aspects.bzl <<EOF
def _print_aspect_impl(target, ctx):
# Make sure the rule has a srcs attribute.
if hasattr(ctx.rule.attr, 'srcs'):
print(ctx.rule.attr.srcs)
return []
print_aspect = aspect(
implementation = _print_aspect_impl,
attr_aspects = ['deps'],
)
EOF
bazel clean --expunge
echo; echo "no repo"; echo
bazel build //:lib --aspects @new_repo//:aspects.bzl%print_aspect \
&& fail "Failure expected" || true
echo; echo "repo_one"; echo
bazel build //:lib --override_repository="new_repo=$PWD/repo_one" \
--aspects @new_repo//:aspects.bzl%print_aspect \
|| fail "Expected build to succeed"
echo; echo "repo_two"; echo
bazel build //:lib --override_repository="new_repo=$PWD/repo_two" \
--aspects @new_repo//:aspects.bzl%print_aspect \
|| fail "Expected build to succeed"
}
function test_mainrepo_name_is_not_different_repo() {
# Repository a refers to @x
mkdir -p mainrepo
echo "workspace(name = 'mainrepo')" > mainrepo/WORKSPACE
cat > mainrepo/BUILD<<EOF
load("//:def.bzl", "a")
load("@mainrepo//:def.bzl", "a")
EOF
cat > mainrepo/def.bzl<<EOF
print("def.bzl loaded")
a = 1
EOF
cd mainrepo
bazel query --experimental_remap_main_repo //... &>"$TEST_log" \
|| fail "Expected query to succeed"
expect_log "def.bzl loaded"
expect_not_log "external"
}
function test_mainrepo_name_remapped_properly() {
mkdir -p mainrepo
touch mainrepo/BUILD
cat > mainrepo/WORKSPACE<<EOF
workspace(name = "mainrepo")
local_repository(
name = "a",
path = "../a"
)
EOF
cat > mainrepo/def.bzl<<EOF
print ("def.bzl loaded")
x = 10
EOF
mkdir -p a
touch a/WORKSPACE
echo "load('@mainrepo//:def.bzl', 'x')"> a/BUILD
# the bzl file should be loaded from the main workspace and
# not as an external repository
cd mainrepo
bazel query --experimental_remap_main_repo @a//... &>"$TEST_log" \
|| fail "Expected query to succeed"
expect_log "def.bzl loaded"
expect_not_log "external"
cd ..
cat > mainrepo/WORKSPACE<<EOF
workspace(name = "mainrepo")
local_repository(
name = "a",
path = "../a",
repo_mapping = {"@mainrepo" : "@newname"}
)
EOF
# now that @mainrepo doesn't exist within workspace "a",
# the query should fail
cd mainrepo
bazel query --experimental_remap_main_repo --experimental_enable_repo_mapping \
@a//... &>"$TEST_log" \
&& fail "Failure expected" || true
}
run_suite "workspace tests"
|