From ff7bf8a6d991d93a3644f167e47b8423c3bdf47c Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 2 Dec 2024 17:41:10 +0100 Subject: [PATCH] Prefer distribution/lib/Standard files when runEngineDistribution --- .../interpreter/runtime/ModuleSources.java | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/ModuleSources.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/ModuleSources.java index 804b9a937602..6b7d964f43a8 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/ModuleSources.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/ModuleSources.java @@ -3,6 +3,7 @@ import com.oracle.truffle.api.TruffleFile; import com.oracle.truffle.api.source.Source; import java.io.IOException; +import java.util.LinkedList; import org.enso.common.LanguageInfo; import org.enso.pkg.QualifiedName; import org.enso.text.buffer.Rope; @@ -68,7 +69,9 @@ ModuleSources ensureSource(QualifiedName name) throws IOException { Source src = Source.newBuilder(LanguageInfo.ID, rope.characters(), name.toString()).build(); return new ModuleSources(file, rope, src); } else if (file != null) { - Source src = Source.newBuilder(LanguageInfo.ID, file).build(); + var b = Source.newBuilder(LanguageInfo.ID, file); + assert alternativeFile(b, file) : "Cannot find alternative for " + file; + var src = b.build(); org.enso.text.buffer.Rope lit = Rope.apply(src.getCharacters().toString()); return new ModuleSources(file, lit, src); } @@ -83,4 +86,48 @@ ModuleSources ensureSource(QualifiedName name) throws IOException { String getPath() { return file() == null ? null : file().getPath(); } + + /** + * Special support for reassigning locations of sources when running development version. When + * {@code -ea} is enabled, then we try to locate library files under the {@code + * distribution}/{@code lib} directories. + */ + private static boolean alternativeFile(Source.SourceBuilder b, TruffleFile file) { + var root = file; + var stack = new LinkedList(); + while (root != null) { + if ("0.0.0-dev".equals(root.getName())) { + break; + } + stack.addFirst(root.getName()); + root = root.getParent(); + } + if (root == null) { + return true; + } + var libName = root.getParent(); + var libNamespace = libName.getParent(); + var libVersion = libNamespace.getParent().getParent(); + var builtDistribution = libVersion.getParent().getParent(); + if ("built-distribution".equals(builtDistribution.getName())) { + var repositoryRoot = builtDistribution.getParent(); + var distRoot = repositoryRoot.resolve("distribution").resolve("lib"); + var newLibRoot = + distRoot + .resolve(libNamespace.getName()) + .resolve(libName.getName()) + .resolve(root.getName()); + if (newLibRoot.isDirectory()) { + var newFile = newLibRoot; + for (var n : stack) { + newFile = newFile.resolve(n); + } + if (newFile.exists()) { + b.uri(newFile.toUri()); + return true; + } + } + } + return false; + } }