Skip to content

Commit 276dcad

Browse files
committed
feat(manifest): list the sidecar classpath in JVM load order
projects[].classpath now gives each project's classpath in the order the JVM searches it, with one version per module, so the analysis can keep the first class per name instead of every copy. - Maven: the extension emits each module's classpath in the order Maven builds it from the resolved graph, test scope last. - Gradle: in --with-files runs, a configuration's nodes follow its artifact order, read after verification and only over fetched artifacts so nothing else is downloaded. - sbt: nodes already follow dependencyClasspath. - The assembler ranks roots runtime, compile, other variants, test, tooling, and keeps the first version of each module.
1 parent 547dc35 commit 276dcad

18 files changed

Lines changed: 568 additions & 45 deletions

File tree

‎.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ test/fixtures/commands/fix/e2e-test-py-temp-*
4040
/src/commands/manifest/scripts/test/gradle-compat/project/localrepo/
4141
/src/commands/manifest/scripts/test/gradle-compat/project/records.tsv
4242
/src/commands/manifest/scripts/test/gradle-compat/project/workspaces-records.tsv
43+
/src/commands/manifest/scripts/test/gradle-compat/order/.gradle/
44+
/src/commands/manifest/scripts/test/gradle-compat/order/build/
45+
/src/commands/manifest/scripts/test/gradle-compat/order/classpath.txt
46+
/src/commands/manifest/scripts/test/gradle-compat/order/localrepo/
47+
/src/commands/manifest/scripts/test/gradle-compat/order/records.tsv
4348
/src/commands/manifest/scripts/test/maven-compat/project/localrepo/
4449
/src/commands/manifest/scripts/test/maven-compat/project/records.tsv
4550
/src/commands/manifest/scripts/test/maven-compat/project/target/

‎src/commands/manifest/scripts/assemble.mts‎

Lines changed: 138 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
} from './facts.mts'
1111
import { moduleKey } from './resolution-report.mts'
1212

13-
import type { ParsedRecords, RawCoord, RawProject } from './records.mts'
13+
import type {
14+
ParsedRecords,
15+
RawCoord,
16+
RawProject,
17+
RawRoot,
18+
} from './records.mts'
1419
import type { ResolutionReport } from './resolution-report.mts'
1520

1621
const PURL_TYPE_MAVEN = 'maven'
@@ -44,7 +49,9 @@ type SubprojectGraph = {
4449

4550
type PerRoot = {
4651
projectKey: string
52+
config: string
4753
prod: boolean
54+
// In the root's classpath order.
4855
nodes: Map<string, RootNode>
4956
}
5057

@@ -93,6 +100,7 @@ export function assembleFacts(
93100
[...parsed.projects.values()],
94101
[...parsed.excludedProjects.values()],
95102
graphs,
103+
buildClasspaths(parsed, perRoot),
96104
fileExists,
97105
),
98106
}
@@ -139,14 +147,19 @@ function buildPerRoot(parsed: ParsedRecords): Map<string, PerRoot> {
139147
parent.children.push(childId)
140148
}
141149
}
142-
out.set(rootId, { projectKey: r.projectKey, prod: r.prod, nodes })
150+
out.set(rootId, {
151+
projectKey: r.projectKey,
152+
config: r.config,
153+
prod: r.prod,
154+
nodes,
155+
})
143156
}
144157
return out
145158
}
146159

