Skip to content

Commit 3387b47

Browse files
committed
fix(manifest): resolve Maven modules inside their class realm
Maven resolves a project inside its class realm, where the artifact types its build extensions define are visible (LifecycleDependencyResolver). The facts extension resolved each module after the build, outside that realm, so such a type fell back to Maven's default handler: a type that includes its dependencies was traversed anyway, and its file extension was wrong for --with-files. Each module is now resolved inside its project's class realm. Resolves REA-1163.
1 parent 7bd93ab commit 3387b47

6 files changed

Lines changed: 147 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1313
- When `dotnet restore` fails in one .NET project, the projects it may have skipped as up to date are no longer reported as not restored.
1414
- Maven Socket facts now include every dependency edge Maven resolves, as Gradle and sbt facts do, so dependency chains show each path to a package. A dependency Maven puts on the runtime classpath is no longer marked as a dev dependency because Maven kept it under a test or provided dependency.
1515
- With `--ignore-unresolved`, a Maven module with unresolvable version conflicts no longer adds a partial dependency list.
16+
- Maven Socket facts honor artifact types that build extensions define, so they no longer list dependencies Maven doesn't resolve, or fetch files under the wrong extension.
1617

1718
## [1.14.1](https://github.com/SocketDev/socket-cli/releases/tag/v1.14.1) - 2026-11-06
1819

‎src/commands/manifest/scripts/maven-extension/src/main/java/tech/coana/socket/SocketFactsRecordsEngine.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,15 @@ public void run(MavenSession session, List<MavenProject> reactor, File rootDir,
168168
if (SocketSupport.isExcludedDir(excludeBase, module.getBasedir().toPath(), excludes)) continue;
169169
Map<String, Node> nodes = new LinkedHashMap<>();
170170
Set<String> directIds = new HashSet<>();
171-
collectModule(session, module, passingScopes, reactorGavs, populateGavs, opts, nodes, directIds, failures, unscannable);
171+
// As in Maven's LifecycleDependencyResolver: artifact types the build extensions contribute are
172+
// only found from inside the project's class realm.
173+
ClassLoader previous = Thread.currentThread().getContextClassLoader();
174+
if (module.getClassRealm() != null) Thread.currentThread().setContextClassLoader(module.getClassRealm());
175+
try {
176+
collectModule(session, module, passingScopes, reactorGavs, populateGavs, opts, nodes, directIds, failures, unscannable);
177+
} finally {
178+
Thread.currentThread().setContextClassLoader(previous);
179+
}
172180
rootIdx = emitModuleRoots(lines, rootIdx, projectKey(module), nodes, directIds);
173181
}
174182

