Skip to content

Commit

Permalink
fix: Fixes dependent source discovery
Browse files Browse the repository at this point in the history
Parent directories for sources are now added to the compiler's source
path so any types referenced in them that are available in the same
directories can be found even when they are not explicitely mentioned
in any `//SOURCES` lines. This also fixes the problem where adding a
`//DEPS` line would cause the compiler to be unable to find those same
types.

Fixes jbangdev#1502
  • Loading branch information
quintesse committed Nov 7, 2022
1 parent 192972e commit b591089
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
3 changes: 3 additions & 0 deletions itests/extending/Bar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package extending;

public class Bar {}
10 changes: 10 additions & 0 deletions itests/extending/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///usr/bin/env jbang "$0" "$@" ; exit $?
///DEPS com.github.lalyos:jfiglet:0.0.9

package extending;

public class Foo extends Bar {
public static void main(String... args) {
System.out.println("hello JBang");
}
}
6 changes: 5 additions & 1 deletion src/main/java/dev/jbang/dependencies/ModularClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ public List<String> getClassPaths() {

public String getClassPath() {
if (classPath == null) {
classPath = String.join(CP_SEPARATOR, getClassPaths());
classPath = toClassPath(getClassPaths());
}

return classPath;
}

public static String toClassPath(List<String> pathElements) {
return String.join(CP_SEPARATOR, pathElements);
}

public String getManifestPath() {
if (manifestPath == null) {
manifestPath = artifacts.stream()
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/dev/jbang/source/buildsteps/CompileBuildStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import dev.jbang.cli.ExitException;
import dev.jbang.dependencies.MavenCoordinate;
import dev.jbang.dependencies.ModularClassPath;
import dev.jbang.source.Builder;
import dev.jbang.source.Project;
import dev.jbang.util.CommandBuffer;
Expand Down Expand Up @@ -42,10 +43,24 @@ protected Project compile() throws IOException {
optionList.addAll(project.getMainSourceSet().getCompileOptions());
String path = project.resolveClassPath().getClassPath();
if (!Util.isBlankString(path)) {
optionList.addAll(Arrays.asList("-classpath", path));
optionList.add("-classpath");
optionList.add(path);
}
optionList.addAll(Arrays.asList("-d", compileDir.toAbsolutePath().toString()));

// add -sourcepath for all source folders
List<String> srcDirs = project .getMainSourceSet()
.getSources()
.stream()
.map(x -> x.getFile().getParent().toAbsolutePath())
.distinct()
.map(Path::toString)
.collect(Collectors.toList());
if (!srcDirs.isEmpty()) {
optionList.add("-sourcepath");
optionList.add(ModularClassPath.toClassPath(srcDirs));
}

// add source files to compile
optionList.addAll(project .getMainSourceSet()
.getSources()
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/dev/jbang/source/generators/JshCmdGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.apache.commons.text.StringEscapeUtils;

import dev.jbang.dependencies.ModularClassPath;
import dev.jbang.source.*;
import dev.jbang.util.JavaUtil;
import dev.jbang.util.Util;
Expand Down Expand Up @@ -98,6 +99,19 @@ protected List<String> generateCommandLineList() throws IOException {
fullArgs.addAll(optionalArgs);

if (project.isJShell()) {
// add -sourcepath for all source folders
List<String> srcDirs = project .getMainSourceSet()
.getSources()
.stream()
.map(x -> x.getFile().getParent().toAbsolutePath())
.distinct()
.map(Path::toString)
.collect(Collectors.toList());
if (!srcDirs.isEmpty()) {
fullArgs.add("-C-sourcepath");
fullArgs.add(ModularClassPath.toClassPath(srcDirs));
}

ArrayList<ResourceRef> revSources = new ArrayList<>(project.getMainSourceSet().getSources());
Collections.reverse(revSources);
for (ResourceRef s : revSources) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/dev/jbang/source/TestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ void testAdditionalSourcesFolder() throws IOException {
Util.setCwd(examplesTestFolder);
String mainFile = examplesTestFolder.resolve("foo.java").toString();
String incFile = examplesTestFolder.resolve("bar/Bar.java").toString();
String srcPath = examplesTestFolder + File.pathSeparator + examplesTestFolder.resolve("bar");

ProjectBuilder pb = ProjectBuilder.create();
pb.additionalSources(Arrays.asList("bar"));
Expand All @@ -217,6 +218,7 @@ protected Builder<Project> getCompileBuildStep() {
protected void runCompiler(List<String> optionList) {
assertThat(optionList, hasItem(mainFile));
assertThat(optionList, hasItem(incFile));
assertThat(optionList, hasItems("-sourcepath", srcPath));
// Skip the compiler
}
};
Expand Down Expand Up @@ -490,4 +492,25 @@ protected void runNativeBuilder(List<String> optionList) throws IOException {
}
}.setFresh(true).build();
}

@Test
void testMultiSourceWithDeps() throws IOException {
Path fooDir = examplesTestFolder.resolve("extending").toAbsolutePath();
Path foo = fooDir.resolve("Foo.java");
ProjectBuilder pb = ProjectBuilder.create();
Project prj = pb.build(foo.toString());

new JavaSource.JavaAppBuilder(prj) {
@Override
protected Builder<Project> getCompileBuildStep() {
return new JavaCompileBuildStep() {
@Override
protected void runCompiler(List<String> optionList) {
assertThat(optionList, hasItems("-sourcepath", fooDir.toString(), foo.toString()));
// Skip the compiler
}
};
}
}.setFresh(true).build();
}
}

0 comments on commit b591089

Please sign in to comment.