Skip to content

Commit 47f2143

Browse files
authored
Fix PklBugException when reading local package assets (#642)
1 parent 7868d9d commit 47f2143

File tree

4 files changed

+113
-24
lines changed

4 files changed

+113
-24
lines changed

pkl-core/src/main/java/org/pkl/core/resource/ResourceReaders.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,16 @@ public Optional<Object> read(URI uri)
488488
var dependency = getProjectDepsResolver().getResolvedDependency(assetUri.getPackageUri());
489489
var local = getLocalUri(dependency, assetUri);
490490
if (local != null) {
491-
return VmContext.get(null).getResourceManager().read(local, null);
491+
var resourceManager = VmContext.get(null).getResourceManager();
492+
var securityManager = VmContext.get(null).getSecurityManager();
493+
securityManager.checkReadResource(local);
494+
var reader = resourceManager.getResourceReader(local);
495+
if (reader == null) {
496+
throw new VmExceptionBuilder()
497+
.evalError("noResourceReaderRegistered", uri.getScheme())
498+
.build();
499+
}
500+
return resourceManager.doRead(reader, local, null);
492501
}
493502
var remoteDep = (Dependency.RemoteDependency) dependency;
494503
var bytes = getPackageResolver().getBytes(assetUri, true, remoteDep.getChecksums());

pkl-core/src/main/java/org/pkl/core/runtime/ResourceManager.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,43 +67,46 @@ public ResourceReader getReader(URI resourceUri, Node readNode) {
6767
return reader;
6868
}
6969

70+
public Optional<Object> doRead(ResourceReader reader, URI uri, @Nullable Node readNode) {
71+
Optional<Object> resource;
72+
try {
73+
resource = reader.read(uri);
74+
} catch (IOException e) {
75+
throw new VmExceptionBuilder()
76+
.evalError("ioErrorReadingResource", uri)
77+
.withCause(e)
78+
.withOptionalLocation(readNode)
79+
.build();
80+
} catch (URISyntaxException e) {
81+
throw new VmExceptionBuilder()
82+
.evalError("invalidResourceUri", uri)
83+
.withHint(e.getReason())
84+
.withOptionalLocation(readNode)
85+
.build();
86+
} catch (SecurityManagerException | PackageLoadError | HttpClientInitException e) {
87+
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
88+
}
89+
return resource;
90+
}
91+
7092
@TruffleBoundary
7193
public Optional<Object> read(URI resourceUri, @Nullable Node readNode) {
7294
return resources.computeIfAbsent(
7395
resourceUri.normalize(),
74-
uri -> {
96+
(uri) -> {
7597
try {
7698
securityManager.checkReadResource(uri);
7799
} catch (SecurityManagerException e) {
78100
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
79101
}
80-
81-
var reader = resourceReaders.get(uri.getScheme());
102+
var reader = getResourceReader(uri);
82103
if (reader == null) {
83104
throw new VmExceptionBuilder()
84105
.withOptionalLocation(readNode)
85-
.evalError("noResourceReaderRegistered", resourceUri.getScheme())
86-
.build();
87-
}
88-
89-
Optional<Object> resource;
90-
try {
91-
resource = reader.read(uri);
92-
} catch (IOException e) {
93-
throw new VmExceptionBuilder()
94-
.evalError("ioErrorReadingResource", uri)
95-
.withCause(e)
96-
.withOptionalLocation(readNode)
106+
.evalError("noResourceReaderRegistered", uri.getScheme())
97107
.build();
98-
} catch (URISyntaxException e) {
99-
throw new VmExceptionBuilder()
100-
.evalError("invalidResourceUri", resourceUri)
101-
.withHint(e.getReason())
102-
.withOptionalLocation(readNode)
103-
.build();
104-
} catch (SecurityManagerException | PackageLoadError | HttpClientInitException e) {
105-
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
106108
}
109+
var resource = doRead(reader, uri, readNode);
107110
if (resource.isEmpty()) return resource;
108111

109112
var res = resource.get();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
res = read("@project2/penguin.pkl").text
2+
res2 = read*("@project2/*")
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
res = """
2+
import "@burds/Bird.pkl"
3+
4+
bird: Bird = new {
5+
name = "Penguin"
6+
favoriteFruit {
7+
name = "Ice Fruit"
8+
}
9+
}
10+
11+
"""
12+
res2 {
13+
["@project2/PklProject"] {
14+
uri = "file:/$snippetsDir/input/projects/project2/PklProject"
15+
text = """
16+
amends "pkl:Project"
17+
18+
package {
19+
name = "project2"
20+
baseUri = "package://localhost:0/project2"
21+
version = "1.0.0"
22+
packageZipUrl = "https://localhost:0/project2/project2-\\(version).zip"
23+
}
24+
25+
dependencies {
26+
["burds"] {
27+
uri = "package://localhost:0/birds@0.5.0"
28+
}
29+
}
30+
31+
"""
32+
base64 = "YW1lbmRzICJwa2w6UHJvamVjdCIKCnBhY2thZ2UgewogIG5hbWUgPSAicHJvamVjdDIiCiAgYmFzZVVyaSA9ICJwYWNrYWdlOi8vbG9jYWxob3N0OjAvcHJvamVjdDIiCiAgdmVyc2lvbiA9ICIxLjAuMCIKICBwYWNrYWdlWmlwVXJsID0gImh0dHBzOi8vbG9jYWxob3N0OjAvcHJvamVjdDIvcHJvamVjdDItXCh2ZXJzaW9uKS56aXAiCn0KCmRlcGVuZGVuY2llcyB7CiAgWyJidXJkcyJdIHsKICAgIHVyaSA9ICJwYWNrYWdlOi8vbG9jYWxob3N0OjAvYmlyZHNAMC41LjAiCiAgfQp9Cg=="
33+
}
34+
["@project2/PklProject.deps.json"] {
35+
uri = "file:/$snippetsDir/input/projects/project2/PklProject.deps.json"
36+
text = """
37+
{
38+
"schemaVersion": 1,
39+
"resolvedDependencies": {
40+
"package://localhost:0/birds@0": {
41+
"type": "remote",
42+
"uri": "projectpackage://localhost:0/birds@0.5.0",
43+
"checksums": {
44+
"sha256": "$skipChecksumVerification"
45+
}
46+
},
47+
"package://localhost:0/fruit@1": {
48+
"type": "remote",
49+
"uri": "projectpackage://localhost:0/fruit@1.1.0",
50+
"checksums": {
51+
"sha256": "$skipChecksumVerification"
52+
}
53+
}
54+
}
55+
}
56+
57+
"""
58+
base64 = "ewogICJzY2hlbWFWZXJzaW9uIjogMSwKICAicmVzb2x2ZWREZXBlbmRlbmNpZXMiOiB7CiAgICAicGFja2FnZTovL2xvY2FsaG9zdDowL2JpcmRzQDAiOiB7CiAgICAgICJ0eXBlIjogInJlbW90ZSIsCiAgICAgICJ1cmkiOiAicHJvamVjdHBhY2thZ2U6Ly9sb2NhbGhvc3Q6MC9iaXJkc0AwLjUuMCIsCiAgICAgICJjaGVja3N1bXMiOiB7CiAgICAgICAgInNoYTI1NiI6ICIkc2tpcENoZWNrc3VtVmVyaWZpY2F0aW9uIgogICAgICB9CiAgICB9LAogICAgInBhY2thZ2U6Ly9sb2NhbGhvc3Q6MC9mcnVpdEAxIjogewogICAgICAidHlwZSI6ICJyZW1vdGUiLAogICAgICAidXJpIjogInByb2plY3RwYWNrYWdlOi8vbG9jYWxob3N0OjAvZnJ1aXRAMS4xLjAiLAogICAgICAiY2hlY2tzdW1zIjogewogICAgICAgICJzaGEyNTYiOiAiJHNraXBDaGVja3N1bVZlcmlmaWNhdGlvbiIKICAgICAgfQogICAgfQogIH0KfQo="
59+
}
60+
["@project2/penguin.pkl"] {
61+
uri = "file:/$snippetsDir/input/projects/project2/penguin.pkl"
62+
text = """
63+
import "@burds/Bird.pkl"
64+
65+
bird: Bird = new {
66+
name = "Penguin"
67+
favoriteFruit {
68+
name = "Ice Fruit"
69+
}
70+
}
71+
72+
"""
73+
base64 = "aW1wb3J0ICJAYnVyZHMvQmlyZC5wa2wiCgpiaXJkOiBCaXJkID0gbmV3IHsKICBuYW1lID0gIlBlbmd1aW4iCiAgZmF2b3JpdGVGcnVpdCB7CiAgICBuYW1lID0gIkljZSBGcnVpdCIKICB9Cn0K"
74+
}
75+
}

0 commit comments

Comments
 (0)