Repository: apple/pkl
File: pkl-executor/src/main/java/org/pkl/executor/EmbeddedExecutor.java (Java)

Code before:
      }

      // convert to normal to allow running with a dev version
      version = Version.parse(executorSpi.getPklVersion()).toNormal();
    }

    Version getVersion() {
      return version;
    }

    String evaluatePath(Path modulePath, ExecutorOptions options) {
      var currentThread = Thread.currentThread();
      var prevContextClassLoader = currentThread.getContextClassLoader();
      // Truffle loads stuff from context class loader, so set it to our class loader
      currentThread.setContextClassLoader(pklDistributionClassLoader);
      try {
        return executorSpi.evaluatePath(modulePath, options.toSpiOptions());
      } catch (ExecutorSpiException e) {
        throw new ExecutorException(e.getMessage(), e.getCause(), executorSpi.getPklVersion());
      } finally {
        currentThread.setContextClassLoader(prevContextClassLoader);
      }
    }

    @Override
    public void close() throws IOException {
      pklDistributionClassLoader.close();
    }
  }

  private static final class PklDistributionClassLoader extends URLClassLoader {
    final ClassLoader pklExecutorClassLoader;

    static {
      registerAsParallelCapable();
    }

    PklDistributionClassLoader(Path pklFatJar, ClassLoader pklExecutorClassLoader) {
      // pass `null` to make bootstrap class loader the effective parent
      super(toUrls(pklFatJar), null);
      this.pklExecutorClassLoader = pklExecutorClassLoader;
    }

    @Override
    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
      synchronized (getClassLoadingLock(name)) {
        var clazz = findLoadedClass(name);

        if (clazz == null) {
          if (name.startsWith("org.pkl.executor.spi.")) {
            try {

Review comment (suggestion):
Good fix! But probably better to only catch `PklException`.

Also; softened the wording a bit (it's not so bad that we can't catch PklException directly)

```suggestion
        // This branch would ideally never be hit, but older Pkl releases (<0.27) erroneously throw
        // PklException in some cases.
        // Can't catch PklException directly because pkl-executor cannot depend on pkl-core.
        if (e.getClass().getName().equals("org.pkl.core.PklException")) {
          throw new ExecutorException(e.getMessage(), e.getCause());
        }
        throw e;
```

Code after:
      }

      // convert to normal to allow running with a dev version
      version = Version.parse(executorSpi.getPklVersion()).toNormal();
    }

    Version getVersion() {
      return version;
    }

    String evaluatePath(Path modulePath, ExecutorOptions options) {
      var currentThread = Thread.currentThread();
      var prevContextClassLoader = currentThread.getContextClassLoader();
      // Truffle loads stuff from context class loader, so set it to our class loader
      currentThread.setContextClassLoader(pklDistributionClassLoader);
      try {
        return executorSpi.evaluatePath(modulePath, options.toSpiOptions());
      } catch (ExecutorSpiException e) {
        throw new ExecutorException(e.getMessage(), e.getCause(), executorSpi.getPklVersion());
      } catch (RuntimeException e) {
        // This branch would ideally never be hit, but older Pkl releases (<0.27) erroneously throw
        // PklException in some cases.
        // Can't catch PklException directly because pkl-executor cannot depend on pkl-core.
        if (e.getClass().getName().equals("org.pkl.core.PklException")) {
          throw new ExecutorException(e.getMessage(), e.getCause());
        }
        throw e;
      } finally {
        currentThread.setContextClassLoader(prevContextClassLoader);
      }
    }

    @Override
    public void close() throws IOException {
      pklDistributionClassLoader.close();
    }
  }

  private static final class PklDistributionClassLoader extends URLClassLoader {
    final ClassLoader pklExecutorClassLoader;

    static {
      registerAsParallelCapable();
    }

    PklDistributionClassLoader(Path pklFatJar, ClassLoader pklExecutorClassLoader) {
      // pass `null` to make bootstrap class loader the effective parent
      super(toUrls(pklFatJar), null);
      this.pklExecutorClassLoader = pklExecutorClassLoader;
    }