Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -33,14 +34,15 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ResolutionRequest {
public class ResolutionRequest implements AutoCloseable {

private final List<URI> repos = new ArrayList<>();
private final List<Artifact> dependencies = new ArrayList<>();
private final List<Artifact> boms = new ArrayList<>();
private final Set<Coordinates> globalExclusions = new HashSet<>();
private boolean useUnsafeSharedCache;
private Path userHome;
private boolean usingTemporaryUserHome;
private boolean isUsingM2Local;

public ResolutionRequest addRepository(String uri) {
Expand Down Expand Up @@ -132,6 +134,7 @@ public ResolutionRequest replaceDependencies(Collection<Artifact> amended) {
getGlobalExclusions().forEach(toReturn::exclude);
toReturn.useUnsafeSharedCache = isUseUnsafeSharedCache();
toReturn.userHome = userHome;
toReturn.usingTemporaryUserHome = usingTemporaryUserHome;
toReturn.isUsingM2Local = isUsingM2Local();

return toReturn;
Expand Down Expand Up @@ -168,9 +171,11 @@ public Path getUserHome() {

if (isUseUnsafeSharedCache()) {
userHome = Paths.get(USER_HOME.value());
usingTemporaryUserHome = false;
} else {
try {
userHome = Files.createTempDirectory("resolver-home");
usingTemporaryUserHome = true;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -207,11 +212,34 @@ public Path getLocalCache(String resolver) {
return localRepo;
}

@Override
public void close() {
if (!usingTemporaryUserHome || userHome == null || !Files.exists(userHome)) {
return;
}

try (Stream<Path> files = Files.walk(userHome)) {
files.sorted(Comparator.reverseOrder()).forEach(this::deleteIfExists);
userHome = null;
usingTemporaryUserHome = false;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private void createDirectories(Path path) {
try {
Files.createDirectories(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private void deleteIfExists(Path path) {
try {
Files.deleteIfExists(path);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,16 @@ public void doMain(String[] args) {
Set<DependencyInfo> infos;
try (EventListener listener = HttpDownloader.defaultEventListener()) {
ResolverConfig config = new ResolverConfig(listener, args);
try (ResolutionRequest request = config.getResolutionRequest()) {
Resolver resolver = getResolver(config.getNetrc(), config.getMaxThreads(), listener);

ResolutionRequest request = config.getResolutionRequest();
ResolutionResult resolutionResult = resolver.resolve(request);

Resolver resolver = getResolver(config.getNetrc(), config.getMaxThreads(), listener);

ResolutionResult resolutionResult = resolver.resolve(request);

infos = fulfillDependencyInfos(resolver, listener, config, resolutionResult);

writeLockFile(listener, config, request, infos, resolutionResult.getConflicts());
writeDependencyIndex(config, infos);
infos = fulfillDependencyInfos(resolver, listener, config, resolutionResult);

writeLockFile(listener, config, request, infos, resolutionResult.getConflicts());
writeDependencyIndex(config, infos);
}
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
Expand Down
Binary file modified private/tools/prebuilt/lock_file_converter_deploy.jar
Binary file not shown.
16 changes: 16 additions & 0 deletions tests/com/github/bazelbuild/rules_jvm_external/resolver/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_java//java:java_library.bzl", "java_library")
load("@rules_java//java:java_test.bzl", "java_test")
load("//:defs.bzl", "artifact")

java_library(
Expand Down Expand Up @@ -38,3 +39,18 @@ java_library(
),
],
)

java_test(
name = "ResolutionRequestTest",
size = "small",
srcs = ["ResolutionRequestTest.java"],
test_class = "com.github.bazelbuild.rules_jvm_external.resolver.ResolutionRequestTest",
deps = [
"//private/tools/java/com/github/bazelbuild/rules_jvm_external/resolver",
artifact(
"junit:junit",
repository_name = "regression_testing_coursier",
),
artifact("org.hamcrest:hamcrest"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2026 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.github.bazelbuild.rules_jvm_external.resolver;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Test;

public class ResolutionRequestTest {

@Test
public void shouldDeleteTemporaryUserHomeOnCleanup() throws IOException {
ResolutionRequest request = new ResolutionRequest();

Path userHome = request.getUserHome();
Path marker = request.getLocalCache("maven").resolve("cleanup-marker");
Files.writeString(marker, "cleanup-me");

assertTrue(Files.exists(userHome));
assertTrue(Files.exists(marker));

request.close();

assertFalse(Files.exists(userHome));
}

@Test
public void shouldNotDeleteSharedUserHomeWhenUsingUnsafeCache() {
ResolutionRequest request = new ResolutionRequest();
request.useUnsafeSharedCache(true);

Path userHome = request.getUserHome();
request.close();

assertEquals(Paths.get(System.getProperty("user.home")), userHome);
assertTrue(Files.exists(userHome));
}
}