Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey; |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 18 | import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertThrows; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.fail; |
| 21 | |
Lukacs Berki | d3262d1 | 2015-10-30 14:33:51 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableMap; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 25 | import com.google.devtools.build.lib.clock.BlazeClock; |
wyv | 4858cbf | 2022-05-04 13:56:45 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.cmdline.BazelModuleContext; |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.cmdline.Label; |
Googler | 9197839 | 2023-03-16 12:41:23 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.RuleVisibility; |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.pkgcache.PackageOptions; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Googler | 4e22092 | 2023-01-12 06:33:22 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.runtime.QuiescingExecutorsImpl; |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.skyframe.BzlLoadFunction.BzlLoadFailedException; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 38 | import com.google.devtools.build.lib.vfs.FileSystem; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.vfs.Path; |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 41 | import com.google.devtools.build.lib.vfs.Root; |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 42 | import com.google.devtools.build.lib.vfs.RootedPath; |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.skyframe.ErrorInfo; |
| 45 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 46 | import com.google.devtools.build.skyframe.SkyKey; |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 47 | import com.google.devtools.common.options.Options; |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 50 | import java.util.UUID; |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 51 | import javax.annotation.Nullable; |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 52 | import net.starlark.java.eval.StarlarkInt; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 53 | import org.junit.Before; |
| 54 | import org.junit.Test; |
| 55 | import org.junit.runner.RunWith; |
| 56 | import org.junit.runners.JUnit4; |
| 57 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 58 | /** Tests for BzlLoadFunction. */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 59 | @RunWith(JUnit4.class) |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 60 | public class BzlLoadFunctionTest extends BuildViewTestCase { |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 61 | |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 62 | @Override |
| 63 | protected FileSystem createFileSystem() { |
| 64 | return new CustomInMemoryFs(); |
| 65 | } |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 66 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 67 | @Before |
laurentlb | dd612a8 | 2018-10-16 19:42:48 -0700 | [diff] [blame] | 68 | public final void preparePackageLoading() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 69 | Path alternativeRoot = scratch.dir("/root_2"); |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 70 | PackageOptions packageOptions = Options.getDefaults(PackageOptions.class); |
Googler | 9197839 | 2023-03-16 12:41:23 -0700 | [diff] [blame] | 71 | packageOptions.defaultVisibility = RuleVisibility.PUBLIC; |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 72 | packageOptions.showLoadingProgress = true; |
| 73 | packageOptions.globbingThreads = 7; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 74 | getSkyframeExecutor() |
| 75 | .preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 76 | new PathPackageLocator( |
| 77 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 78 | ImmutableList.of(Root.fromPath(rootDirectory), Root.fromPath(alternativeRoot)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 79 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 80 | packageOptions, |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 81 | Options.getDefaults(BuildLanguageOptions.class), |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 82 | UUID.randomUUID(), |
Googler | b3da9fb | 2022-10-05 07:43:12 -0700 | [diff] [blame] | 83 | ImmutableMap.of(), |
Googler | 4e22092 | 2023-01-12 06:33:22 -0800 | [diff] [blame] | 84 | QuiescingExecutorsImpl.forTesting(), |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 85 | new TimestampGranularityMonitor(BlazeClock.instance())); |
Googler | b3da9fb | 2022-10-05 07:43:12 -0700 | [diff] [blame] | 86 | skyframeExecutor.setActionEnv(ImmutableMap.of()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 89 | @Test |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 90 | public void testBzlLoadLabels() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 91 | scratch.file("pkg1/BUILD"); |
| 92 | scratch.file("pkg1/ext.bzl"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 93 | checkSuccessfulLookup("//pkg1:ext.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 94 | |
| 95 | scratch.file("pkg2/BUILD"); |
| 96 | scratch.file("pkg2/dir/ext.bzl"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 97 | checkSuccessfulLookup("//pkg2:dir/ext.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 98 | |
| 99 | scratch.file("dir/pkg3/BUILD"); |
| 100 | scratch.file("dir/pkg3/dir/ext.bzl"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 101 | checkSuccessfulLookup("//dir/pkg3:dir/ext.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 104 | @Test |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 105 | public void testBzlLoadLabelsAlternativeRoot() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 106 | scratch.file("/root_2/pkg4/BUILD"); |
| 107 | scratch.file("/root_2/pkg4/ext.bzl"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 108 | checkSuccessfulLookup("//pkg4:ext.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 111 | @Test |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 112 | public void testBzlLoadLabelsMultipleBuildFiles() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 113 | scratch.file("dir1/BUILD"); |
| 114 | scratch.file("dir1/dir2/BUILD"); |
| 115 | scratch.file("dir1/dir2/ext.bzl"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 116 | checkSuccessfulLookup("//dir1/dir2:ext.bzl"); |
| 117 | } |
| 118 | |
| 119 | @Test |
gregce | 1cd84ec | 2020-04-09 15:45:19 -0700 | [diff] [blame] | 120 | public void testLoadFromStarlarkFileInRemoteRepo() throws Exception { |
laurentlb | dd612a8 | 2018-10-16 19:42:48 -0700 | [diff] [blame] | 121 | scratch.overwriteFile( |
| 122 | "WORKSPACE", |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 123 | "local_repository(", |
| 124 | " name = 'a_remote_repo',", |
| 125 | " path = '/a_remote_repo'", |
| 126 | ")"); |
John Cater | e684392 | 2017-04-20 16:10:11 +0200 | [diff] [blame] | 127 | scratch.file("/a_remote_repo/WORKSPACE"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 128 | scratch.file("/a_remote_repo/remote_pkg/BUILD"); |
laurentlb | dd612a8 | 2018-10-16 19:42:48 -0700 | [diff] [blame] | 129 | scratch.file("/a_remote_repo/remote_pkg/ext1.bzl", "load(':ext2.bzl', 'CONST')"); |
| 130 | scratch.file("/a_remote_repo/remote_pkg/ext2.bzl", "CONST = 17"); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 131 | checkSuccessfulLookup("@a_remote_repo//remote_pkg:ext1.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 134 | @Test |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 135 | public void testLoadRelativeLabel() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 136 | scratch.file("pkg/BUILD"); |
| 137 | scratch.file("pkg/ext1.bzl", "a = 1"); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 138 | scratch.file("pkg/ext2.bzl", "load(':ext1.bzl', 'a')"); |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 139 | checkSuccessfulLookup("//pkg:ext2.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 142 | @Test |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 143 | public void testLoadAbsoluteLabel() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 144 | scratch.file("pkg2/BUILD"); |
| 145 | scratch.file("pkg3/BUILD"); |
| 146 | scratch.file("pkg2/ext.bzl", "b = 1"); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 147 | scratch.file("pkg3/ext.bzl", "load('//pkg2:ext.bzl', 'b')"); |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 148 | checkSuccessfulLookup("//pkg3:ext.bzl"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 151 | @Test |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 152 | public void testLoadFromSameAbsoluteLabelTwice() throws Exception { |
John Field | 81e093e | 2015-11-13 21:25:41 +0000 | [diff] [blame] | 153 | scratch.file("pkg1/BUILD"); |
| 154 | scratch.file("pkg2/BUILD"); |
| 155 | scratch.file("pkg1/ext.bzl", "a = 1", "b = 2"); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 156 | scratch.file("pkg2/ext.bzl", "load('//pkg1:ext.bzl', 'a')", "load('//pkg1:ext.bzl', 'b')"); |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 157 | checkSuccessfulLookup("//pkg2:ext.bzl"); |
John Field | 81e093e | 2015-11-13 21:25:41 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 160 | @Test |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 161 | public void testLoadFromSameRelativeLabelTwice() throws Exception { |
John Field | 81e093e | 2015-11-13 21:25:41 +0000 | [diff] [blame] | 162 | scratch.file("pkg/BUILD"); |
| 163 | scratch.file("pkg/ext1.bzl", "a = 1", "b = 2"); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 164 | scratch.file("pkg/ext2.bzl", "load(':ext1.bzl', 'a')", "load(':ext1.bzl', 'b')"); |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 165 | checkSuccessfulLookup("//pkg:ext2.bzl"); |
John Field | 3b58a1c | 2015-11-17 21:21:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 168 | @Test |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 169 | public void testLoadFromRelativeLabelInSubdir() throws Exception { |
John Field | 3b58a1c | 2015-11-17 21:21:34 +0000 | [diff] [blame] | 170 | scratch.file("pkg/BUILD"); |
| 171 | scratch.file("pkg/subdir/ext1.bzl", "a = 1"); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 172 | scratch.file("pkg/subdir/ext2.bzl", "load(':subdir/ext1.bzl', 'a')"); |
nharmata | 11642e9 | 2018-10-09 19:26:14 -0700 | [diff] [blame] | 173 | checkSuccessfulLookup("//pkg:subdir/ext2.bzl"); |
John Field | 81e093e | 2015-11-13 21:25:41 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Googler | a0cd355 | 2023-05-01 13:43:28 -0700 | [diff] [blame] | 176 | @Test |
| 177 | public void testLoadBadExtension_sclDisabled() throws Exception { |
| 178 | setBuildLanguageOptions("--experimental_enable_scl_dialect=false"); |
| 179 | |
| 180 | scratch.file("pkg/BUILD"); |
| 181 | scratch.file("pkg/ext.bzl", "load(':foo.garbage', 'a')"); |
| 182 | reporter.removeHandler(failFastHandler); |
| 183 | checkFailingLookup("//pkg:ext.bzl", "has invalid load statements"); |
| 184 | assertContainsEvent("The label must reference a file with extension \".bzl\""); |
| 185 | assertDoesNotContainEvent(".scl"); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testLoadBadExtension_sclEnabled() throws Exception { |
| 190 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 191 | |
| 192 | scratch.file("pkg/BUILD"); |
| 193 | scratch.file("pkg/ext.bzl", "load(':foo.garbage', 'a')"); |
| 194 | reporter.removeHandler(failFastHandler); |
| 195 | checkFailingLookup("//pkg:ext.bzl", "has invalid load statements"); |
| 196 | assertContainsEvent("The label must reference a file with extension \".bzl\" or \".scl\""); |
| 197 | } |
| 198 | |
| 199 | @Test |
| 200 | public void testLoadingSclRequiresExperimentalFlag() throws Exception { |
| 201 | setBuildLanguageOptions("--experimental_enable_scl_dialect=false"); |
| 202 | |
| 203 | scratch.file("pkg/BUILD"); |
| 204 | scratch.file("pkg/ext.scl"); |
| 205 | reporter.removeHandler(failFastHandler); |
| 206 | checkFailingLookup( |
| 207 | "//pkg:ext.scl", "loading .scl files requires setting --experimental_enable_scl_dialect"); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testCanLoadScl() throws Exception { |
| 212 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 213 | |
| 214 | scratch.file("pkg/BUILD"); |
| 215 | scratch.file("pkg/ext.scl"); |
| 216 | checkSuccessfulLookup("//pkg:ext.scl"); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testCanLoadSclFromBzlAndScl() throws Exception { |
| 221 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 222 | |
| 223 | scratch.file("pkg/BUILD"); |
| 224 | scratch.file("pkg/ext1.scl", "a = 1"); |
| 225 | // Can use relative load label syntax from ext2a.bzl, but not from ext2b.scl. |
| 226 | scratch.file("pkg/ext2a.bzl", "load(':ext1.scl', 'a')"); |
| 227 | scratch.file("pkg/ext2b.scl", "load('//pkg:ext1.scl', 'a')"); |
| 228 | |
| 229 | checkSuccessfulLookup("//pkg:ext2a.bzl"); |
| 230 | checkSuccessfulLookup("//pkg:ext2b.scl"); |
| 231 | } |
| 232 | |
| 233 | @Test |
| 234 | public void testSclCannotLoadNonSclFiles() throws Exception { |
| 235 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 236 | |
| 237 | scratch.file("pkg/BUILD"); |
| 238 | scratch.file("pkg/ext1a.bzl", "a = 1"); |
| 239 | scratch.file("pkg/ext1a.garbage", "a = 1"); |
| 240 | // Cannot use relative label. |
| 241 | scratch.file("pkg/ext2a.scl", "load('//pkg:ext1a.bzl', 'a')"); |
| 242 | scratch.file("pkg/ext2b.scl", "load('//pkg:ext1b.garbage', 'a')"); |
| 243 | |
| 244 | reporter.removeHandler(failFastHandler); |
| 245 | checkFailingLookup("//pkg:ext2a.scl", "has invalid load statements"); |
| 246 | assertContainsEvent( |
| 247 | "The label must reference a file with extension \".scl\" (.scl files cannot load .bzl" |
| 248 | + " files)"); |
| 249 | eventCollector.clear(); |
| 250 | checkFailingLookup("//pkg:ext2b.scl", "has invalid load statements"); |
| 251 | assertContainsEvent("The label must reference a file with extension \".scl\""); |
| 252 | assertDoesNotContainEvent(".bzl"); |
| 253 | } |
| 254 | |
| 255 | @Test |
| 256 | public void testSclCanOnlyLoadLabelsRelativeToDefaultRepoRoot() throws Exception { |
| 257 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 258 | |
| 259 | scratch.file("pkg/BUILD"); |
| 260 | scratch.file("pkg/ext1.scl", "load(':foo.scl', 'a')"); |
| 261 | scratch.file("pkg/ext2.scl", "load('@repo//:foo.scl', 'a')"); |
| 262 | |
| 263 | reporter.removeHandler(failFastHandler); |
| 264 | checkFailingLookup("//pkg:ext1.scl", "has invalid load statements"); |
| 265 | assertContainsEvent("in .scl files, load labels must begin with \"//\""); |
| 266 | eventCollector.clear(); |
| 267 | checkFailingLookup("//pkg:ext2.scl", "has invalid load statements"); |
| 268 | assertContainsEvent("in .scl files, load labels must begin with \"//\""); |
| 269 | } |
| 270 | |
Googler | a67200c | 2023-05-22 12:16:29 -0700 | [diff] [blame] | 271 | @Test |
| 272 | public void testSclSupportsStructAndVisibility() throws Exception { |
| 273 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 274 | |
| 275 | scratch.file("pkg/BUILD"); |
| 276 | scratch.file( |
| 277 | "pkg/ext1.scl", // |
| 278 | "visibility('private')", |
| 279 | "a = struct()"); |
| 280 | scratch.file( |
| 281 | "pkg/ext2.scl", // |
| 282 | "load('//pkg:ext1.scl', 'a')"); |
| 283 | scratch.file("pkg2/BUILD"); |
| 284 | scratch.file( |
| 285 | "pkg2/ext3.scl", // |
| 286 | "load('//pkg:ext1.scl', 'a')"); |
| 287 | |
| 288 | checkSuccessfulLookup("//pkg:ext2.scl"); |
| 289 | reporter.removeHandler(failFastHandler); |
| 290 | checkFailingLookup( |
| 291 | "//pkg2:ext3.scl", "module //pkg2:ext3.scl contains .bzl load visibility violations"); |
| 292 | } |
| 293 | |
| 294 | @Test |
| 295 | public void testSclDoesNotSupportOtherBazelSymbols() throws Exception { |
| 296 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 297 | |
| 298 | scratch.file("pkg/BUILD"); |
| 299 | scratch.file( |
| 300 | "pkg/ext.scl", // |
| 301 | "a = depset([])"); |
| 302 | |
| 303 | reporter.removeHandler(failFastHandler); |
| 304 | checkFailingLookup("//pkg:ext.scl", "compilation of module 'pkg/ext.scl' failed"); |
| 305 | assertContainsEvent("name 'depset' is not defined"); |
| 306 | } |
| 307 | |
Googler | 0099454 | 2023-05-23 06:42:51 -0700 | [diff] [blame] | 308 | @Test |
| 309 | public void testSclDisallowsNonAsciiStringLiterals() throws Exception { |
| 310 | setBuildLanguageOptions("--experimental_enable_scl_dialect=true"); |
| 311 | |
| 312 | scratch.file("pkg/BUILD"); |
| 313 | scratch.file( |
| 314 | "pkg/ext1.bzl", // |
| 315 | "'x\377z'"); // xÿz |
| 316 | scratch.file( |
| 317 | "pkg/ext2.scl", // |
| 318 | "'x\377z'"); |
| 319 | |
| 320 | checkSuccessfulLookup("//pkg:ext1.bzl"); |
| 321 | reporter.removeHandler(failFastHandler); |
| 322 | checkFailingLookup("//pkg:ext2.scl", "compilation of module 'pkg/ext2.scl' failed"); |
| 323 | assertContainsEvent("string literal contains non-ASCII character"); |
| 324 | } |
| 325 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 326 | private EvaluationResult<BzlLoadValue> get(SkyKey skyKey) throws Exception { |
| 327 | EvaluationResult<BzlLoadValue> result = |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 328 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 329 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 330 | if (result.hasError()) { |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 331 | fail(result.getError(skyKey).getException().getMessage()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 332 | } |
| 333 | return result; |
| 334 | } |
| 335 | |
ajurkowski | e298291 | 2020-04-09 10:32:08 -0700 | [diff] [blame] | 336 | private static SkyKey key(String label) { |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 337 | return BzlLoadValue.keyForBuild(Label.parseCanonicalUnchecked(label)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 340 | /** Loads a .bzl with the given label and asserts success. */ |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 341 | private void checkSuccessfulLookup(String label) throws Exception { |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 342 | SkyKey skyKey = key(label); |
| 343 | EvaluationResult<BzlLoadValue> result = get(skyKey); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 344 | // Ensure that the file has been processed by checking its Module for the label field. |
adonovan | 39e42d1 | 2020-07-09 09:16:58 -0700 | [diff] [blame] | 345 | assertThat(label) |
| 346 | .isEqualTo(BazelModuleContext.of(result.get(skyKey).getModule()).label().toString()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 349 | /* Loads a .bzl with the given label and asserts BzlLoadFailedException with the given message. */ |
| 350 | private void checkFailingLookup(String label, String expectedMessage) |
| 351 | throws InterruptedException { |
| 352 | SkyKey skyKey = key(label); |
| 353 | EvaluationResult<BzlLoadValue> result = |
| 354 | SkyframeExecutorTestUtils.evaluate( |
| 355 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
| 356 | assertThat(result.hasError()).isTrue(); |
| 357 | assertThatEvaluationResult(result) |
| 358 | .hasErrorEntryForKeyThat(skyKey) |
| 359 | .hasExceptionThat() |
| 360 | .isInstanceOf(BzlLoadFailedException.class); |
| 361 | assertThatEvaluationResult(result) |
| 362 | .hasErrorEntryForKeyThat(skyKey) |
| 363 | .hasExceptionThat() |
| 364 | .hasMessageThat() |
| 365 | .contains(expectedMessage); |
| 366 | } |
| 367 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 368 | @Test |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 369 | public void testBzlLoadNoBuildFile() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 370 | scratch.file("pkg/ext.bzl", ""); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 371 | SkyKey skyKey = key("//pkg:ext.bzl"); |
| 372 | EvaluationResult<BzlLoadValue> result = |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 373 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 374 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 375 | assertThat(result.hasError()).isTrue(); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 376 | ErrorInfo errorInfo = result.getError(skyKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 377 | String errorMessage = errorInfo.getException().getMessage(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 378 | assertThat(errorMessage) |
laurentlb | 3f791e76 | 2019-03-26 08:01:28 -0700 | [diff] [blame] | 379 | .contains( |
| 380 | "Every .bzl file must have a corresponding package, but '//pkg:ext.bzl' does not"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 381 | } |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 382 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 383 | @Test |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 384 | public void testBzlLoadNoBuildFileForLoad() throws Exception { |
John Field | 110b065 | 2015-11-13 21:56:42 +0000 | [diff] [blame] | 385 | scratch.file("pkg2/BUILD"); |
| 386 | scratch.file("pkg1/ext.bzl", "a = 1"); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 387 | scratch.file("pkg2/ext.bzl", "load('//pkg1:ext.bzl', 'a')"); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 388 | SkyKey skyKey = key("//pkg:ext.bzl"); |
| 389 | EvaluationResult<BzlLoadValue> result = |
John Field | 110b065 | 2015-11-13 21:56:42 +0000 | [diff] [blame] | 390 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 391 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 392 | assertThat(result.hasError()).isTrue(); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 393 | ErrorInfo errorInfo = result.getError(skyKey); |
John Field | 110b065 | 2015-11-13 21:56:42 +0000 | [diff] [blame] | 394 | String errorMessage = errorInfo.getException().getMessage(); |
laurentlb | 3f791e76 | 2019-03-26 08:01:28 -0700 | [diff] [blame] | 395 | assertThat(errorMessage).contains("Every .bzl file must have a corresponding package"); |
John Field | 110b065 | 2015-11-13 21:56:42 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 398 | @Test |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 399 | public void testBzlLoadFilenameWithControlChars() throws Exception { |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 400 | scratch.file("pkg/BUILD", ""); |
laurentlb | 7cf1c69 | 2017-12-04 05:44:54 -0800 | [diff] [blame] | 401 | scratch.file("pkg/ext.bzl", "load('//pkg:oops\u0000.bzl', 'a')"); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 402 | SkyKey skyKey = key("//pkg:ext.bzl"); |
jcater | 83130f4 | 2019-04-30 14:29:28 -0700 | [diff] [blame] | 403 | AssertionError e = |
| 404 | assertThrows( |
| 405 | AssertionError.class, |
jcater | c8b0188 | 2019-05-03 05:51:05 -0700 | [diff] [blame] | 406 | () -> |
| 407 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 408 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter)); |
jcater | 83130f4 | 2019-04-30 14:29:28 -0700 | [diff] [blame] | 409 | String errorMessage = e.getMessage(); |
| 410 | assertThat(errorMessage) |
| 411 | .contains( |
| 412 | "invalid target name 'oops<?>.bzl': " |
| 413 | + "target names may not contain non-printable characters: '\\x00'"); |
John Field | a97e17f | 2015-11-13 02:19:52 +0000 | [diff] [blame] | 414 | } |
John Field | bcb1bea | 2016-01-16 19:05:56 +0000 | [diff] [blame] | 415 | |
| 416 | @Test |
Damien Martin-Guillerez | 6f07b79 | 2016-02-10 13:29:46 +0000 | [diff] [blame] | 417 | public void testLoadFromExternalRepoInWorkspaceFileAllowed() throws Exception { |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 418 | Path p = |
| 419 | scratch.overwriteFile( |
| 420 | "WORKSPACE", |
| 421 | "local_repository(", |
| 422 | " name = 'a_remote_repo',", |
| 423 | " path = '/a_remote_repo'", |
| 424 | ")"); |
John Cater | e684392 | 2017-04-20 16:10:11 +0200 | [diff] [blame] | 425 | scratch.file("/a_remote_repo/WORKSPACE"); |
John Field | bcb1bea | 2016-01-16 19:05:56 +0000 | [diff] [blame] | 426 | scratch.file("/a_remote_repo/remote_pkg/BUILD"); |
laurentlb | dd612a8 | 2018-10-16 19:42:48 -0700 | [diff] [blame] | 427 | scratch.file("/a_remote_repo/remote_pkg/ext.bzl", "CONST = 17"); |
John Field | bcb1bea | 2016-01-16 19:05:56 +0000 | [diff] [blame] | 428 | |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 429 | RootedPath rootedPath = |
| 430 | RootedPath.toRootedPath( |
| 431 | Root.fromPath(p.getParentDirectory()), PathFragment.create("WORKSPACE")); |
| 432 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 433 | SkyKey skyKey = |
brandjon | b28a76b | 2020-05-26 12:53:22 -0700 | [diff] [blame] | 434 | BzlLoadValue.keyForWorkspace( |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 435 | Label.parseCanonicalUnchecked("@a_remote_repo//remote_pkg:ext.bzl"), |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 436 | /* inWorkspace= */ |
| 437 | /* workspaceChunk= */ 0, |
| 438 | rootedPath); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 439 | EvaluationResult<BzlLoadValue> result = |
John Field | bcb1bea | 2016-01-16 19:05:56 +0000 | [diff] [blame] | 440 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 441 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
John Field | bcb1bea | 2016-01-16 19:05:56 +0000 | [diff] [blame] | 442 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 443 | assertThat(result.hasError()).isFalse(); |
John Field | bcb1bea | 2016-01-16 19:05:56 +0000 | [diff] [blame] | 444 | } |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 445 | |
| 446 | @Test |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 447 | public void testLoadFromSubdirInSamePackageIsOk() throws Exception { |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 448 | scratch.file("a/BUILD"); |
| 449 | scratch.file("a/a.bzl", "load('//a:b/b.bzl', 'b')"); |
| 450 | scratch.file("a/b/b.bzl", "b = 42"); |
| 451 | |
| 452 | checkSuccessfulLookup("//a:a.bzl"); |
| 453 | } |
| 454 | |
| 455 | @Test |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 456 | public void testLoadMustRespectPackageBoundary_ofSubpkg() throws Exception { |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 457 | scratch.file("a/BUILD"); |
| 458 | scratch.file("a/a.bzl", "load('//a:b/b.bzl', 'b')"); |
| 459 | scratch.file("a/b/BUILD", ""); |
| 460 | scratch.file("a/b/b.bzl", "b = 42"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 461 | checkFailingLookup( |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 462 | "//a:a.bzl", |
| 463 | "Label '//a:b/b.bzl' is invalid because 'a/b' is a subpackage; perhaps you meant to" |
| 464 | + " put the colon here: '//a/b:b.bzl'?"); |
| 465 | } |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 466 | |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 467 | @Test |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 468 | public void testLoadMustRespectPackageBoundary_ofSubpkg_relative() throws Exception { |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 469 | scratch.file("a/BUILD"); |
| 470 | scratch.file("a/a.bzl", "load('b/b.bzl', 'b')"); |
| 471 | scratch.file("a/b/BUILD", ""); |
| 472 | scratch.file("a/b/b.bzl", "b = 42"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 473 | checkFailingLookup( |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 474 | "//a:a.bzl", |
| 475 | "Label '//a:b/b.bzl' is invalid because 'a/b' is a subpackage; perhaps you meant to" |
| 476 | + " put the colon here: '//a/b:b.bzl'?"); |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | @Test |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 480 | public void testLoadMustRespectPackageBoundary_ofIndirectSubpkg() throws Exception { |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 481 | scratch.file("a/BUILD"); |
| 482 | scratch.file("a/a.bzl", "load('//a/b:c/c.bzl', 'c')"); |
| 483 | scratch.file("a/b/BUILD", ""); |
| 484 | scratch.file("a/b/c/BUILD", ""); |
| 485 | scratch.file("a/b/c/c.bzl", "c = 42"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 486 | checkFailingLookup( |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 487 | "//a:a.bzl", |
| 488 | "Label '//a/b:c/c.bzl' is invalid because 'a/b/c' is a subpackage; perhaps you meant" |
| 489 | + " to put the colon here: '//a/b/c:c.bzl'?"); |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Test |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 493 | public void testLoadMustRespectPackageBoundary_ofParentPkg() throws Exception { |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 494 | scratch.file("a/b/BUILD"); |
| 495 | scratch.file("a/b/b.bzl", "load('//a/c:c/c.bzl', 'c')"); |
| 496 | scratch.file("a/BUILD"); |
| 497 | scratch.file("a/c/c/c.bzl", "c = 42"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 498 | checkFailingLookup( |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 499 | "//a/b:b.bzl", |
| 500 | "Label '//a/c:c/c.bzl' is invalid because 'a/c' is not a package; perhaps you meant to " |
| 501 | + "put the colon here: '//a:c/c/c.bzl'?"); |
| 502 | } |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 503 | |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 504 | @Test |
| 505 | public void testBzlVisibility_disabledWithoutFlag() throws Exception { |
| 506 | setBuildLanguageOptions("--experimental_bzl_visibility=false"); |
| 507 | |
| 508 | scratch.file("a/BUILD"); |
| 509 | scratch.file( |
| 510 | "a/foo.bzl", // |
| 511 | "load(\"//b:bar.bzl\", \"x\")"); |
| 512 | scratch.file("b/BUILD"); |
| 513 | scratch.file( |
| 514 | "b/bar.bzl", // |
| 515 | "visibility(\"private\")", |
| 516 | "x = 1"); |
| 517 | |
| 518 | reporter.removeHandler(failFastHandler); |
| 519 | checkFailingLookup("//a:foo.bzl", "initialization of module 'b/bar.bzl' failed"); |
| 520 | assertContainsEvent("Use of `visibility()` requires --experimental_bzl_visibility"); |
| 521 | } |
| 522 | |
| 523 | @Test |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 524 | public void testBzlVisibility_publicExplicit() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 525 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 526 | |
| 527 | scratch.file("a/BUILD"); |
| 528 | scratch.file( |
| 529 | "a/foo.bzl", // |
| 530 | "load(\"//b:bar.bzl\", \"x\")"); |
| 531 | scratch.file("b/BUILD"); |
| 532 | scratch.file( |
| 533 | "b/bar.bzl", // |
| 534 | "visibility(\"public\")", |
| 535 | "x = 1"); |
| 536 | |
| 537 | checkSuccessfulLookup("//a:foo.bzl"); |
| 538 | assertNoEvents(); |
| 539 | } |
| 540 | |
| 541 | @Test |
| 542 | public void testBzlVisibility_publicImplicit() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 543 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 544 | |
| 545 | scratch.file("a/BUILD"); |
| 546 | scratch.file( |
| 547 | "a/foo.bzl", // |
| 548 | "load(\"//b:bar.bzl\", \"x\")"); |
| 549 | scratch.file("b/BUILD"); |
| 550 | scratch.file( |
| 551 | "b/bar.bzl", |
| 552 | // No visibility() declaration, defaults to public. |
| 553 | "x = 1"); |
| 554 | |
| 555 | checkSuccessfulLookup("//a:foo.bzl"); |
| 556 | assertNoEvents(); |
| 557 | } |
| 558 | |
| 559 | @Test |
| 560 | public void testBzlVisibility_privateSamePackage() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 561 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 562 | |
| 563 | scratch.file("a/BUILD"); |
| 564 | scratch.file( |
| 565 | "a/foo.bzl", // |
| 566 | "load(\"//a:bar.bzl\", \"x\")"); |
| 567 | scratch.file( |
| 568 | "a/bar.bzl", // |
Googler | faea4a1 | 2022-08-24 07:39:57 -0700 | [diff] [blame] | 569 | "visibility(\"private\")", |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 570 | "x = 1"); |
| 571 | |
| 572 | checkSuccessfulLookup("//a:foo.bzl"); |
| 573 | assertNoEvents(); |
| 574 | } |
| 575 | |
| 576 | @Test |
| 577 | public void testBzlVisibility_privateDifferentPackage() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 578 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 579 | |
| 580 | scratch.file("a/BUILD"); |
| 581 | scratch.file( |
| 582 | "a/foo.bzl", // |
| 583 | "load(\"//b:bar.bzl\", \"x\")"); |
| 584 | scratch.file("b/BUILD"); |
| 585 | scratch.file( |
| 586 | "b/bar.bzl", // |
| 587 | "visibility(\"private\")", |
| 588 | "x = 1"); |
| 589 | |
| 590 | reporter.removeHandler(failFastHandler); |
| 591 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 592 | "//a:foo.bzl", "module //a:foo.bzl contains .bzl load visibility violations"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 593 | assertContainsEvent("Starlark file //b:bar.bzl is not visible for loading from package //a."); |
| 594 | } |
| 595 | |
| 596 | @Test |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 597 | public void testBzlVisibility_emptyListMeansPrivate() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 598 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 599 | |
| 600 | scratch.file("a/BUILD"); |
| 601 | scratch.file( |
| 602 | "a/foo.bzl", // |
| 603 | "load(\"//b:bar.bzl\", \"x\")"); |
| 604 | scratch.file("b/BUILD"); |
| 605 | scratch.file( |
| 606 | "b/bar.bzl", // |
| 607 | "visibility([])", |
| 608 | "x = 1"); |
| 609 | |
| 610 | reporter.removeHandler(failFastHandler); |
| 611 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 612 | "//a:foo.bzl", "module //a:foo.bzl contains .bzl load visibility violations"); |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 613 | assertContainsEvent("Starlark file //b:bar.bzl is not visible for loading from package //a."); |
| 614 | } |
| 615 | |
| 616 | @Test |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 617 | public void testBzlVisibility_publicListElement() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 618 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 619 | |
| 620 | scratch.file("a/BUILD"); |
| 621 | scratch.file( |
| 622 | "a/foo.bzl", // |
| 623 | "load(\"//b:bar.bzl\", \"x\")"); |
| 624 | scratch.file("b/BUILD"); |
| 625 | scratch.file( |
| 626 | "b/bar.bzl", // |
| 627 | // Tests "public" as a list item, and alongside other list items. |
| 628 | "visibility([\"public\", \"//c\"])", |
| 629 | "x = 1"); |
| 630 | |
| 631 | checkSuccessfulLookup("//a:foo.bzl"); |
| 632 | assertNoEvents(); |
| 633 | } |
| 634 | |
| 635 | @Test |
| 636 | public void testBzlVisibility_privateListElement() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 637 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 638 | |
| 639 | scratch.file("a1/BUILD"); |
| 640 | scratch.file( |
| 641 | "a1/foo.bzl", // |
| 642 | "load(\"//b:bar.bzl\", \"x\")"); |
| 643 | scratch.file("a2/BUILD"); |
| 644 | scratch.file( |
| 645 | "a2/foo.bzl", // |
| 646 | "load(\"//b:bar.bzl\", \"x\")"); |
| 647 | scratch.file("b/BUILD"); |
| 648 | scratch.file( |
| 649 | "b/bar.bzl", // |
| 650 | // Tests "private" as a list item, and alongside other list items. |
| 651 | "visibility([\"private\", \"//a1\"])", |
| 652 | "x = 1"); |
| 653 | |
| 654 | checkSuccessfulLookup("//a1:foo.bzl"); |
| 655 | assertNoEvents(); |
| 656 | reporter.removeHandler(failFastHandler); |
| 657 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 658 | "//a2:foo.bzl", "module //a2:foo.bzl contains .bzl load visibility violations"); |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 659 | assertContainsEvent("Starlark file //b:bar.bzl is not visible for loading from package //a2."); |
| 660 | } |
| 661 | |
| 662 | @Test |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 663 | public void testBzlVisibility_failureInDependency() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 664 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 665 | |
| 666 | scratch.file("a/BUILD"); |
| 667 | scratch.file( |
| 668 | "a/foo.bzl", // |
| 669 | "load(\"//b:bar.bzl\", \"x\")"); |
| 670 | scratch.file("b/BUILD"); |
| 671 | scratch.file( |
| 672 | "b/bar.bzl", // |
| 673 | "load(\"//c:baz.bzl\", \"y\")", |
| 674 | "visibility(\"public\")", |
| 675 | "x = y"); |
| 676 | scratch.file("c/BUILD"); |
| 677 | scratch.file( |
| 678 | "c/baz.bzl", // |
| 679 | "visibility(\"private\")", |
| 680 | "y = 1"); |
| 681 | |
| 682 | reporter.removeHandler(failFastHandler); |
| 683 | checkFailingLookup( |
| 684 | "//a:foo.bzl", |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 685 | "at /workspace/a/foo.bzl:1:6: module //b:bar.bzl contains .bzl load visibility violations"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 686 | assertContainsEvent("Starlark file //c:baz.bzl is not visible for loading from package //b."); |
| 687 | } |
| 688 | |
| 689 | @Test |
Googler | 32d56b6 | 2022-10-06 12:06:11 -0700 | [diff] [blame] | 690 | public void testBzlVisibility_cannotBeSetInFunction() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 691 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 692 | |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 693 | scratch.file("a/BUILD"); |
| 694 | scratch.file( |
| 695 | "a/foo.bzl", // |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 696 | "def helper():", |
Googler | 32d56b6 | 2022-10-06 12:06:11 -0700 | [diff] [blame] | 697 | " visibility(\"public\")", |
| 698 | "helper()"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 699 | |
| 700 | reporter.removeHandler(failFastHandler); |
Googler | 32d56b6 | 2022-10-06 12:06:11 -0700 | [diff] [blame] | 701 | checkFailingLookup("//a:foo.bzl", "initialization of module 'a/foo.bzl' failed"); |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 702 | assertContainsEvent("load visibility may only be set at the top level"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | @Test |
| 706 | public void testBzlVisibility_cannotBeSetTwice() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 707 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 24a4941 | 2022-07-01 13:01:25 -0700 | [diff] [blame] | 708 | |
| 709 | scratch.file("a/BUILD"); |
| 710 | scratch.file( |
| 711 | "a/foo.bzl", // |
| 712 | "visibility(\"public\")", |
| 713 | "visibility(\"public\")"); |
| 714 | |
| 715 | reporter.removeHandler(failFastHandler); |
| 716 | checkFailingLookup("//a:foo.bzl", "initialization of module 'a/foo.bzl' failed"); |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 717 | assertContainsEvent("load visibility may not be set more than once"); |
nharmata | d86b509 | 2018-10-16 15:50:21 -0700 | [diff] [blame] | 718 | } |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 719 | |
| 720 | @Test |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 721 | public void testBzlVisibility_enumeratedPackages() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 722 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 723 | |
| 724 | scratch.file("a1/BUILD"); |
| 725 | scratch.file( |
| 726 | "a1/foo1.bzl", // |
| 727 | "load(\"//b:bar.bzl\", \"x\")"); |
| 728 | scratch.file("a2/BUILD"); |
| 729 | scratch.file( |
| 730 | "a2/foo2.bzl", // |
| 731 | "load(\"//b:bar.bzl\", \"x\")"); |
| 732 | scratch.file("b/BUILD"); |
| 733 | scratch.file( |
| 734 | "b/bar.bzl", // |
| 735 | "visibility([\"//a1\"])", |
| 736 | "x = 1"); |
| 737 | |
| 738 | checkSuccessfulLookup("//a1:foo1.bzl"); |
| 739 | assertNoEvents(); |
| 740 | |
| 741 | reporter.removeHandler(failFastHandler); |
| 742 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 743 | "//a2:foo2.bzl", "module //a2:foo2.bzl contains .bzl load visibility violations"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 744 | assertContainsEvent("Starlark file //b:bar.bzl is not visible for loading from package //a2."); |
| 745 | } |
| 746 | |
| 747 | @Test |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 748 | public void testBzlVisibility_singleEnumeratedPackageAsString() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 749 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 750 | |
| 751 | scratch.file("a1/BUILD"); |
| 752 | scratch.file( |
| 753 | "a1/foo1.bzl", // |
| 754 | "load(\"//b:bar.bzl\", \"x\")"); |
| 755 | scratch.file("a2/BUILD"); |
| 756 | scratch.file( |
| 757 | "a2/foo2.bzl", // |
| 758 | "load(\"//b:bar.bzl\", \"x\")"); |
| 759 | scratch.file("b/BUILD"); |
| 760 | scratch.file( |
| 761 | "b/bar.bzl", // |
| 762 | // Note: "//a1", not ["//a1"] |
| 763 | "visibility(\"//a1\")", |
| 764 | "x = 1"); |
| 765 | |
| 766 | checkSuccessfulLookup("//a1:foo1.bzl"); |
| 767 | assertNoEvents(); |
| 768 | |
| 769 | reporter.removeHandler(failFastHandler); |
| 770 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 771 | "//a2:foo2.bzl", "module //a2:foo2.bzl contains .bzl load visibility violations"); |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 772 | assertContainsEvent("Starlark file //b:bar.bzl is not visible for loading from package //a2."); |
| 773 | } |
| 774 | |
| 775 | @Test |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 776 | public void testBzlVisibility_enumeratedPackagesMultipleRepos() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 777 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 778 | |
| 779 | // @repo//pkg:foo1.bzl and @//pkg:foo2.bzl both try to access @repo//lib:bar.bzl. Test that when |
| 780 | // bar.bzl declares a visibility allowing "//pkg", it means @repo//pkg and *not* @//pkg. |
| 781 | scratch.overwriteFile( |
| 782 | "WORKSPACE", // |
| 783 | "local_repository(", |
| 784 | " name = 'repo',", |
| 785 | " path = 'repo'", |
| 786 | ")"); |
| 787 | scratch.file("repo/WORKSPACE"); |
| 788 | scratch.file("repo/pkg/BUILD"); |
| 789 | scratch.file( |
| 790 | "repo/pkg/foo1.bzl", // |
| 791 | "load(\"//lib:bar.bzl\", \"x\")"); |
| 792 | scratch.file("repo/lib/BUILD"); |
| 793 | scratch.file( |
| 794 | "repo/lib/bar.bzl", // |
| 795 | "visibility([\"//pkg\"])", |
| 796 | "x = 1"); |
| 797 | scratch.file("pkg/BUILD"); |
| 798 | scratch.file( |
| 799 | "pkg/foo2.bzl", // |
| 800 | "load(\"@repo//lib:bar.bzl\", \"x\")"); |
| 801 | |
| 802 | checkSuccessfulLookup("@repo//pkg:foo1.bzl"); |
| 803 | assertNoEvents(); |
| 804 | |
| 805 | reporter.removeHandler(failFastHandler); |
| 806 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 807 | "//pkg:foo2.bzl", "module //pkg:foo2.bzl contains .bzl load visibility violations"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 808 | assertContainsEvent( |
| 809 | "Starlark file @repo//lib:bar.bzl is not visible for loading from package //pkg."); |
| 810 | } |
| 811 | |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 812 | // TODO(#16365): This test case can be deleted once --incompatible_package_group_has_public_syntax |
| 813 | // is deleted (not just flipped). |
| 814 | @Test |
| 815 | public void testBzlVisibility_canUsePublicPrivate_regardlessOfFlag() throws Exception { |
| 816 | setBuildLanguageOptions( |
| 817 | "--experimental_bzl_visibility=true", |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 818 | // Test that we can use "public" and "private" visibility for .bzl files even when the |
| 819 | // incompatible flag is disabled. |
| 820 | "--incompatible_package_group_has_public_syntax=false"); |
| 821 | |
| 822 | scratch.file("a/BUILD"); |
| 823 | scratch.file( |
| 824 | "a/foo1.bzl", // |
| 825 | "visibility(\"public\")"); |
| 826 | scratch.file( |
| 827 | "a/foo2.bzl", // |
| 828 | "visibility(\"private\")"); |
| 829 | |
| 830 | checkSuccessfulLookup("//a:foo1.bzl"); |
| 831 | checkSuccessfulLookup("//a:foo2.bzl"); |
| 832 | assertNoEvents(); |
| 833 | } |
| 834 | |
| 835 | // TODO(#16324): Once --incompatible_fix_package_group_reporoot_syntax is deleted (not just |
| 836 | // flipped), this test case will be redundant with tests for //... in PackageGroupTest. At that |
| 837 | // point we'll just delete this test case. |
| 838 | @Test |
| 839 | public void testBzlVisibility_repoRootSubpackagesIsNotPublic_regardlessOfFlag() throws Exception { |
| 840 | setBuildLanguageOptions( |
| 841 | "--experimental_bzl_visibility=true", |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 842 | // Test that we get the fixed behavior even when the incompatible flag is disabled. |
| 843 | "--incompatible_fix_package_group_reporoot_syntax=false"); |
| 844 | |
| 845 | scratch.overwriteFile( |
| 846 | "WORKSPACE", // |
| 847 | "local_repository(", |
| 848 | " name = 'repo',", |
| 849 | " path = 'repo'", |
| 850 | ")"); |
| 851 | scratch.file("repo/WORKSPACE"); |
| 852 | scratch.file("repo/a/BUILD"); |
| 853 | scratch.file( |
| 854 | "repo/a/foo.bzl", // |
| 855 | "load(\"@//b:bar.bzl\", \"x\")"); |
| 856 | scratch.file("b/BUILD"); |
| 857 | scratch.file( |
| 858 | "b/bar.bzl", // |
| 859 | "visibility([\"//...\"])", |
| 860 | "x = 1"); |
| 861 | |
| 862 | reporter.removeHandler(failFastHandler); |
| 863 | checkFailingLookup( |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 864 | "@repo//a:foo.bzl", "module @repo//a:foo.bzl contains .bzl load visibility violations"); |
Googler | 1473988 | 2022-10-06 10:22:52 -0700 | [diff] [blame] | 865 | assertContainsEvent( |
| 866 | "Starlark file //b:bar.bzl is not visible for loading from package @repo//a."); |
| 867 | } |
| 868 | |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 869 | @Test |
Googler | faea4a1 | 2022-08-24 07:39:57 -0700 | [diff] [blame] | 870 | public void testBzlVisibility_disallowsSubpackagesWithoutWildcard() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 871 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | faea4a1 | 2022-08-24 07:39:57 -0700 | [diff] [blame] | 872 | |
| 873 | scratch.file("a/BUILD"); |
| 874 | scratch.file( |
| 875 | "a/foo1.bzl", // |
| 876 | "load(\"//b:bar.bzl\", \"x\")"); |
| 877 | scratch.file("a/subpkg/BUILD"); |
| 878 | scratch.file( |
| 879 | "a/subpkg/foo2.bzl", // |
| 880 | "load(\"//b:bar.bzl\", \"x\")"); |
| 881 | scratch.file("b/BUILD"); |
| 882 | scratch.file( |
| 883 | "b/bar.bzl", // |
| 884 | "visibility([\"//a\"])", |
| 885 | "x = 1"); |
| 886 | |
| 887 | checkSuccessfulLookup("//a:foo1.bzl"); |
| 888 | assertNoEvents(); |
| 889 | |
| 890 | reporter.removeHandler(failFastHandler); |
| 891 | checkFailingLookup( |
| 892 | "//a/subpkg:foo2.bzl", |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 893 | "module //a/subpkg:foo2.bzl contains .bzl load visibility violations"); |
Googler | faea4a1 | 2022-08-24 07:39:57 -0700 | [diff] [blame] | 894 | assertContainsEvent( |
| 895 | "Starlark file //b:bar.bzl is not visible for loading from package //a/subpkg."); |
| 896 | } |
| 897 | |
| 898 | @Test |
| 899 | public void testBzlVisibility_allowsSubpackagesWithWildcard() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 900 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | faea4a1 | 2022-08-24 07:39:57 -0700 | [diff] [blame] | 901 | |
| 902 | scratch.file("a/BUILD"); |
| 903 | scratch.file( |
| 904 | "a/foo1.bzl", // |
| 905 | "load(\"//b:bar.bzl\", \"x\")"); |
| 906 | scratch.file("a/subpkg/BUILD"); |
| 907 | scratch.file( |
| 908 | "a/subpkg/foo2.bzl", // |
| 909 | "load(\"//b:bar.bzl\", \"x\")"); |
| 910 | scratch.file("b/BUILD"); |
| 911 | scratch.file( |
| 912 | "b/bar.bzl", // |
| 913 | "visibility([\"//a/...\"])", |
| 914 | "x = 1"); |
| 915 | |
| 916 | checkSuccessfulLookup("//a:foo1.bzl"); |
| 917 | assertNoEvents(); |
| 918 | |
| 919 | checkSuccessfulLookup("//a/subpkg:foo2.bzl"); |
| 920 | assertNoEvents(); |
| 921 | } |
| 922 | |
| 923 | @Test |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 924 | public void testBzlVisibility_invalid_badType() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 925 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 926 | |
| 927 | scratch.file("a/BUILD"); |
| 928 | scratch.file( |
| 929 | "a/foo.bzl", // |
| 930 | "visibility(123)"); |
| 931 | |
| 932 | reporter.removeHandler(failFastHandler); |
| 933 | checkFailingLookup("//a:foo.bzl", "initialization of module 'a/foo.bzl' failed"); |
Googler | 27f0f0f | 2022-10-21 15:04:57 -0700 | [diff] [blame] | 934 | assertContainsEvent("Invalid visibility: got 'int', want string or list of strings"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | @Test |
| 938 | public void testBzlVisibility_invalid_badElementType() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 939 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 940 | |
| 941 | scratch.file("a/BUILD"); |
| 942 | scratch.file( |
| 943 | "a/foo.bzl", // |
| 944 | "visibility([\"//a\", 123])"); |
| 945 | |
| 946 | reporter.removeHandler(failFastHandler); |
| 947 | checkFailingLookup("//a:foo.bzl", "initialization of module 'a/foo.bzl' failed"); |
Fabian Meumertzheim | d624ea8 | 2022-10-11 04:53:01 -0700 | [diff] [blame] | 948 | assertContainsEvent("at index 1 of visibility list, got element of type int, want string"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | @Test |
| 952 | public void testBzlVisibility_invalid_packageOutsideRepo() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 953 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 954 | |
| 955 | scratch.file("a/BUILD"); |
| 956 | scratch.file( |
| 957 | "a/foo.bzl", // |
| 958 | "visibility([\"@repo//b\"])"); |
| 959 | |
| 960 | reporter.removeHandler(failFastHandler); |
| 961 | checkFailingLookup("//a:foo.bzl", "initialization of module 'a/foo.bzl' failed"); |
Googler | faea4a1 | 2022-08-24 07:39:57 -0700 | [diff] [blame] | 962 | assertContainsEvent("invalid package name '@repo//b': must start with '//'"); |
Googler | 0152338 | 2022-07-01 18:08:41 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | @Test |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 966 | public void testBzlVisibility_invalid_negationNotSupported() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 967 | setBuildLanguageOptions("--experimental_bzl_visibility=true"); |
Googler | 535a0ce | 2022-10-06 11:33:12 -0700 | [diff] [blame] | 968 | |
| 969 | scratch.file("a/BUILD"); |
| 970 | scratch.file( |
| 971 | "a/foo.bzl", // |
| 972 | "visibility([\"-//a\"])"); |
| 973 | |
| 974 | reporter.removeHandler(failFastHandler); |
| 975 | checkFailingLookup("//a:foo.bzl", "initialization of module 'a/foo.bzl' failed"); |
| 976 | assertContainsEvent("Cannot use negative package patterns here"); |
| 977 | } |
| 978 | |
| 979 | @Test |
Googler | 8476104 | 2022-10-06 12:43:04 -0700 | [diff] [blame] | 980 | public void testBzlVisibility_errorsDemotedToWarningWhenBreakGlassFlagIsSet() throws Exception { |
Googler | e42e5c6 | 2022-10-18 17:48:39 -0700 | [diff] [blame] | 981 | setBuildLanguageOptions("--experimental_bzl_visibility=true", "--check_bzl_visibility=false"); |
Googler | 8476104 | 2022-10-06 12:43:04 -0700 | [diff] [blame] | 982 | |
| 983 | scratch.file("a/BUILD"); |
| 984 | scratch.file( |
| 985 | "a/foo.bzl", // |
| 986 | "load(\"//b:bar.bzl\", \"x\")"); |
| 987 | scratch.file("b/BUILD"); |
| 988 | scratch.file( |
| 989 | "b/bar.bzl", // |
| 990 | "visibility(\"private\")", |
| 991 | "x = 1"); |
| 992 | |
| 993 | checkSuccessfulLookup("//a:foo.bzl"); |
| 994 | assertContainsEvent("Starlark file //b:bar.bzl is not visible for loading from package //a."); |
| 995 | assertContainsEvent("Continuing because --nocheck_bzl_visibility is active"); |
| 996 | } |
| 997 | |
| 998 | @Test |
brandjon | 2491264 | 2020-05-27 11:28:45 -0700 | [diff] [blame] | 999 | public void testLoadFromNonExistentRepository_producesMeaningfulError() throws Exception { |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1000 | scratch.file("BUILD", "load(\"@repository//dir:file.bzl\", \"foo\")"); |
| 1001 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1002 | SkyKey skyKey = key("@repository//dir:file.bzl"); |
| 1003 | EvaluationResult<BzlLoadValue> result = |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1004 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1005 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1006 | assertThat(result.hasError()).isTrue(); |
| 1007 | assertThatEvaluationResult(result) |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1008 | .hasErrorEntryForKeyThat(skyKey) |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1009 | .hasExceptionThat() |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1010 | .isInstanceOf(BzlLoadFailedException.class); |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1011 | assertThatEvaluationResult(result) |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1012 | .hasErrorEntryForKeyThat(skyKey) |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1013 | .hasExceptionThat() |
| 1014 | .hasMessageThat() |
| 1015 | .contains( |
| 1016 | "Unable to find package for @repository//dir:file.bzl: The repository '@repository' " |
pcloudy | 87dbf7d | 2021-09-02 09:22:20 -0700 | [diff] [blame] | 1017 | + "could not be resolved: Repository '@repository' is not defined."); |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | @Test |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 1021 | public void testLoadBzlFileFromWorkspaceWithRemapping() throws Exception { |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 1022 | Path p = |
| 1023 | scratch.overwriteFile( |
| 1024 | "WORKSPACE", |
| 1025 | "local_repository(", |
| 1026 | " name = 'y',", |
| 1027 | " path = '/y'", |
| 1028 | ")", |
| 1029 | "local_repository(", |
| 1030 | " name = 'a',", |
| 1031 | " path = '/a',", |
| 1032 | " repo_mapping = {'@x' : '@y'}", |
| 1033 | ")", |
| 1034 | "load('@a//:a.bzl', 'a_symbol')"); |
| 1035 | |
| 1036 | scratch.file("/y/WORKSPACE"); |
| 1037 | scratch.file("/y/BUILD"); |
| 1038 | scratch.file("/y/y.bzl", "y_symbol = 5"); |
| 1039 | |
| 1040 | scratch.file("/a/WORKSPACE"); |
| 1041 | scratch.file("/a/BUILD"); |
| 1042 | scratch.file("/a/a.bzl", "load('@x//:y.bzl', 'y_symbol')", "a_symbol = y_symbol"); |
| 1043 | |
| 1044 | Root root = Root.fromPath(p.getParentDirectory()); |
| 1045 | RootedPath rootedPath = RootedPath.toRootedPath(root, PathFragment.create("WORKSPACE")); |
| 1046 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1047 | SkyKey skyKey = |
Googler | 08463dc | 2023-01-20 08:02:19 -0800 | [diff] [blame] | 1048 | BzlLoadValue.keyForWorkspace(Label.parseCanonicalUnchecked("@a//:a.bzl"), 1, rootedPath); |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 1049 | |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1050 | EvaluationResult<BzlLoadValue> result = |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 1051 | SkyframeExecutorTestUtils.evaluate( |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1052 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 1053 | |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 1054 | assertThat(result.get(skyKey).getModule().getGlobals()) |
| 1055 | .containsEntry("a_symbol", StarlarkInt.of(5)); |
dannark | 4e42c32 | 2018-11-08 19:32:04 -0800 | [diff] [blame] | 1056 | } |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1057 | |
| 1058 | @Test |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1059 | public void testLoadBzlFileFromBzlmod() throws Exception { |
Googler | d51144c | 2023-10-13 03:20:20 -0700 | [diff] [blame] | 1060 | setBuildLanguageOptions("--experimental_enable_scl_dialect"); |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1061 | scratch.overwriteFile("MODULE.bazel", "bazel_dep(name='foo',version='1.0')"); |
| 1062 | registry |
| 1063 | .addModule( |
| 1064 | createModuleKey("foo", "1.0"), |
| 1065 | "module(name='foo',version='1.0')", |
| 1066 | "bazel_dep(name='bar',version='2.0',repo_name='bar_alias')") |
| 1067 | .addModule(createModuleKey("bar", "2.0"), "module(name='bar',version='2.0')"); |
Googler | 7f9de9e | 2022-08-25 03:25:02 -0700 | [diff] [blame] | 1068 | Path fooDir = moduleRoot.getRelative("foo~1.0"); |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1069 | scratch.file(fooDir.getRelative("WORKSPACE").getPathString()); |
| 1070 | scratch.file(fooDir.getRelative("BUILD").getPathString()); |
| 1071 | scratch.file( |
| 1072 | fooDir.getRelative("test.bzl").getPathString(), |
Googler | a0cd355 | 2023-05-01 13:43:28 -0700 | [diff] [blame] | 1073 | // Also test that bzlmod .bzl files can load .scl files. |
| 1074 | "load('@bar_alias//:test.scl', 'haha')", |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1075 | "hoho = haha"); |
Googler | 7f9de9e | 2022-08-25 03:25:02 -0700 | [diff] [blame] | 1076 | Path barDir = moduleRoot.getRelative("bar~2.0"); |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1077 | scratch.file(barDir.getRelative("WORKSPACE").getPathString()); |
| 1078 | scratch.file(barDir.getRelative("BUILD").getPathString()); |
Googler | a0cd355 | 2023-05-01 13:43:28 -0700 | [diff] [blame] | 1079 | scratch.file(barDir.getRelative("test.scl").getPathString(), "haha = 5"); |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1080 | |
Googler | 34a4f13 | 2022-07-12 08:42:20 -0700 | [diff] [blame] | 1081 | SkyKey skyKey = BzlLoadValue.keyForBzlmod(Label.parseCanonical("@@foo~1.0//:test.bzl")); |
wyv | a817645 | 2021-09-02 05:38:18 -0700 | [diff] [blame] | 1082 | EvaluationResult<BzlLoadValue> result = |
| 1083 | SkyframeExecutorTestUtils.evaluate( |
| 1084 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
| 1085 | |
| 1086 | assertThatEvaluationResult(result).hasNoError(); |
| 1087 | assertThat(result.get(skyKey).getModule().getGlobals()) |
| 1088 | .containsEntry("hoho", StarlarkInt.of(5)); |
| 1089 | // Note that we're not testing the case of a non-registry override using @bazel_tools here, but |
| 1090 | // that is incredibly hard to set up in a unit test. So we should just rely on integration tests |
| 1091 | // for that. |
| 1092 | } |
| 1093 | |
| 1094 | @Test |
brandjon | 6c63b8f | 2021-02-01 10:17:42 -0800 | [diff] [blame] | 1095 | public void testBuiltinsInjectionFailure() throws Exception { |
| 1096 | setBuildLanguageOptions("--experimental_builtins_bzl_path=tools/builtins_staging"); |
| 1097 | scratch.file( |
| 1098 | "tools/builtins_staging/exports.bzl", |
| 1099 | "1 // 0 # <-- dynamic error", |
| 1100 | "exported_toplevels = {}", |
| 1101 | "exported_rules = {}", |
| 1102 | "exported_to_java = {}"); |
| 1103 | scratch.file("pkg/BUILD"); |
| 1104 | scratch.file("pkg/foo.bzl"); |
| 1105 | reporter.removeHandler(failFastHandler); |
| 1106 | |
| 1107 | SkyKey key = key("//pkg:foo.bzl"); |
| 1108 | EvaluationResult<BzlLoadValue> result = |
| 1109 | SkyframeExecutorTestUtils.evaluate( |
| 1110 | getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); |
| 1111 | |
| 1112 | assertContainsEvent( |
| 1113 | "File \"/workspace/tools/builtins_staging/exports.bzl\", line 1, column 3, in <toplevel>"); |
| 1114 | assertContainsEvent("Error: integer division by zero"); |
| 1115 | Exception ex = result.getError(key).getException(); |
| 1116 | assertThat(ex) |
| 1117 | .hasMessageThat() |
| 1118 | .contains( |
| 1119 | "Internal error while loading Starlark builtins for //pkg:foo.bzl: Failed to load" |
| 1120 | + " builtins sources: initialization of module 'exports.bzl' (internal) failed"); |
| 1121 | } |
| 1122 | |
| 1123 | @Test |
brandjon | 9db98c4 | 2020-08-24 08:43:55 -0700 | [diff] [blame] | 1124 | public void testErrorReadingBzlFileIsTransientWhenUsingASTInlining() throws Exception { |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1125 | CustomInMemoryFs fs = (CustomInMemoryFs) fileSystem; |
| 1126 | scratch.file("a/BUILD"); |
| 1127 | fs.badPathForRead = scratch.file("a/a1.bzl", "doesntmatter"); |
| 1128 | |
| 1129 | SkyKey key = key("//a:a1.bzl"); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1130 | EvaluationResult<BzlLoadValue> result = |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1131 | SkyframeExecutorTestUtils.evaluate( |
| 1132 | getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); |
| 1133 | assertThatEvaluationResult(result).hasErrorEntryForKeyThat(key).isTransient(); |
| 1134 | } |
| 1135 | |
| 1136 | @Test |
| 1137 | public void testErrorReadingOtherBzlFileIsPersistentFromPerspectiveOfParent() throws Exception { |
| 1138 | CustomInMemoryFs fs = (CustomInMemoryFs) fileSystem; |
| 1139 | scratch.file("a/BUILD"); |
| 1140 | scratch.file("a/a1.bzl", "load('//a:a2.bzl', 'a2')"); |
| 1141 | fs.badPathForRead = scratch.file("a/a2.bzl", "doesntmatter"); |
| 1142 | |
| 1143 | SkyKey key = key("//a:a1.bzl"); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1144 | EvaluationResult<BzlLoadValue> result = |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1145 | SkyframeExecutorTestUtils.evaluate( |
| 1146 | getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); |
| 1147 | assertThatEvaluationResult(result).hasErrorEntryForKeyThat(key).isNotTransient(); |
| 1148 | } |
| 1149 | |
| 1150 | @Test |
| 1151 | public void testErrorStatingBzlFileInFileStateFunctionIsPersistent() throws Exception { |
| 1152 | CustomInMemoryFs fs = (CustomInMemoryFs) fileSystem; |
| 1153 | scratch.file("a/BUILD"); |
| 1154 | fs.badPathForStat = scratch.file("a/a1.bzl", "doesntmatter"); |
| 1155 | |
| 1156 | SkyKey key = key("//a:a1.bzl"); |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 1157 | EvaluationResult<BzlLoadValue> result = |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1158 | SkyframeExecutorTestUtils.evaluate( |
| 1159 | getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); |
| 1160 | assertThatEvaluationResult(result).hasErrorEntryForKeyThat(key).isNotTransient(); |
| 1161 | } |
| 1162 | |
| 1163 | private static class CustomInMemoryFs extends InMemoryFileSystem { |
| 1164 | @Nullable private Path badPathForStat; |
| 1165 | @Nullable private Path badPathForRead; |
| 1166 | |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 1167 | CustomInMemoryFs() { |
| 1168 | super(DigestHashFunction.SHA256); |
| 1169 | } |
| 1170 | |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1171 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 1172 | public FileStatus statIfFound(PathFragment path, boolean followSymlinks) throws IOException { |
| 1173 | if (badPathForStat != null && badPathForStat.asFragment().equals(path)) { |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1174 | throw new IOException("bad"); |
| 1175 | } |
| 1176 | return super.statIfFound(path, followSymlinks); |
| 1177 | } |
| 1178 | |
| 1179 | @Override |
Googler | f069347 | 2022-07-21 15:10:20 -0700 | [diff] [blame] | 1180 | protected synchronized InputStream getInputStream(PathFragment path) throws IOException { |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 1181 | if (badPathForRead != null && badPathForRead.asFragment().equals(path)) { |
nharmata | dc1d9dc | 2020-04-18 16:53:28 -0700 | [diff] [blame] | 1182 | throw new IOException("bad"); |
| 1183 | } |
| 1184 | return super.getInputStream(path); |
| 1185 | } |
| 1186 | } |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1187 | } |