Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always load Rascal modules and Java classes of std from the current rascal (attempt 2) #2112

Open
wants to merge 4 commits into
base: replace-lib-by-mvn-and-others
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion META-INF/RASCAL.MF
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Project-Name: rascal
Source: src/org/rascalmpl/library,test/org/rascalmpl/benchmark,test//org/rascalmpl/test/data
Source: src/org/rascalmpl/library,src/org/rascalmpl/typepal,test/org/rascalmpl/benchmark,test//org/rascalmpl/test/data
Courses: src/org/rascalmpl/courses


95 changes: 87 additions & 8 deletions src/org/rascalmpl/library/util/PathConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.jar.Manifest;

import org.rascalmpl.interpreter.Configuration;
Expand All @@ -26,6 +27,7 @@
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.URIUtil;
import org.rascalmpl.uri.file.MavenRepositoryURIResolver;
import org.rascalmpl.uri.jar.JarURIResolver;
import org.rascalmpl.values.IRascalValueFactory;
import org.rascalmpl.values.ValueFactoryFactory;

Expand Down Expand Up @@ -635,15 +637,22 @@ else if (rascalProject != null) {
messages.append(Messages.error(e.getMessage(), getRascalMfLocation(manifestRoot)));
}

try {
addRascalToSourcePath(srcsWriter);
} catch (IOException e) {
messages.append(Messages.error(e.getMessage(), getRascalMfLocation(manifestRoot)));
}

for (String srcName : manifest.getSourceRoots(manifestRoot)) {
var srcFolder = URIUtil.getChildLocation(manifestRoot, srcName);

if (!reg.exists(srcFolder) || !reg.isDirectory(srcFolder)) {
messages.append(Messages.error("Source folder " + srcFolder + " does not exist.", getRascalMfLocation(rascalProject)));
}
if (!projectName.equals("rascal")) { // Don't add `rascal` again
for (String srcName : manifest.getSourceRoots(manifestRoot)) {
var srcFolder = URIUtil.getChildLocation(manifestRoot, srcName);

if (!reg.exists(srcFolder) || !reg.isDirectory(srcFolder)) {
messages.append(Messages.error("Source folder " + srcFolder + " does not exist.", getRascalMfLocation(rascalProject)));
}

srcsWriter.append(srcFolder);
srcsWriter.append(srcFolder);
}
Comment on lines +647 to +655
Copy link
Contributor Author

@sungshik sungshik Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Exactly the same as the base branch, but indented because of the new outer conditional)

}

return new PathConfig(
Expand All @@ -655,6 +664,65 @@ else if (rascalProject != null) {
messages.done());
}

/**
* Adds the source folders of the `rascal` project to `srcsWriter`.
*
* For each source folder, there are at most two version to choose from:
* - The version in the "current" `rascal` (as per
* `resolveCurrentRascalRuntimeJar()`). Alway available.
* - The version in the "other" `rascal` (as per the
* `URIResolverRegistry`). Available when `rascal` itself is the main
* project, or when `rascal` is open in the workspace.
*
* The version of each source folder of the `rascal` project to add, is
* chosen based on the kind of that source folder:
* - Kind #1 (e.g., `.../library`, i.e., `|std:///|`): Add the version of
* the source folder in the current `rascal`
* - Kind #2 (e.g., `.../typepal`): Add the version of the source folder
* in the other `rascal` (if available) or the current `rascal` (else)
* - Kind #3 (e.g., `test/...`): Add the version of the source folder in
* the other `rascal` (if available)
*
* Thus, source folders of kinds #1 and #2 are added always, while source
* folders of kind #3 are added only when the `rascal` project itself is
* being developed.
*
* @throws IOException When the current `rascal` can't be resolved
*/
private static void addRascalToSourcePath(IListWriter srcsWriter) throws IOException {
var currentRascalTargetClasses = JarURIResolver.jarify(resolveCurrentRascalRuntimeJar());

// Assumption: If `rascal` itself is the main project, or if `rascal` is
// open in the workspace, then its root folder has already been
// registered as `|project://rascal/|` in the resolver registry (so
// checking the project name isn't needed after checking the registry).
var otherRascal = URIUtil.correctLocation("project", "rascal", "");
var otherRascalExists = URIResolverRegistry.getInstance().exists(otherRascal);

// Include source folders of kind #1
srcsWriter.append(URIUtil.rootLocation("std"));

// Include source folders of kind #2
if (otherRascalExists) {
srcsWriter.append(URIUtil.getChildLocation(otherRascal, "src/org/rascalmpl/typepal"));
} else {
srcsWriter.append(URIUtil.getChildLocation(currentRascalTargetClasses, "org/rascalmpl/typepal"));
}

// Include source folders of kind #3
if (otherRascalExists) {
Predicate<String> isKind3 = s ->
!s.equals("src/org/rascalmpl/library") &&
!s.equals("src/org/rascalmpl/typepal");

new RascalManifest()
.getSourceRoots(otherRascal)
.stream()
.filter(isKind3)
.forEach(src -> srcsWriter.append(URIUtil.getChildLocation(otherRascal, src)));
}
}

private static void addLibraryToSourcePath(URIResolverRegistry reg, IListWriter srcsWriter, IListWriter messages, ISourceLocation jar) {
if (!reg.exists(URIUtil.getChildLocation(jar, RascalManifest.META_INF_RASCAL_MF))) {
// skip all the non Rascal libraries
Expand Down Expand Up @@ -788,7 +856,18 @@ private static String computeMavenCommandName() {
*/
public void printInterpreterConfigurationStatus(PrintWriter out) {
out.println("Module paths:");
getSrcs().forEach((f) -> out.println(" ".repeat(4) + f));
getSrcs().forEach(f -> {
var l = (ISourceLocation) f;
var s = " ".repeat(4) + l;
if (l.getScheme().equals("std") || l.getScheme().equals("project")) {
try {
s += " at " + URIResolverRegistry.getInstance().logicalToPhysical(l);
} catch (IOException e) {
s += " at unknown physical location";
}
}
out.println(s);
});
out.println("JVM library classpath:");
getLibsAndTarget().forEach((l) -> out.println(" ".repeat(4) + l));
out.flush();
Expand Down
5 changes: 5 additions & 0 deletions src/org/rascalmpl/typepal/Typepal.rsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Typepal

// TODO: This is intended to be a temporary placeholder module (useful for
// testing `PathConfig` changes). It should be removed when `typepal` has been
// merged into `rascal`.