Skip to content

Commit 6255358

Browse files
committed
Remove (deprecated) implicit outputs, part 1
The `outputs` parameter on the `rule()` function has been deprecated and will be removed in a future version of Bazel. See bazelbuild/bazel#7977 for context.
1 parent 62746bd commit 6255358

32 files changed

+426
-139
lines changed

closure/compiler/closure_js_binary.bzl

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ load(
3333
"closure_js_aspect",
3434
)
3535

36+
JavaScriptBinaryInfo = provider(
37+
doc = "Encapsulates information provided by `closure_js_binary`.",
38+
fields = {
39+
"output": """
40+
Primary output file.
41+
""".strip(),
42+
"sourcemap": """
43+
The sourcemap file.
44+
""".strip(),
45+
"property_renaming_report": """
46+
File containing a serialized version of the property renaming map.
47+
48+
If `compilation_level` is not `ADVANCED`, this is an empty file.
49+
""".strip(),
50+
},
51+
)
52+
3653
_dependency_mode_warning = '\n'.join([
3754
"{target}: dependency_mode={old_mode} is deprecated and will be " +
3855
"removed soon; prefer to use its equivalent {new_mode}.",
@@ -77,11 +94,19 @@ def _impl(ctx):
7794

7895
_validate_css_graph(ctx, js)
7996

97+
output = ctx.actions.declare_file("{}.js".format(ctx.attr.name))
98+
sourcemap = ctx.actions.declare_file("{}.js.map".format(ctx.attr.name))
99+
100+
if ctx.outputs.property_renaming_report:
101+
property_renaming_report = ctx.outputs.property_renaming_report
102+
else:
103+
property_renaming_report = ctx.actions.declare_file("{}.property_renaming_report.txt".format(ctx.attr.name))
104+
80105
# This is the list of files we'll be generating.
81-
outputs = [ctx.outputs.bin, ctx.outputs.map, ctx.outputs.stderr]
106+
outputs = [output, sourcemap, ctx.outputs.stderr]
82107

83108
# This is the subset of that list we'll report to parent rules.
84-
files = [ctx.outputs.bin, ctx.outputs.map]
109+
files = [output, sourcemap]
85110

86111
# JsCompiler is thin veneer over the Closure compiler. It's configured with a
87112
# superset of its flags. It introduces a private testing API, allows per-file
@@ -91,9 +116,9 @@ def _impl(ctx):
91116
args = [
92117
"JsCompiler",
93118
"--js_output_file",
94-
ctx.outputs.bin.path,
119+
output.path,
95120
"--create_source_map",
96-
ctx.outputs.map.path,
121+
sourcemap.path,
97122
"--output_errors",
98123
ctx.outputs.stderr.path,
99124
"--language_in",
@@ -138,7 +163,7 @@ def _impl(ctx):
138163
js_module_roots = sort_roots(
139164
depset(transitive = [
140165
find_js_module_roots(
141-
[ctx.outputs.bin],
166+
[output],
142167
ctx.workspace_name,
143168
ctx.label,
144169
getattr(ctx.attr, "includes", []),
@@ -155,7 +180,7 @@ def _impl(ctx):
155180
# stored within the same directory as the compiled JS binary; therefore, the
156181
# JSON sourcemap file should cite that file as relative to itself.
157182
args.append("--source_map_location_mapping")
158-
args.append("%s|%s" % (ctx.outputs.bin.path, ctx.outputs.bin.basename))
183+
args.append("%s|%s" % (output.path, output.basename))
159184

160185
# By default we're going to include the raw sources in the .js.map file. This
161186
# can be disabled with the nodefs attribute.
@@ -205,12 +230,16 @@ def _impl(ctx):
205230
if ctx.attr.output_wrapper == "(function(){%output%}).call(this);":
206231
args.append("--assume_function_wrapper")
207232

208-
if ctx.outputs.property_renaming_report:
209-
report = ctx.outputs.property_renaming_report
210-
files.append(report)
211-
outputs.append(report)
233+
if ("ADVANCED" == ctx.attr.compilation_level) and (not ctx.attr.internal_expect_failure):
234+
files.append(property_renaming_report)
235+
outputs.append(property_renaming_report)
212236
args.append("--property_renaming_report")
213-
args.append(report.path)
237+
args.append(property_renaming_report.path)
238+
else:
239+
ctx.actions.write(
240+
output = property_renaming_report,
241+
content = "",
242+
)
214243

215244
# All sources must conform to these protos.
216245
for config in ctx.files.conformance:
@@ -268,7 +297,7 @@ def _impl(ctx):
268297
execution_requirements = {"supports-workers": "1"},
269298
progress_message = "Compiling %d JavaScript files to %s" % (
270299
len(js.srcs.to_list()),
271-
ctx.outputs.bin.short_path,
300+
output.short_path,
272301
),
273302
)
274303

@@ -280,8 +309,8 @@ def _impl(ctx):
280309
files = depset(files),
281310
closure_js_library = js,
282311
closure_js_binary = struct(
283-
bin = ctx.outputs.bin,
284-
map = ctx.outputs.map,
312+
bin = output,
313+
map = sourcemap,
285314
language = ctx.attr.language,
286315
),
287316
runfiles = ctx.runfiles(
@@ -292,6 +321,17 @@ def _impl(ctx):
292321
collect_runfiles(ctx.attr.data),
293322
]),
294323
),
324+
325+
# Modern-stype providers.
326+
#
327+
# TODO(yannic): Remove legacy providers.
328+
providers = [
329+
JavaScriptBinaryInfo(
330+
output = output,
331+
sourcemap = sourcemap,
332+
property_renaming_report = property_renaming_report,
333+
),
334+
],
295335
)
296336

297337
def _validate_css_graph(ctx, js):
@@ -345,8 +385,6 @@ closure_js_binary = rule(
345385
"internal_expect_warnings": attr.bool(default = False),
346386
}, **CLOSURE_JS_TOOLCHAIN_ATTRS),
347387
outputs = {
348-
"bin": "%{name}.js",
349-
"map": "%{name}.js.map",
350388
"stderr": "%{name}-stderr.txt",
351389
},
352390
)

closure/compiler/closure_js_library.bzl

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ def _closure_js_library_impl(
104104
# and will be replaced with |actions.declare_file()| soon.
105105
deprecated_info_file = None,
106106
deprecated_stderr_file = None,
107-
deprecated_ijs_file = None,
108107
deprecated_typecheck_file = None):
109108
# TODO(yannic): Figure out how to modify |find_js_module_roots|
110109
# so that we won't need |workspace_name| anymore.
@@ -150,11 +149,8 @@ def _closure_js_library_impl(
150149
deprecated_stderr_file,
151150
"%s-stderr.txt" % label.name,
152151
)
153-
ijs_file = _maybe_declare_file(
154-
actions,
155-
deprecated_ijs_file,
156-
"%s.i.js" % label.name,
157-
)
152+
153+
ijs_file = ctx.actions.declare_file("{}.i.js".format(label.name))
158154

159155
if not no_closure_library:
160156
deps = deps + closure_library_base
@@ -403,24 +399,23 @@ def _closure_js_library(ctx):
403399
srcs = ctx.files.externs + srcs
404400

405401
library = _closure_js_library_impl(
406-
ctx,
407-
srcs,
408-
ctx.attr.deps,
409-
ctx.attr.testonly,
410-
ctx.attr.suppress,
411-
ctx.attr.lenient,
412-
ctx.attr.convention,
413-
getattr(ctx.attr, "includes", []),
414-
ctx.attr.exports,
415-
ctx.files.internal_descriptors,
416-
ctx.attr.no_closure_library,
417-
ctx.attr.internal_expect_failure,
402+
ctx = ctx,
403+
srcs = srcs,
404+
deps = ctx.attr.deps,
405+
testonly = ctx.attr.testonly,
406+
suppress = ctx.attr.suppress,
407+
lenient = ctx.attr.lenient,
408+
convention = ctx.attr.convention,
409+
includes = getattr(ctx.attr, "includes", []),
410+
exports = ctx.attr.exports,
411+
internal_descriptors = ctx.files.internal_descriptors,
412+
no_closure_library = ctx.attr.no_closure_library,
413+
internal_expect_failure = ctx.attr.internal_expect_failure,
418414

419415
# Deprecated output files.
420-
ctx.outputs.info,
421-
ctx.outputs.stderr,
422-
ctx.outputs.ijs,
423-
ctx.outputs.typecheck,
416+
deprecated_info_file = ctx.outputs.info,
417+
deprecated_stderr_file = ctx.outputs.stderr,
418+
deprecated_typecheck_file = ctx.outputs.typecheck,
424419
)
425420

426421
return struct(
@@ -476,7 +471,6 @@ closure_js_library = rule(
476471
outputs = {
477472
"info": "%{name}.pbtxt",
478473
"stderr": "%{name}-stderr.txt",
479-
"ijs": "%{name}.i.js",
480474
"typecheck": "%{name}_typecheck", # dummy output file
481475
},
482476
)

closure/compiler/test/BUILD

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package(default_testonly = True)
1616

1717
licenses(["notice"]) # Apache 2.0
1818

19+
load("//closure/private:expected_output_test.bzl", "closure_js_binary_output_test", "closure_js_binary_sourcemap_test")
1920
load("//closure/private:file_test.bzl", "file_test")
2021
load("//closure:defs.bzl", "closure_js_binary", "closure_js_library")
2122

@@ -30,24 +31,16 @@ closure_js_binary(
3031
deps = [":hello_lib"],
3132
)
3233

33-
file_test(
34+
closure_js_binary_output_test(
3435
name = "minification",
35-
content = "console.log(\"hello world\");\n",
36-
file = "hello_bin.js",
36+
expected_output = "golden/minification.js",
37+
target = ":hello_bin",
3738
)
3839

39-
file_test(
40+
closure_js_binary_sourcemap_test(
4041
name = "sourcemap_doesntContainWeirdBazelDirectories",
41-
content = ("{\n" +
42-
"\"version\":3,\n" +
43-
"\"file\":\"hello_bin.js\",\n" +
44-
"\"lineCount\":1,\n" +
45-
"\"mappings\":\"AAgBAA,OAAA,CAAUC,GAAV,CAAgB,aAAhB;\",\n" +
46-
"\"sources\":[\"/closure/compiler/test/hello.js\"],\n" +
47-
"\"sourcesContent\":[\"// Copyright 2016 The Closure Rules Authors. All rights reserved.\\n//\\n// Licensed under the Apache License, Version 2.0 (the \\\"License\\\");\\n// you may not use this file except in compliance with the License.\\n// You may obtain a copy of the License at\\n//\\n// http://www.apache.org/licenses/LICENSE-2.0\\n//\\n// Unless required by applicable law or agreed to in writing, software\\n// distributed under the License is distributed on an \\\"AS IS\\\" BASIS,\\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\\n// See the License for the specific language governing permissions and\\n// limitations under the License.\\n\\n// hello world\\n\\nconsole . log ( 'hello world' ) ;\\n\"],\n" +
48-
"\"names\":[\"console\",\"log\"]\n" +
49-
"}\n"),
50-
file = "hello_bin.js.map",
42+
expected_output = "golden/sourcemap_doesn_contain_weird_bazel_directories.js.map",
43+
target = ":hello_bin",
5144
)
5245

5346
# Make sure bazel doesn't complain about some outputs not created.
@@ -64,24 +57,16 @@ closure_js_binary(
6457
deps = [":hello_lib"],
6558
)
6659

67-
file_test(
68-
name = "minificationWithWrapper",
69-
content = "(function(){console.log(\"hello world\");}).call(this);\n",
70-
file = "hello_wrap_bin.js",
60+
closure_js_binary_output_test(
61+
name = "minification_with_wrapper",
62+
expected_output = "golden/minification_with_wrapper.js",
63+
target = ":hello_wrap_bin",
7164
)
7265

73-
file_test(
66+
closure_js_binary_sourcemap_test(
7467
name = "sourcemapWithWrapper_hasDifferentMappingCodes",
75-
content = "{\n" +
76-
"\"version\":3,\n" +
77-
"\"file\":\"hello_wrap_bin.js\",\n" +
78-
"\"lineCount\":1,\n" +
79-
"\"mappings\":\"A,YAgBAA,OAAA,CAAUC,GAAV,CAAgB,aAAhB;\",\n" +
80-
"\"sources\":[\"/closure/compiler/test/hello.js\"],\n" +
81-
"\"sourcesContent\":[\"// Copyright 2016 The Closure Rules Authors. All rights reserved.\\n//\\n// Licensed under the Apache License, Version 2.0 (the \\\"License\\\");\\n// you may not use this file except in compliance with the License.\\n// You may obtain a copy of the License at\\n//\\n// http://www.apache.org/licenses/LICENSE-2.0\\n//\\n// Unless required by applicable law or agreed to in writing, software\\n// distributed under the License is distributed on an \\\"AS IS\\\" BASIS,\\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\\n// See the License for the specific language governing permissions and\\n// limitations under the License.\\n\\n// hello world\\n\\nconsole . log ( 'hello world' ) ;\\n\"],\n" +
82-
"\"names\":[\"console\",\"log\"]\n" +
83-
"}\n",
84-
file = "hello_wrap_bin.js.map",
68+
expected_output = "golden/sourcemap_with_wrapper_has_different_mapping_codes.js.map",
69+
target = ":hello_wrap_bin",
8570
)
8671

8772
closure_js_binary(
@@ -90,10 +75,10 @@ closure_js_binary(
9075
deps = [":hello_lib"],
9176
)
9277

93-
file_test(
78+
closure_js_binary_output_test(
9479
name = "strictOutputLanguage_addsUseStrict",
95-
content = "'use strict';console.log(\"hello world\");\n",
96-
file = "hello_es5strict_bin.js",
80+
expected_output = "golden/strict_output_language_adds_use_strict.js",
81+
target = ":hello_es5strict_bin",
9782
)
9883

9984
closure_js_library(
@@ -109,10 +94,10 @@ closure_js_binary(
10994
deps = [":es6const_lib"],
11095
)
11196

112-
file_test(
97+
closure_js_binary_output_test(
11398
name = "es6WithConstKeyword_getsRemoved",
114-
content = "var hello=\"hello world\";console.log(hello);\n",
115-
file = "es6const_bin.js",
99+
expected_output = "golden/es6_with_const_keyword_gets_removed.js",
100+
target = ":es6const_bin",
116101
)
117102

118103
closure_js_library(
@@ -128,10 +113,10 @@ closure_js_binary(
128113
deps = [":es6arrow_lib"],
129114
)
130115

131-
file_test(
116+
closure_js_binary_output_test(
132117
name = "es6WithArrowFunction_getsExpanded",
133-
content = "var hello=function(e){return e+\" world\"};console.log(hello(\"hello\"));\n",
134-
file = "es6arrow_bin.js",
118+
expected_output = "golden/es6_with_arrow_function_gets_expanded.js",
119+
target = ":es6arrow_bin",
135120
)
136121

137122
closure_js_library(
@@ -165,10 +150,10 @@ closure_js_binary(
165150
deps = [":hello_lib"],
166151
)
167152

168-
file_test(
153+
closure_js_binary_output_test(
169154
name = "output_wrapper_dash_dash_space",
170-
content = "-- console.log(\"hello world\");\n",
171-
file = "hello_output_wrapper_dash_dash_space_bin.js",
155+
expected_output = "golden/output_wrapper_dash_dash_space.js",
156+
target = ":hello_output_wrapper_dash_dash_space_bin",
172157
)
173158

174159
closure_js_binary(
@@ -177,10 +162,10 @@ closure_js_binary(
177162
deps = [":empty_lib"],
178163
)
179164

180-
file_test(
165+
closure_js_binary_output_test(
181166
name = "multiline_output_wrapper_test",
182-
content = "\n//# sourceMappingURL=/app.js.map\n",
183-
file = "multiline_output_wrapper_bin.js",
167+
expected_output = "golden/multiline_output_wrapper_test.js",
168+
target = ":multiline_output_wrapper_bin",
184169
)
185170

186171
################################################################################
@@ -202,10 +187,10 @@ closure_js_binary(
202187
deps = [":extern_invoke_lib"],
203188
)
204189

205-
file_test(
190+
closure_js_binary_output_test(
206191
name = "externFile_namesDidntCollapse",
207-
content = "omg.im_an_extern(\"hello\");\n",
208-
file = "extern_bin.js",
192+
expected_output = "golden/extern_file_names_didnt_collapse.js",
193+
target = ":extern_bin",
209194
)
210195

211196
closure_js_library(

0 commit comments

Comments
 (0)