Skip to content

Commit 2974954

Browse files
adonovanhsudhof
authored andcommitted
remove unnecessary uses of Location
The SkylarkCallable(structField=true) feature was never supposed to support useLocation=true, but sadly we forgot to enforce this. This change removes a couple of places where the Location was used to throw an EvalException. It is legal to throw EvalException from a Starlark built-in function; the interpreter will add the location to the exception automatically. The Starlark.errorf utility function makes this very convenient. PiperOrigin-RevId: 286618143 Change-Id: Iaa6b548eb3c7bc0329dbfd47278c63057c147dab
1 parent c7eb8d4 commit 2974954

30 files changed

+899
-743
lines changed

java/com/google/copybara/CheckoutPath.java

+34-42
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.copybara.util.FileUtil;
2323
import com.google.copybara.util.FileUtil.ResolvedSymlink;
2424
import com.google.copybara.util.Glob;
25-
import com.google.devtools.build.lib.events.Location;
2625
import com.google.devtools.build.lib.skylarkinterface.Param;
2726
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
2827
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
@@ -60,14 +59,13 @@ public class CheckoutPath implements Comparable<CheckoutPath>, StarlarkValue {
6059
this.checkoutDir = Preconditions.checkNotNull(checkoutDir);
6160
}
6261

63-
private CheckoutPath create(Path path, Location location) throws EvalException {
64-
return createWithCheckoutDir(path, checkoutDir, location);
62+
private CheckoutPath create(Path path) throws EvalException {
63+
return createWithCheckoutDir(path, checkoutDir);
6564
}
6665

67-
static CheckoutPath createWithCheckoutDir(Path relative, Path checkoutDir,
68-
Location location) throws EvalException {
66+
static CheckoutPath createWithCheckoutDir(Path relative, Path checkoutDir) throws EvalException {
6967
if (relative.isAbsolute()) {
70-
throw new EvalException(location, "Absolute paths are not allowed: " + relative);
68+
throw Starlark.errorf("Absolute paths are not allowed: %s", relative);
7169
}
7270
return new CheckoutPath(relative.normalize(), checkoutDir);
7371
}
@@ -89,19 +87,16 @@ public String name() {
8987
name = "parent",
9088
doc = "Get the parent path",
9189
structField = true,
92-
allowReturnNones = true,
93-
useLocation = true)
94-
public Object parent(Location location) throws EvalException {
90+
allowReturnNones = true)
91+
public Object parent() throws EvalException {
9592
Path parent = path.getParent();
9693
if (parent == null) {
9794
// nio equivalent of new_path("foo").parent returns null, but we want to be able to do
9895
// foo.parent.resolve("bar"). While sibbling could be use for this, sometimes we'll need
9996
// to return the parent folder and another function resolve a path based on that.
100-
return path.toString().equals("")
101-
? Starlark.NONE
102-
: create(path.getFileSystem().getPath(""), location);
97+
return path.toString().equals("") ? Starlark.NONE : create(path.getFileSystem().getPath(""));
10398
}
104-
return create(parent, location);
99+
return create(parent);
105100
}
106101

107102
@SkylarkCallable(
@@ -115,9 +110,9 @@ public Object parent(Location location) throws EvalException {
115110
name = "other",
116111
type = CheckoutPath.class,
117112
doc = "The path to relativize against this path"),
118-
}, useLocation = true)
119-
public CheckoutPath relativize(CheckoutPath other, Location location) throws EvalException {
120-
return create(path.relativize(other.path), location);
113+
})
114+
public CheckoutPath relativize(CheckoutPath other) throws EvalException {
115+
return create(path.relativize(other.path));
121116
}
122117

123118
@SkylarkCallable(
@@ -130,15 +125,15 @@ public CheckoutPath relativize(CheckoutPath other, Location location) throws Eva
130125
doc =
131126
"Resolve the given path against this path. The parameter"
132127
+ " can be a string or a Path.")
133-
}, useLocation = true)
134-
public CheckoutPath resolve(Object child, Location location) throws EvalException {
128+
})
129+
public CheckoutPath resolve(Object child) throws EvalException {
135130
if (child instanceof String) {
136-
return create(path.resolve((String) child), location);
131+
return create(path.resolve((String) child));
137132
} else if (child instanceof CheckoutPath) {
138-
return create(path.resolve(((CheckoutPath) child).path), location);
133+
return create(path.resolve(((CheckoutPath) child).path));
139134
}
140-
throw new EvalException(location,
141-
"Cannot resolve children for type " + child.getClass().getSimpleName() + ":" + child);
135+
throw Starlark.errorf(
136+
"Cannot resolve children for type %s: %s", child.getClass().getSimpleName(), child);
142137
}
143138

