Skip to content

fix: Fixes dependent source discovery #1508

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
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");
}
}
8 changes: 7 additions & 1 deletion itests/test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ assert_stderr(){
export NL=$'\n'

echo "Cleaning JBANG_CACHE"
rm -rf ~/.jbang/cache
rm -rf ~/.jbang/cache/{jars,urls,scripts,stdins}

echo Testing with `which jbang`

Expand All @@ -61,6 +61,12 @@ assert_raises "rm $SCRATCH/test.java" 0

assert "jbang helloworld.java jbangtest" "Hello jbangtest"

echo "JAVA::::"
java -version
echo $JAVA_HOME
which java
which jshell

java -version 2>&1 >/dev/null| grep version | grep "1.8" >/dev/null
JAVA8=$?

Expand Down
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
4 changes: 4 additions & 0 deletions src/main/java/dev/jbang/source/ResourceRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public boolean isStdin() {
return originalResource != null && isStdin(originalResource);
}

public boolean isFile() {
return originalResource != null && Util.isValidPath(originalResource);
}

@Nullable
public Path getFile() {
return file;
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/dev/jbang/source/Source.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package dev.jbang.source;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import dev.jbang.cli.BaseCommand;
import dev.jbang.cli.ExitException;
Expand Down Expand Up @@ -87,6 +90,29 @@ public ResourceRef getResourceRef() {
return resourceRef;
}

@Nullable
public ResourceRef getSourceDirRef() {
if (contents != null && resourceRef.isFile()) {
Path parent = getResourceRef().getFile().getParent();
Optional<String> pkg = getJavaPackage();
if (pkg.isPresent()) {
String[] elems = pkg.get().split("\\.");
Collections.reverse(Arrays.asList(elems));
for (String elem : elems) {
if (parent != null && !elem.equals(parent.getFileName().toString())) {
// if path doesn't match package we return null
return null;
}
parent = parent.getParent();
}
}
return ResourceRef.forFile(parent);
} else {
// If the resource isn't a local file we return null
return null;
}
}

public Optional<String> getJavaPackage() {
if (contents != null) {
return Util.getSourcePackage(contents);
Expand Down Expand Up @@ -145,6 +171,10 @@ public Project updateProject(Project prj, ResourceResolver resolver) {
if (!prj.getMainSourceSet().getSources().contains(getResourceRef())) {
SourceSet ss = prj.getMainSourceSet();
ss.addSource(this.getResourceRef());
ResourceRef srcDir = getSourceDirRef();
if (srcDir != null) {
ss.addSourceDir(srcDir);
}
ss.addResources(tagReader.collectFiles(resourceRef,
new SiblingResourceResolver(resourceRef, ResourceResolver.forResources())));
ss.addDependencies(collectDependencies());
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/dev/jbang/source/SourceSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
Expand All @@ -21,6 +23,7 @@
public class SourceSet {
private final List<ResourceRef> sources = new ArrayList<>();
private final List<RefTarget> resources = new ArrayList<>();
private final Set<ResourceRef> sourceDirs = new HashSet<>();
private final List<String> dependencies = new ArrayList<>();
private final List<String> classPaths = new ArrayList<>();
private final List<String> compileOptions = new ArrayList<>();
Expand Down Expand Up @@ -67,6 +70,23 @@ public SourceSet addResources(Collection<RefTarget> resources) {
return this;
}

@Nonnull
public Set<ResourceRef> getSourceDirs() {
return Collections.unmodifiableSet(sourceDirs);
}

@Nonnull
public SourceSet addSourceDir(ResourceRef sourceDir) {
sourceDirs.add(sourceDir);
return this;
}

@Nonnull
public SourceSet addSourceDirs(Collection<ResourceRef> sourceDirs) {
this.sourceDirs.addAll(sourceDirs);
return this;
}

@Nonnull
public List<String> getDependencies() {
return Collections.unmodifiableList(dependencies);
Expand Down
15 changes: 14 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 @@ -41,10 +42,22 @@ 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()
.getSourceDirs()
.stream()
.map(d -> d.getFile().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
20 changes: 17 additions & 3 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 @@ -43,8 +44,10 @@ protected List<String> generateCommandLineList() throws IOException {
List<String> optionalArgs = new ArrayList<>();

String requestedJavaVersion = getProject().getJavaVersion();
String javacmd;
javacmd = JavaUtil.resolveInJavaHome("jshell", requestedJavaVersion);
if (requestedJavaVersion == null) {
requestedJavaVersion = "9+";
}
String jshcmd = JavaUtil.resolveInJavaHome("jshell", requestedJavaVersion);

// NB: See https://github.com/jbangdev/jbang/issues/992 for the reasons why we
// use the -J flags below
Expand Down Expand Up @@ -90,14 +93,25 @@ protected List<String> generateCommandLineList() throws IOException {
Util.warnMsg("Java Flight Recording not possible when running via jshell.");
}

fullArgs.add(javacmd);
fullArgs.add(jshcmd);
addAgentsArgs(fullArgs);

fullArgs.addAll(jshellOpts(project.getRuntimeOptions()));
fullArgs.addAll(project.resolveClassPath().getAutoDectectedModuleArguments(requestedJavaVersion));
fullArgs.addAll(optionalArgs);

if (project.isJShell()) {
// add -sourcepath for all source folders
List<String> srcDirs = project .getMainSourceSet()
.getSourceDirs()
.stream()
.map(d -> d.getFile().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
4 changes: 2 additions & 2 deletions src/test/java/dev/jbang/cli/TestRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ void testHelloWorldShellNoExit() throws IOException {

String result = prj.cmdGenerator().generate();

assertThat(result, startsWith("jshell"));
assertThat(result, matchesPattern("^.*jshell(.exe)?.*"));
assertThat(result, not(containsString(" ")));
assertThat(result, containsString("helloworld.jsh"));
assertThat(result, not(containsString("--source 11")));
Expand Down Expand Up @@ -662,7 +662,7 @@ void testDependenciesInteractive() throws IOException {

String result = prj.cmdGenerator().generate();

assertThat(result, startsWith("jshell "));
assertThat(result, matchesPattern("^.*jshell(.exe)?.*"));
assertThat(result, (containsString("classpath_example.java")));
// assertThat(result, containsString(" --source 11 "));
assertThat(result, not(containsString(" ")));
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/dev/jbang/source/TestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,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.resolve("bar") + File.pathSeparator + examplesTestFolder;

ProjectBuilder pb = ProjectBuilder.create();
pb.additionalSources(Arrays.asList("bar"));
Expand All @@ -239,6 +240,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 @@ -512,4 +514,24 @@ protected void runNativeBuilder(List<String> optionList) throws IOException {
}
}.setFresh(true).build();
}

@Test
void testMultiSourceWithDeps() throws IOException {
Path foo = examplesTestFolder.resolve("extending").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", examplesTestFolder.toString(), foo.toString()));
// Skip the compiler
}
};
}
}.setFresh(true).build();
}
}