mirror of
https://github.com/apple/pkl.git
synced 2026-03-26 11:01:14 +01:00
Fix PklBugException when reading local package assets (#642)
This commit is contained in:
@@ -488,7 +488,16 @@ public final class ResourceReaders {
|
|||||||
var dependency = getProjectDepsResolver().getResolvedDependency(assetUri.getPackageUri());
|
var dependency = getProjectDepsResolver().getResolvedDependency(assetUri.getPackageUri());
|
||||||
var local = getLocalUri(dependency, assetUri);
|
var local = getLocalUri(dependency, assetUri);
|
||||||
if (local != null) {
|
if (local != null) {
|
||||||
return VmContext.get(null).getResourceManager().read(local, null);
|
var resourceManager = VmContext.get(null).getResourceManager();
|
||||||
|
var securityManager = VmContext.get(null).getSecurityManager();
|
||||||
|
securityManager.checkReadResource(local);
|
||||||
|
var reader = resourceManager.getResourceReader(local);
|
||||||
|
if (reader == null) {
|
||||||
|
throw new VmExceptionBuilder()
|
||||||
|
.evalError("noResourceReaderRegistered", uri.getScheme())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
return resourceManager.doRead(reader, local, null);
|
||||||
}
|
}
|
||||||
var remoteDep = (Dependency.RemoteDependency) dependency;
|
var remoteDep = (Dependency.RemoteDependency) dependency;
|
||||||
var bytes = getPackageResolver().getBytes(assetUri, true, remoteDep.getChecksums());
|
var bytes = getPackageResolver().getBytes(assetUri, true, remoteDep.getChecksums());
|
||||||
|
|||||||
@@ -67,43 +67,46 @@ public final class ResourceManager {
|
|||||||
return reader;
|
return reader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Optional<Object> doRead(ResourceReader reader, URI uri, @Nullable Node readNode) {
|
||||||
|
Optional<Object> resource;
|
||||||
|
try {
|
||||||
|
resource = reader.read(uri);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new VmExceptionBuilder()
|
||||||
|
.evalError("ioErrorReadingResource", uri)
|
||||||
|
.withCause(e)
|
||||||
|
.withOptionalLocation(readNode)
|
||||||
|
.build();
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw new VmExceptionBuilder()
|
||||||
|
.evalError("invalidResourceUri", uri)
|
||||||
|
.withHint(e.getReason())
|
||||||
|
.withOptionalLocation(readNode)
|
||||||
|
.build();
|
||||||
|
} catch (SecurityManagerException | PackageLoadError | HttpClientInitException e) {
|
||||||
|
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
|
||||||
|
}
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
@TruffleBoundary
|
@TruffleBoundary
|
||||||
public Optional<Object> read(URI resourceUri, @Nullable Node readNode) {
|
public Optional<Object> read(URI resourceUri, @Nullable Node readNode) {
|
||||||
return resources.computeIfAbsent(
|
return resources.computeIfAbsent(
|
||||||
resourceUri.normalize(),
|
resourceUri.normalize(),
|
||||||
uri -> {
|
(uri) -> {
|
||||||
try {
|
try {
|
||||||
securityManager.checkReadResource(uri);
|
securityManager.checkReadResource(uri);
|
||||||
} catch (SecurityManagerException e) {
|
} catch (SecurityManagerException e) {
|
||||||
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
|
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
|
||||||
}
|
}
|
||||||
|
var reader = getResourceReader(uri);
|
||||||
var reader = resourceReaders.get(uri.getScheme());
|
|
||||||
if (reader == null) {
|
if (reader == null) {
|
||||||
throw new VmExceptionBuilder()
|
throw new VmExceptionBuilder()
|
||||||
.withOptionalLocation(readNode)
|
.withOptionalLocation(readNode)
|
||||||
.evalError("noResourceReaderRegistered", resourceUri.getScheme())
|
.evalError("noResourceReaderRegistered", uri.getScheme())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
var resource = doRead(reader, uri, readNode);
|
||||||
Optional<Object> resource;
|
|
||||||
try {
|
|
||||||
resource = reader.read(uri);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new VmExceptionBuilder()
|
|
||||||
.evalError("ioErrorReadingResource", uri)
|
|
||||||
.withCause(e)
|
|
||||||
.withOptionalLocation(readNode)
|
|
||||||
.build();
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
throw new VmExceptionBuilder()
|
|
||||||
.evalError("invalidResourceUri", resourceUri)
|
|
||||||
.withHint(e.getReason())
|
|
||||||
.withOptionalLocation(readNode)
|
|
||||||
.build();
|
|
||||||
} catch (SecurityManagerException | PackageLoadError | HttpClientInitException e) {
|
|
||||||
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
|
|
||||||
}
|
|
||||||
if (resource.isEmpty()) return resource;
|
if (resource.isEmpty()) return resource;
|
||||||
|
|
||||||
var res = resource.get();
|
var res = resource.get();
|
||||||
|
|||||||
2
pkl-core/src/test/files/LanguageSnippetTests/input/projects/project1/localProjectRead.pkl
vendored
Normal file
2
pkl-core/src/test/files/LanguageSnippetTests/input/projects/project1/localProjectRead.pkl
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
res = read("@project2/penguin.pkl").text
|
||||||
|
res2 = read*("@project2/*")
|
||||||
75
pkl-core/src/test/files/LanguageSnippetTests/output/projects/project1/localProjectRead.pcf
vendored
Normal file
75
pkl-core/src/test/files/LanguageSnippetTests/output/projects/project1/localProjectRead.pcf
vendored
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
res = """
|
||||||
|
import "@burds/Bird.pkl"
|
||||||
|
|
||||||
|
bird: Bird = new {
|
||||||
|
name = "Penguin"
|
||||||
|
favoriteFruit {
|
||||||
|
name = "Ice Fruit"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
|
res2 {
|
||||||
|
["@project2/PklProject"] {
|
||||||
|
uri = "file:/$snippetsDir/input/projects/project2/PklProject"
|
||||||
|
text = """
|
||||||
|
amends "pkl:Project"
|
||||||
|
|
||||||
|
package {
|
||||||
|
name = "project2"
|
||||||
|
baseUri = "package://localhost:0/project2"
|
||||||
|
version = "1.0.0"
|
||||||
|
packageZipUrl = "https://localhost:0/project2/project2-\\(version).zip"
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
["burds"] {
|
||||||
|
uri = "package://localhost:0/birds@0.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
|
base64 = "YW1lbmRzICJwa2w6UHJvamVjdCIKCnBhY2thZ2UgewogIG5hbWUgPSAicHJvamVjdDIiCiAgYmFzZVVyaSA9ICJwYWNrYWdlOi8vbG9jYWxob3N0OjAvcHJvamVjdDIiCiAgdmVyc2lvbiA9ICIxLjAuMCIKICBwYWNrYWdlWmlwVXJsID0gImh0dHBzOi8vbG9jYWxob3N0OjAvcHJvamVjdDIvcHJvamVjdDItXCh2ZXJzaW9uKS56aXAiCn0KCmRlcGVuZGVuY2llcyB7CiAgWyJidXJkcyJdIHsKICAgIHVyaSA9ICJwYWNrYWdlOi8vbG9jYWxob3N0OjAvYmlyZHNAMC41LjAiCiAgfQp9Cg=="
|
||||||
|
}
|
||||||
|
["@project2/PklProject.deps.json"] {
|
||||||
|
uri = "file:/$snippetsDir/input/projects/project2/PklProject.deps.json"
|
||||||
|
text = """
|
||||||
|
{
|
||||||
|
"schemaVersion": 1,
|
||||||
|
"resolvedDependencies": {
|
||||||
|
"package://localhost:0/birds@0": {
|
||||||
|
"type": "remote",
|
||||||
|
"uri": "projectpackage://localhost:0/birds@0.5.0",
|
||||||
|
"checksums": {
|
||||||
|
"sha256": "$skipChecksumVerification"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"package://localhost:0/fruit@1": {
|
||||||
|
"type": "remote",
|
||||||
|
"uri": "projectpackage://localhost:0/fruit@1.1.0",
|
||||||
|
"checksums": {
|
||||||
|
"sha256": "$skipChecksumVerification"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
|
base64 = "ewogICJzY2hlbWFWZXJzaW9uIjogMSwKICAicmVzb2x2ZWREZXBlbmRlbmNpZXMiOiB7CiAgICAicGFja2FnZTovL2xvY2FsaG9zdDowL2JpcmRzQDAiOiB7CiAgICAgICJ0eXBlIjogInJlbW90ZSIsCiAgICAgICJ1cmkiOiAicHJvamVjdHBhY2thZ2U6Ly9sb2NhbGhvc3Q6MC9iaXJkc0AwLjUuMCIsCiAgICAgICJjaGVja3N1bXMiOiB7CiAgICAgICAgInNoYTI1NiI6ICIkc2tpcENoZWNrc3VtVmVyaWZpY2F0aW9uIgogICAgICB9CiAgICB9LAogICAgInBhY2thZ2U6Ly9sb2NhbGhvc3Q6MC9mcnVpdEAxIjogewogICAgICAidHlwZSI6ICJyZW1vdGUiLAogICAgICAidXJpIjogInByb2plY3RwYWNrYWdlOi8vbG9jYWxob3N0OjAvZnJ1aXRAMS4xLjAiLAogICAgICAiY2hlY2tzdW1zIjogewogICAgICAgICJzaGEyNTYiOiAiJHNraXBDaGVja3N1bVZlcmlmaWNhdGlvbiIKICAgICAgfQogICAgfQogIH0KfQo="
|
||||||
|
}
|
||||||
|
["@project2/penguin.pkl"] {
|
||||||
|
uri = "file:/$snippetsDir/input/projects/project2/penguin.pkl"
|
||||||
|
text = """
|
||||||
|
import "@burds/Bird.pkl"
|
||||||
|
|
||||||
|
bird: Bird = new {
|
||||||
|
name = "Penguin"
|
||||||
|
favoriteFruit {
|
||||||
|
name = "Ice Fruit"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
"""
|
||||||
|
base64 = "aW1wb3J0ICJAYnVyZHMvQmlyZC5wa2wiCgpiaXJkOiBCaXJkID0gbmV3IHsKICBuYW1lID0gIlBlbmd1aW4iCiAgZmF2b3JpdGVGcnVpdCB7CiAgICBuYW1lID0gIkljZSBGcnVpdCIKICB9Cn0K"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user