144139
@SkylarkCallable(
@@ -151,57 +146,54 @@ public CheckoutPath resolve(Object child, Location location) throws EvalExceptio
151146
doc =
152147
"Resolve the given path against this path. The parameter can be a string or"
153148
+ " a Path."),
154-
}, useLocation = true)
155-
public CheckoutPath resolveSibling(Object other, Location location) throws EvalException {
149+
})
150+
public CheckoutPath resolveSibling(Object other) throws EvalException {
156151
if (other instanceof String) {
157-
return create(path.resolveSibling((String) other), location);
152+
return create(path.resolveSibling((String) other));
158153
} else if (other instanceof CheckoutPath) {
159-
return create(path.resolveSibling(((CheckoutPath) other).path), location);
154+
return create(path.resolveSibling(((CheckoutPath) other).path));
160155
}
161-
throw new EvalException(location,
162-
"Cannot resolve sibling for type " + other.getClass().getSimpleName() + ": " + other);
156+
throw Starlark.errorf(
157+
"Cannot resolve sibling for type %s: %s", other.getClass().getSimpleName(), other);
163158
}
164159

165160
@SkylarkCallable(
166161
name = "attr",
167162
doc = "Get the file attributes, for example size.",
168-
structField = true,
169-
useLocation = true)
170-
public CheckoutPathAttributes attr(Location location) throws EvalException {
163+
structField = true)
164+
public CheckoutPathAttributes attr() throws EvalException {
171165
try {
172166
return new CheckoutPathAttributes(path,
173167
Files.readAttributes(checkoutDir.resolve(path), BasicFileAttributes.class,
174168
LinkOption.NOFOLLOW_LINKS));
175169
} catch (IOException e) {
176170
String msg = "Error getting attributes for " + path + ":" + e;
177171
logger.atSevere().withCause(e).log(msg);
178-
throw new EvalException(location, msg); // or IOException?
172+
throw Starlark.errorf("%s", msg); // or IOException?
179173
}
180174
}
181175

182-
@SkylarkCallable(name = "read_symlink", doc = "Read the symlink", useLocation = true)
183-
public CheckoutPath readSymbolicLink(Location location) throws EvalException {
176+
@SkylarkCallable(name = "read_symlink", doc = "Read the symlink")
177+
public CheckoutPath readSymbolicLink() throws EvalException {
184178
try {
185179
Path symlinkPath = checkoutDir.resolve(path);
186180
if (!Files.isSymbolicLink(symlinkPath)) {
187-
throw new EvalException(location, String.format("%s is not a symlink", path));
181+
throw Starlark.errorf("%s is not a symlink", path);
188182
}
189183

190184
ResolvedSymlink resolvedSymlink =
191185
FileUtil.resolveSymlink(Glob.ALL_FILES.relativeTo(checkoutDir), symlinkPath);
192186
if (!resolvedSymlink.isAllUnderRoot()) {
193-
throw new EvalException(
194-
location,
195-
String.format(
196-
"Symlink %s points to a file outside the checkout dir: %s",
197-
symlinkPath, resolvedSymlink.getRegularFile()));
187+
throw Starlark.errorf(
188+
"Symlink %s points to a file outside the checkout dir: %s",
189+
symlinkPath, resolvedSymlink.getRegularFile());
198190
}
199191

200-
return create(checkoutDir.relativize(resolvedSymlink.getRegularFile()), location);
192+
return create(checkoutDir.relativize(resolvedSymlink.getRegularFile()));
201193
} catch (IOException e) {
202194
String msg = String.format("Cannot resolve symlink %s: %s", path, e);
203195
logger.atSevere().withCause(e).log(msg);
204-
throw new EvalException(null, msg, e);
196+
throw Starlark.errorf("%s", msg);
205197
}
206198
}
207199

java/com/google/copybara/CheckoutPathAttributes.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
package com.google.copybara;
1818

1919
import com.google.common.base.Preconditions;
20-
import com.google.devtools.build.lib.events.Location;
2120
import com.google.devtools.build.lib.skylarkinterface.SkylarkCallable;
2221
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
2322
import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
24-
import com.google.devtools.build.lib.syntax.EvalException;
23+
import com.google.devtools.build.lib.syntax.Starlark;
2524
import com.google.devtools.build.lib.syntax.StarlarkValue;
2625
import java.nio.file.Path;
2726
import java.nio.file.attribute.BasicFileAttributes;
@@ -45,14 +44,13 @@ public class CheckoutPathAttributes implements StarlarkValue {
4544
@SkylarkCallable(
4645
name = "size",
4746
doc = "The size of the file. Throws an error if file size > 2GB.",
48-
structField = true, useLocation = true)
49-
public int size(Location location) throws Exception {
47+
structField = true)
48+
public int size() throws Exception {
5049
long size = attributes.size();
5150
try {
5251
return Math.toIntExact(size);
5352
} catch (ArithmeticException e) {
54-
throw new EvalException(location,
55-
String.format("File %s is too big to compute the size: %d bytes", path, size));
53+
throw Starlark.errorf("File %s is too big to compute the size: %d bytes", path, size);
5654
}
5755
}
5856

0 commit comments

Comments
 (0)