Skip to content

Commit ffe595c

Browse files
mai93copybara-github
authored andcommitted
Remove ctx.new_file
RELNOTES[INC]: `ctx.new_file` was removed, `ctx.actions.declare_file` is to be used instead. PiperOrigin-RevId: 564409453 Change-Id: I293fefc8de46e9f7cda9168a182957deefb51b9d
1 parent e3c20dc commit ffe595c

File tree

3 files changed

+0
-180
lines changed

3 files changed

+0
-180
lines changed

src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
import com.google.devtools.build.lib.packages.StructProvider;
7474
import com.google.devtools.build.lib.packages.Type;
7575
import com.google.devtools.build.lib.packages.Type.LabelClass;
76-
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
7776
import com.google.devtools.build.lib.shell.ShellUtils;
7877
import com.google.devtools.build.lib.shell.ShellUtils.TokenizationException;
7978
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleContextApi;
@@ -820,59 +819,6 @@ boolean isForAspect() {
820819
return isForAspect;
821820
}
822821

823-
@Override
824-
public Artifact newFile(Object var1, Object var2, Object fileSuffix) throws EvalException {
825-
checkMutable("new_file");
826-
checkDeprecated("ctx.actions.declare_file", "ctx.new_file", getStarlarkSemantics());
827-
828-
// Determine which of new_file's four signatures is being used. Yes, this is terrible.
829-
// It's one major reason that this method is deprecated.
830-
if (fileSuffix != Starlark.UNBOUND) {
831-
// new_file(file_root, sibling_file, suffix)
832-
ArtifactRoot root =
833-
assertTypeForNewFile(
834-
var1, ArtifactRoot.class, "expected first param to be of type 'root'");
835-
Artifact siblingFile =
836-
assertTypeForNewFile(var2, Artifact.class, "expected second param to be of type 'File'");
837-
PathFragment original =
838-
siblingFile.getOutputDirRelativePath(getConfiguration().isSiblingRepositoryLayout());
839-
PathFragment fragment = original.replaceName(original.getBaseName() + fileSuffix);
840-
return ruleContext.getDerivedArtifact(fragment, root);
841-
842-
} else if (var2 == Starlark.UNBOUND) {
843-
// new_file(filename)
844-
String filename =
845-
assertTypeForNewFile(var1, String.class, "expected first param to be of type 'string'");
846-
return actionFactory.declareFile(filename, Starlark.NONE);
847-
848-
} else {
849-
String filename =
850-
assertTypeForNewFile(var2, String.class, "expected second param to be of type 'string'");
851-
if (var1 instanceof ArtifactRoot) {
852-
// new_file(root, filename)
853-
ArtifactRoot root = (ArtifactRoot) var1;
854-
855-
return ruleContext.getPackageRelativeArtifact(filename, root);
856-
} else {
857-
// new_file(sibling_file, filename)
858-
Artifact siblingFile =
859-
assertTypeForNewFile(
860-
var1, Artifact.class, "expected first param to be of type 'File' or 'root'");
861-
862-
return actionFactory.declareFile(filename, siblingFile);
863-
}
864-
}
865-
}
866-
867-
private static <T> T assertTypeForNewFile(Object obj, Class<T> type, String errorMessage)
868-
throws EvalException {
869-
if (type.isInstance(obj)) {
870-
return type.cast(obj);
871-
} else {
872-
throw new EvalException(errorMessage);
873-
}
874-
}
875-
876822
@Override
877823
public boolean checkPlaceholders(String template, Sequence<?> allowedPlaceholders) // <String>
878824
throws EvalException {
@@ -1171,16 +1117,6 @@ private static Map<Label, Iterable<Artifact>> checkLabelDict(Map<?, ?> labelDict
11711117
return convertedMap;
11721118
}
11731119

1174-
private static void checkDeprecated(String newApi, String oldApi, StarlarkSemantics semantics)
1175-
throws EvalException {
1176-
if (semantics.getBool(BuildLanguageOptions.INCOMPATIBLE_NEW_ACTIONS_API)) {
1177-
throw Starlark.errorf(
1178-
"Use %s instead of %s.\n"
1179-
+ "Use --incompatible_new_actions_api=false to temporarily disable this check.",
1180-
newApi, oldApi);
1181-
}
1182-
}
1183-
11841120
/**
11851121
* Builds a map: Label -> List of files from the given labels
11861122
*

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/StarlarkRuleContextApi.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
public interface StarlarkRuleContextApi<ConstraintValueT extends ConstraintValueInfoApi>
5757
extends StarlarkValue {
5858

59-
String DOC_NEW_FILE_TAIL =
60-
"Does not actually create a file on the file system, just declares that some action will do"
61-
+ " so. You must create an action that generates the file. If the file should be visible"
62-
+ " to other rules, declare a rule output instead when possible. Doing so enables Blaze"
63-
+ " to associate a label with the file that rules can refer to (allowing finer"
64-
+ " dependency control) instead of referencing the whole rule.";
6559
String EXECUTABLE_DOC =
6660
"A <code>struct</code> containing executable files defined in <a"
6761
+ " href='../toplevel/attr.html#label'>label type attributes</a> marked as <a"
@@ -334,47 +328,6 @@ public interface StarlarkRuleContextApi<ConstraintValueT extends ConstraintValue
334328
})
335329
Sequence<String> tokenize(String optionString) throws EvalException;
336330

337-
@StarlarkMethod(
338-
name = "new_file",
339-
doc =
340-
"DEPRECATED. Use <a"
341-
+ " href=\"../builtins/actions.html#declare_file\">ctx.actions.declare_file</a>."
342-
+ " <br>Creates a file object. There are four possible signatures to this"
343-
+ " method:<br><ul><li>new_file(filename): Creates a file object with the given"
344-
+ " filename in the current package.</li><li>new_file(file_root, filename): Creates a"
345-
+ " file object with the given filename under the given file root.</li>"
346-
+ "<li>new_file(sibling_file, filename): Creates a file object in the same directory"
347-
+ " as the given sibling file.</li><li>new_file(file_root, sibling_file, suffix):"
348-
+ " Creates a file object with same base name of the sibling_file but with different"
349-
+ " given suffix, under the given file root.</li></ul> <br>"
350-
+ DOC_NEW_FILE_TAIL,
351-
parameters = {
352-
@Param(
353-
name = "var1",
354-
allowedTypes = {
355-
@ParamType(type = String.class),
356-
@ParamType(type = FileRootApi.class),
357-
@ParamType(type = FileApi.class),
358-
},
359-
doc = ""),
360-
@Param(
361-
name = "var2",
362-
allowedTypes = {
363-
@ParamType(type = String.class),
364-
@ParamType(type = FileApi.class),
365-
},
366-
defaultValue = "unbound",
367-
doc = ""),
368-
@Param(
369-
name = "var3",
370-
allowedTypes = {
371-
@ParamType(type = String.class),
372-
},
373-
defaultValue = "unbound",
374-
doc = "")
375-
})
376-
FileApi newFile(Object var1, Object var2, Object var3) throws EvalException;
377-
378331
@StarlarkMethod(
379332
name = "check_placeholders",
380333
documented = false,

src/test/java/com/google/devtools/build/lib/starlark/StarlarkRuleContextTest.java

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.devtools.build.lib.starlark;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18-
import static com.google.common.truth.Truth.assertWithMessage;
1918
import static com.google.common.truth.Truth8.assertThat;
2019
import static com.google.devtools.build.lib.packages.Attribute.attr;
2120
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
@@ -1029,15 +1028,6 @@ public void testWorkspaceName() throws Exception {
10291028
assertThat(ruleClassProvider.getRunfilesPrefix()).isEqualTo(result);
10301029
}
10311030

1032-
@Test
1033-
public void testDeriveArtifactLegacy() throws Exception {
1034-
setBuildLanguageOptions("--incompatible_new_actions_api=false");
1035-
setRuleContext(createRuleContext("//foo:foo"));
1036-
Object result = ev.eval("ruleContext.new_file(ruleContext.genfiles_dir," + " 'a/b.txt')");
1037-
PathFragment fragment = ((Artifact) result).getRootRelativePath();
1038-
assertThat(fragment.getPathString()).isEqualTo("foo/a/b.txt");
1039-
}
1040-
10411031
@Test
10421032
public void testDeriveArtifact() throws Exception {
10431033
setRuleContext(createRuleContext("//foo:foo"));
@@ -1076,28 +1066,6 @@ public void testDeriveTreeArtifactNextToSibling() throws Exception {
10761066
assertThat(artifact.isTreeArtifact()).isTrue();
10771067
}
10781068

1079-
@Test
1080-
public void testParamFileLegacy() throws Exception {
1081-
setBuildLanguageOptions("--incompatible_new_actions_api=false");
1082-
setRuleContext(createRuleContext("//foo:foo"));
1083-
Object result =
1084-
ev.eval(
1085-
"ruleContext.new_file(ruleContext.bin_dir," + "ruleContext.files.tools[0], '.params')");
1086-
PathFragment fragment = ((Artifact) result).getRootRelativePath();
1087-
assertThat(fragment.getPathString()).isEqualTo("foo/t.exe.params");
1088-
}
1089-
1090-
@Test
1091-
public void testParamFileSuffixLegacy() throws Exception {
1092-
setBuildLanguageOptions("--incompatible_new_actions_api=false");
1093-
setRuleContext(createRuleContext("//foo:foo"));
1094-
Object result =
1095-
ev.eval(
1096-
"ruleContext.new_file(ruleContext.files.tools[0], "
1097-
+ "ruleContext.files.tools[0].basename + '.params')");
1098-
PathFragment fragment = ((Artifact) result).getRootRelativePath();
1099-
assertThat(fragment.getPathString()).isEqualTo("foo/t.exe.params");
1100-
}
11011069

11021070
@Test
11031071
public void testParamFileSuffix() throws Exception {
@@ -2878,8 +2846,6 @@ public void testCoverageInstrumentedMatchesFilterNonDefaultLabel() throws Except
28782846
"aspect_ids",
28792847
"var",
28802848
"tokenize('foo')",
2881-
"new_file('foo.txt')",
2882-
"new_file(file, 'foo.txt')",
28832849
"actions.declare_file('foo.txt')",
28842850
"actions.declare_file('foo.txt', sibling = file)",
28852851
"actions.declare_directory('foo.txt')",
@@ -2998,41 +2964,6 @@ public void testFrozenRuleContextForAspectsHasInaccessibleAttributes() throws Ex
29982964
}
29992965
}
30002966

3001-
private static final List<String> deprecatedActionsApi =
3002-
ImmutableList.of("new_file('foo.txt')", "new_file(file, 'foo.txt')");
3003-
3004-
@Test
3005-
public void testIncompatibleNewActionsApi() throws Exception {
3006-
scratch.file("test/BUILD", "load('//test:rules.bzl', 'main_rule')", "main_rule(name = 'main')");
3007-
scratch.file("test/rules.bzl");
3008-
3009-
for (String actionApi : deprecatedActionsApi) {
3010-
scratch.overwriteFile(
3011-
"test/rules.bzl",
3012-
"def _main_impl(ctx):",
3013-
" file = ctx.outputs.file",
3014-
" foo = ctx." + actionApi,
3015-
"main_rule = rule(",
3016-
" implementation = _main_impl,",
3017-
" attrs = {",
3018-
" 'deps': attr.label_list()",
3019-
" },",
3020-
" outputs = {'file': 'output.txt'},",
3021-
")");
3022-
setBuildLanguageOptions("--incompatible_new_actions_api=true");
3023-
invalidatePackages();
3024-
AssertionError e =
3025-
assertThrows(
3026-
"Should have reported deprecation error for: " + actionApi,
3027-
AssertionError.class,
3028-
() -> getConfiguredTarget("//test:main"));
3029-
assertWithMessage(actionApi + " reported wrong error")
3030-
.that(e)
3031-
.hasMessageThat()
3032-
.contains("Use --incompatible_new_actions_api=false");
3033-
}
3034-
}
3035-
30362967
@Test
30372968
public void testMapAttributeOrdering() throws Exception {
30382969
scratch.file(

0 commit comments

Comments
 (0)