‎src/commands/manifest/scripts/test/maven-compat/.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ descriptor-failure/*/target/
1919
unsolvable-conflict/records.tsv
2020
unsolvable-conflict/target/
2121
unsolvable-conflict/localrepo/
22+
extension-type/localrepo/
23+
extension-type/records.tsv
24+
extension-type/target/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>demo</groupId>
5+
<artifactId>extension-type</artifactId>
6+
<version>1.0</version>
7+
<properties>
8+
<stub.repo.url>file://${maven.multiModuleProjectDirectory}/localrepo</stub.repo.url>
9+
</properties>
10+
<repositories>
11+
<repository>
12+
<id>socket-stubs</id>
13+
<url>${stub.repo.url}</url>
14+
<releases>
15+
<enabled>true</enabled>
16+
<checksumPolicy>ignore</checksumPolicy>
17+
</releases>
18+
<snapshots>
19+
<enabled>false</enabled>
20+
</snapshots>
21+
</repository>
22+
</repositories>
23+
<pluginRepositories>
24+
<pluginRepository>
25+
<id>socket-stubs</id>
26+
<url>${stub.repo.url}</url>
27+
<releases>
28+
<enabled>true</enabled>
29+
<checksumPolicy>ignore</checksumPolicy>
30+
</releases>
31+
<snapshots>
32+
<enabled>false</enabled>
33+
</snapshots>
34+
</pluginRepository>
35+
</pluginRepositories>
36+
<!-- types-ext defines type fat: a jar that includes its dependencies, so Maven does not resolve
37+
demo.ext:inner below demo.ext:fat. -->
38+
<dependencies>
39+
<dependency>
40+
<groupId>demo.ext</groupId>
41+
<artifactId>fat</artifactId>
42+
<version>1.0</version>
43+
<type>fat</type>
44+
</dependency>
45+
</dependencies>
46+
<build>
47+
<extensions>
48+
<extension>
49+
<groupId>demo.ext</groupId>
50+
<artifactId>types-ext</artifactId>
51+
<version>1.0</version>
52+
</extension>
53+
</extensions>
54+
</build>
55+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env bash
2+
# Regression guard: an artifact type a build extension defines must apply as it does in Maven's own
3+
# resolution. Type fat, from the types-ext extension, includes its dependencies and is a jar on disk,
4+
# so demo.ext:inner below demo.ext:fat must not be emitted, and --with-files must fetch fat-1.0.jar.
5+
#
6+
# Usage: smoke-test-extension-type.sh <path-to-mvn> <path-to-extension-jar>
7+
set -euo pipefail
8+
HERE="$(cd "$(dirname "$0")" && pwd)"
9+
MVN="${1:?usage: smoke-test-extension-type.sh <mvn> <extension-jar>}"
10+
JAR="${2:?usage: smoke-test-extension-type.sh <mvn> <extension-jar>}"
11+
PROJECT="$HERE/extension-type"
12+
RECORDS="$PROJECT/records.tsv"
13+
STUBS="$PROJECT/localrepo"
14+
# shellcheck source=SCRIPTDIR/../compat-cache.sh
15+
. "$HERE/../compat-cache.sh"
16+
M2="$SOCKET_COMPAT_CACHE/m2"
17+
18+
bash "$HERE/../make-stub-repo.sh" "$STUBS" \
19+
'demo.ext:types-ext:1.0' \
20+
'demo.ext:fat:1.0+demo.ext:inner:1.0' \
21+
'demo.ext:inner:1.0'
22+
# The extension is a components.xml registering Maven's own handler class for type fat.
23+
EXT_DIR="$(mktemp -d)"
24+
trap 'rm -rf "$EXT_DIR"' EXIT
25+
mkdir -p "$EXT_DIR/META-INF/plexus"
26+
cat > "$EXT_DIR/META-INF/plexus/components.xml" <<'XML'
27+
<component-set>
28+
<components>
29+
<component>
30+
<role>org.apache.maven.artifact.handler.ArtifactHandler</role>
31+
<role-hint>fat</role-hint>
32+
<implementation>org.apache.maven.artifact.handler.DefaultArtifactHandler</implementation>
33+
<configuration>
34+
<type>fat</type>
35+
<extension>jar</extension>
36+
<language>java</language>
37+
<includesDependencies>true</includesDependencies>
38+
<addedToClasspath>true</addedToClasspath>
39+
</configuration>
40+
</component>
41+
</components>
42+
</component-set>
43+
XML
44+
( cd "$EXT_DIR" && jar cf "$STUBS/demo/ext/types-ext/1.0/types-ext-1.0.jar" META-INF )
45+
rm -rf "$M2/demo/ext/types-ext" "$M2/demo/ext/fat" "$M2/demo/ext/inner" "$RECORDS"
46+
47+
echo "+ $("$MVN" -v 2>/dev/null | head -1) (extension type)"
48+
( cd "$PROJECT" && "$MVN" --batch-mode -q \
49+
"-Dmaven.ext.class.path=$JAR" \
50+
-Dcoana.task=socket-facts \
51+
-Dsocket.withFiles=true \
52+
"-Dmaven.repo.local=$M2" \
53+
"-Dstub.repo.url=file://$STUBS" \
54+
"-Dsocket.recordsFile=$RECORDS" \
55+
validate )
56+
57+
python3 - "$RECORDS" <<'PY'
58+
import sys
59+
rows = [l.rstrip('\n').split('\t') for l in open(sys.argv[1]) if l.strip()]
60+
nodes = {r[2] for r in rows if r[0] == 'node'}
61+
files = {r[3] for r in rows if r[0] == 'file' and r[2].startswith('demo.ext:fat:')}
62+
failures = [r[1:] for r in rows if r[0] in ('failure', 'unscannable')]
63+
errors = []
64+
if not any(n.startswith('demo.ext:fat:') for n in nodes):
65+
errors.append(f"demo.ext:fat missing; nodes={sorted(nodes)}")
66+
if any(n.startswith('demo.ext:inner:') for n in nodes):
67+
errors.append("demo.ext:inner emitted, though type fat includes its dependencies")
68+
if not any(p.endswith('fat-1.0.jar') for p in files):
69+
errors.append(f"fat-1.0.jar not materialized; files={sorted(files)}")
70+
if failures:
71+
errors.append(f"no failures expected; got {failures}")
72+
if errors:
73+
print("FAIL:")
74+
for e in errors:
75+
print(" -", e)
76+
sys.exit(1)
77+
print("PASS (extension type): fat emitted as a jar, inner not resolved below it")
78+
PY

‎src/commands/manifest/scripts/test/run-compat.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ run_maven() {
118118
bash "$HERE/maven-compat/smoke-test-graph-failure.sh" "$dir/bin/mvn" "$jar"
119119
bash "$HERE/maven-compat/smoke-test-descriptor-failure.sh" "$dir/bin/mvn" "$jar"
120120
bash "$HERE/maven-compat/smoke-test-unsolvable-conflict.sh" "$dir/bin/mvn" "$jar"
121+
bash "$HERE/maven-compat/smoke-test-extension-type.sh" "$dir/bin/mvn" "$jar"
121122
done
122123
}
123124

0 commit comments

Comments
 (0)