147160
// Components are merged by id across every resolution root; which ids belong
148161
// to which subproject is kept separately (classpathByProject) for
149-
// reachability, which needs each subproject's exact classpath.
162+
// reachability.
150163
function mergeById(perRoot: Map<string, PerRoot>): Map<string, MergedNode> {
151164
const finalNodes = new Map<string, MergedNode>()
152165
for (const { nodes, prod } of perRoot.values()) {
@@ -395,24 +408,129 @@ function treeRoots(
395408
return roots
396409
}
397410

398-
function byProject(
399-
projects: RawProject[],
400-
graphs: Map<string, SubprojectGraph>,
401-
ids: (graph: SubprojectGraph) => Iterable<string>,
402-
): Map<string, string[]> {
403-
return new Map(
404-
projects.map(p => {
405-
const g = graphs.get(p.projectKey)
406-
return [p.projectKey, g ? [...ids(g)].sort() : []]
407-
}),
411+
// A test configuration: Gradle's `test…`/`…Test…` (testRuntimeClasspath,
412+
// integrationTestCompileClasspath, debugUnitTestRuntimeClasspath), sbt's
413+
// test and it configurations.
414+
const TEST_CONFIG = /^test|Test|^it(?:-internal)?$/
415+
const SBT_TEST_CONFIG = /^(?:test|it)(?:-internal)?$/
416+
417+
// Lower ranks go first; an unrecognised configuration counts as tooling.
418+
function classpathRank(config: string): number {
419+
const n = config.toLowerCase()
420+
// Gradle's legacy `runtime`/`compile` (before Gradle 7) are a subset of the
421+
// matching `…Classpath` configuration, so they rank just behind it.
422+
const legacy = n.endsWith('runtime') || n.endsWith('compile') ? 0.5 : 0
423+
const runtime = n.endsWith('runtimeclasspath') || n.endsWith('runtime')
424+
const compile = n.endsWith('compileclasspath') || n.endsWith('compile')
425+
if (TEST_CONFIG.test(config)) {
426+
if (runtime || SBT_TEST_CONFIG.test(n)) {
427+
return 4 + legacy
428+
}
429+
return compile ? 5 + legacy : 6
430+
}
431+
if (n === 'runtimeclasspath' || n === 'runtime' || n === 'runtime-internal') {
432+
return legacy
433+
}
434+
if (n === 'compileclasspath' || n === 'compile' || n === 'compile-internal') {
435+
return 1 + legacy
436+
}
437+
if (runtime) {
438+
return 2 + legacy
439+
}
440+
if (compile || n === 'provided' || n === 'optional') {
441+
return 3 + legacy
442+
}
443+
return 6
444+
}
445+
446+
// The JVM loads a class from the first classpath entry that has it, so the
447+
// first root by rank to resolve a module fixes its version.
448+
function deriveClasspath(roots: PerRoot[]): string[] {
449+
const ranked = [...roots].sort(
450+
(a, b) => classpathRank(a.config) - classpathRank(b.config),
408451
)
452+
const versionOf = new Map<string, string>()
453+
const out = new Set<string>()
454+
for (const { nodes } of ranked) {
455+
for (const { 0: id, 1: node } of nodes) {
456+
const module = node.project
457+
? `project ${node.project}`
458+
: `${node.coord.group}:${node.coord.name}`
459+
let version = versionOf.get(module)
460+
if (version === undefined) {
461+
version = node.coord.version
462+
versionOf.set(module, version)
463+
}
464+
if (version === node.coord.version) {
465+
out.add(id)
466+
}
467+
}
468+
}
469+
return [...out]
470+
}
471+
472+
// A project's classpath as its build tool states it, by component id.
473+
function statedClasspath(
474+
projectKey: string,
475+
stated: string[],
476+
roots: RawRoot[],
477+
): string[] {
478+
const ids = new Map<string, string>()
479+
for (const r of roots) {
480+
for (const { 0: coordId, 1: n } of r.nodes) {
481+
ids.set(coordId, componentId(coordId, n.project))
482+
}
483+
}
484+
const cp = new Set<string>()
485+
for (const coordId of stated) {
486+
const id = ids.get(coordId)
487+
if (id === undefined) {
488+
throw new Error(
489+
`The classpath of ${projectKey} names ${coordId}, which none of its resolution roots resolved`,
490+
)
491+
}
492+
cp.add(id)
493+
}
494+
return [...cp]
495+
}
496+
497+
function buildClasspaths(
498+
parsed: ParsedRecords,
499+
perRoot: Map<string, PerRoot>,
500+
): Map<string, string[]> {
501+
const rootIdsByProject = new Map<string, string[]>()
502+
for (const { 0: rootId, 1: r } of parsed.roots) {
503+
let list = rootIdsByProject.get(r.projectKey)
504+
if (!list) {
505+
list = []
506+
rootIdsByProject.set(r.projectKey, list)
507+
}
508+
list.push(rootId)
509+
}
510+
const out = new Map<string, string[]>()
511+
for (const projectKey of parsed.projects.keys()) {
512+
const rootIds = rootIdsByProject.get(projectKey) ?? []
513+
const stated = parsed.classpaths.get(projectKey)
514+
out.set(
515+
projectKey,
516+
stated
517+
? statedClasspath(
518+
projectKey,
519+
stated,
520+
rootIds.flatMap(id => parsed.roots.get(id) ?? []),
521+
)
522+
: deriveClasspath(rootIds.flatMap(id => perRoot.get(id) ?? [])),
523+
)
524+
}
525+
return out
409526
}
410527

411528
function buildArtifactPaths(
412529
finalNodes: Map<string, MergedNode>,
413530
projects: RawProject[],
414531
excludedProjects: RawProject[],
415532
graphs: Map<string, SubprojectGraph>,
533+
classpaths: Map<string, string[]>,
416534
fileExists: (path: string) => boolean,
417535
): ResolvedArtifactPaths {
418536
const pathsById: ResolvedArtifactPaths['pathsById'] = new Map()
@@ -454,8 +572,13 @@ function buildArtifactPaths(
454572
}
455573
return {
456574
pathsById,
457-
classpathByProject: byProject(projects, graphs, g => g.children.keys()),
458-
directDependenciesByProject: byProject(projects, graphs, g => g.direct),
575+
classpathByProject: classpaths,
576+
directDependenciesByProject: new Map(
577+
projects.map(p => {
578+
const g = graphs.get(p.projectKey)
579+
return [p.projectKey, g ? [...g.direct].sort() : []]
580+
}),
581+
),
459582
}
460583
}
461584

‎src/commands/manifest/scripts/assemble.test.mts‎

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('records → assemble → sidecar', () => {
100100
dependencies: ['com.example:bom:2.0', 'com.example:lib:jar:1.0'],
101101
targets: ['/abs/app/build/classes'],
102102
sources: ['/abs/app/src/main/java'],
103-
classpath: ['com.example:bom:2.0', 'com.example:lib:jar:1.0'],
103+
classpath: ['com.example:lib:jar:1.0', 'com.example:bom:2.0'],
104104
},
105105
])
106106

@@ -262,11 +262,11 @@ describe('records → assemble → sidecar', () => {
262262
p,
263263
]),
264264
)
265-
expect(byName.get('a')?.classpath).toEqual(['g:dep:jar:1', 'g:lib:jar:1'])
265+
expect(byName.get('a')?.classpath).toEqual(['g:lib:jar:1', 'g:dep:jar:1'])
266266
expect(byName.get('b')?.classpath).toEqual([
267+
'g:lib:jar:1',
267268
'g:dep:jar:2',
268269
'g:junit:jar:4',
269-
'g:lib:jar:1',
270270
])
271271
expect(byName.get('b')?.dependencies).toEqual([
272272
'g:junit:jar:4',
@@ -544,6 +544,148 @@ describe('records → assemble → sidecar', () => {
544544
})
545545
})
546546

547+
function classpath(records: string[], project: string): string[] | undefined {
548+
return assembleFacts(parseRecords(records.join('\n')), {
549+
fileExists: () => true,
550+
}).artifactPaths.classpathByProject.get(project)
551+
}
552+
553+
describe('classpath', () => {
554+
it("follows each root's order, runtime before compile-only, test and tooling roots", () => {
555+
expect(
556+
classpath(
557+
[
558+
'meta\tgradle\t8.10\t17',
559+
'project\t:app\tg\tapp\t1\tapp',
560+
'root\t0\t:app\tannotationProcessor\t0',
561+
'node\t0\tg:proc:jar:1\tg\tproc\t1\tjar\t\t1',
562+
'root\t1\t:app\tcompileClasspath\t1',
563+
'node\t1\tg:api:jar:1\tg\tapi\t1\tjar\t\t1',
564+
'node\t1\tg:servlet:jar:3\tg\tservlet\t3\tjar\t\t1',
565+
'root\t2\t:app\truntimeClasspath\t1',
566+
'node\t2\tg:z:jar:1\tg\tz\t1\tjar\t\t1',
567+
'node\t2\tg:api:jar:1\tg\tapi\t1\tjar\t\t1',
568+
'root\t3\t:app\ttestRuntimeClasspath\t0',
569+
'node\t3\tg:junit:jar:4\tg\tjunit\t4\tjar\t\t1',
570+
'node\t3\tg:z:jar:1\tg\tz\t1\tjar\t\t1',
571+
],
572+
':app',
573+
),
574+
).toEqual([
575+
'g:z:jar:1',
576+
'g:api:jar:1',
577+
'g:servlet:jar:3',
578+
'g:junit:jar:4',
579+
'g:proc:jar:1',
580+
])
581+
})
582+
583+
it('keeps the version of a module the highest-ranked root resolved, with its other artifacts', () => {
584+
expect(
585+
classpath(
586+
[
587+
'meta\tgradle\t8.10\t17',
588+
'project\t:app\tg\tapp\t1\tapp',
589+
'root\t0\t:app\ttestRuntimeClasspath\t0',
590+
'node\t0\tg:guava:jar:32\tg\tguava\t32\tjar\t\t1',
591+
'node\t0\tg:netty:jar:linux:4.1\tg\tnetty\t4.1\tjar\tlinux\t1',
592+
'root\t1\t:app\truntimeClasspath\t1',
593+
'node\t1\tg:guava:jar:30\tg\tguava\t30\tjar\t\t1',
594+
'node\t1\tg:netty:jar:4.1\tg\tnetty\t4.1\tjar\t\t1',
595+
],
596+
':app',
597+
),
598+
).toEqual(['g:guava:jar:30', 'g:netty:jar:4.1', 'g:netty:jar:linux:4.1'])
599+
})
600+
601+
it("ranks Gradle's legacy runtime and compile behind the classpath configurations", () => {
602+
expect(
603+
classpath(
604+
[
605+
'meta\tgradle\t6.9\t11',
606+
'project\t:app\tg\tapp\t1\tapp',
607+
'root\t0\t:app\tcompile\t1',
608+
'node\t0\tg:foo:jar:1\tg\tfoo\t1\tjar\t\t1',
609+
'root\t1\t:app\tcompileClasspath\t1',
610+
'node\t1\tg:bar:jar:1\tg\tbar\t1\tjar\t\t1',
611+
'root\t2\t:app\truntime\t1',
612+
'node\t2\tg:foo:jar:1\tg\tfoo\t1\tjar\t\t1',
613+
'root\t3\t:app\truntimeClasspath\t1',
614+
'node\t3\tg:bar:jar:1\tg\tbar\t1\tjar\t\t1',
615+
'node\t3\tg:foo:jar:2\tg\tfoo\t2\tjar\t\t0',
616+
'edge\t3\tg:bar:jar:1\tg:foo:jar:2',
617+
],
618+
':app',
619+
),
620+
).toEqual(['g:bar:jar:1', 'g:foo:jar:2'])
621+
})
622+
623+
it('tells test configurations by name segment, sbt it among them', () => {
624+
expect(
625+
classpath(
626+
[
627+
'meta\tgradle\t8.10\t17',
628+
'project\t:app\tg\tapp\t1\tapp',
629+
'root\t0\t:app\ttestRuntimeClasspath\t0',
630+
'node\t0\tg:x:jar:3\tg\tx\t3\tjar\t\t1',
631+
'root\t1\t:app\tlatestRuntimeClasspath\t1',
632+
'node\t1\tg:x:jar:2\tg\tx\t2\tjar\t\t1',
633+
],
634+
':app',
635+
),
636+
).toEqual(['g:x:jar:2'])
637+
expect(
638+
classpath(
639+
[
640+
'meta\tsbt\t1.9.9\t17',
641+
'project\tapp\tg\tapp\t1\tapp',
642+
'root\t0\tapp\tscala-tool\t0',
643+
'node\t0\tg:x:jar:2\tg\tx\t2\tjar\t\t1',
644+
'root\t1\tapp\tit\t0',
645+
'node\t1\tg:x:jar:1\tg\tx\t1\tjar\t\t1',
646+
],
647+
'app',
648+
),
649+
).toEqual(['g:x:jar:1'])
650+
})
651+
652+
it('takes a classpath the build tool states as it is, by component id', () => {
653+
expect(
654+
classpath(
655+
[
656+
'meta\tmaven\t3.9.6\t17',
657+
'project\tg:app:1\tg\tapp\t1\tapp',
658+
'project\tg:lib:1\tg\tlib\t1\tlib',
659+
'root\t0\tg:app:1\tcompile\t1',
660+
'node\t0\tg:z:jar:1\tg\tz\t1\tjar\t\t1',
661+
'node\t0\tg:lib:1\tg\tlib\t1\t\t\t1\tg:lib:1',
662+
'root\t1\tg:app:1\ttest\t0',
663+
'node\t1\tg:bcpkix:jar:1\tg\tbcpkix\t1\tjar\t\t1',
664+
'classpath\tg:app:1\tg:bcpkix:jar:1',
665+
'classpath\tg:app:1\tg:lib:1',
666+
'classpath\tg:app:1\tg:z:jar:1',
667+
],
668+
'g:app:1',
669+
),
670+
).toEqual(['g:bcpkix:jar:1', 'g:lib:1', 'g:z:jar:1'])
671+
})
672+
673+
it('rejects a stated classpath entry that no root of the project resolved', () => {
674+
expect(() =>
675+
classpath(
676+
[
677+
'meta\tmaven\t3.9.6\t17',
678+
'project\tg:app:1\tg\tapp\t1\tapp',
679+
'root\t0\tg:app:1\tcompile\t1',
680+
'node\t0\tg:z:jar:1\tg\tz\t1\tjar\t\t1',
681+
'classpath\tg:app:1\tg:y:jar:1',
682+
],
683+
'g:app:1',
684+
),
685+
).toThrow(/names g:y:jar:1/)
686+
})
687+
})
688+
547689
describe('parseRecords', () => {
548690
it('reads the build root the build reports', () => {
549691
expect(

0 commit comments

Comments
 (0)