Файли
openidea/src/features/run/moduleScope.ts
T
aconeshanaтаaconeshana 944b57dead fix(run): open a stack frame's file from the run module's scope (#6366)
Clicking a stack frame in the Run console opened another module's copy of a class when several modules of a multi-module Maven build declare the same package and class: the lookup went by file name, and the package check cannot tell same-package copies apart.

Candidates are now narrowed to the run configuration's module and the modules it depends on (transitively, all scopes) before the name and package checks. The module reaches the Run console links, the test tree's Jump to Source and the Debug console links. Without a Maven model, for an unlisted module, or when the scope matches no candidate, the lookup behaves as before.

Closes #6313

Co-authored-by: aconeshana <[email protected]>
2026-11-27 14:16:59 +08:00

73 рядки
3.1 KiB
TypeScript

/**
* Where a run's JVM stack frames can come from: the module a run configuration belongs to and
* the modules it depends on. A multi-module build compiles the same package and class name in
* several modules, so a frame's file name alone doesn't say which copy ran — only the run's
* classpath does.
*/
import type { BuildModel, MavenModel, MavenProject } from "@/platform/ipc";
/** Folders `dir` of the model's projects, as the IDE reports paths: absolute, `/`-separated. */
function absolute(root: string, dir: string): string {
const base = root.replace(/\/+$/, "");
const rest = dir.replace(/^\/+/, "").replace(/\/+$/, "");
return rest ? `${base}/${rest}` : base;
}
/** The project a run configuration's module refers to (`""` = the project root). */
function projectOf(projects: MavenProject[], root: string, module: string): MavenProject | undefined {
const wanted = module.startsWith("/") ? module.replace(/\/+$/, "") : absolute(root, module);
return projects.find((p) => absolute(root, p.dir) === wanted);
}
/**
* Folders (absolute) of the run module and its Maven module dependencies, transitively, or
* `null` when the model doesn't say — no build model, a module it doesn't list — in which case
* callers keep their name-only lookup.
*/
export function mavenModuleScope(
root: string | null,
module: string | null | undefined,
model: BuildModel | null,
): string[] | null {
const maven: MavenModel | null | undefined = model?.maven;
if (!root || !maven) return null;
// Linked builds carry another project's folders: their `dir` isn't relative to this root.
const projects = maven.projects.filter((p) => !p.projectRoot || p.projectRoot === root);
const start = projectOf(projects, root, module ?? "");
if (!start) return null;
const byCoordinates = new Map(projects.map((p) => [`${p.groupId}:${p.artifactId}`, p]));
const scope = new Set<MavenProject>([start]);
const queue = [start];
while (queue.length) {
const project = queue.pop()!;
for (const dep of project.dependencies) {
// Every scope counts: a test run compiles against test dependencies too, and a folder left
// out here would send a frame to another module's copy of the class.
const next = byCoordinates.get(`${dep.groupId}:${dep.artifactId}`);
if (next && !scope.has(next)) {
scope.add(next);
queue.push(next);
}
}
}
return [...scope].map((p) => absolute(root, p.dir));
}
/**
* `hits` narrowed to the run's module scope: a same-named file of a module the run doesn't
* depend on is another module's copy. Unchanged when the scope is unknown or matches nothing,
* so a lookup never gets worse than the name-only one it replaces.
*/
export function scopeToRunModule(
hits: string[],
root: string | null,
module: string | null | undefined,
model: BuildModel | null,
): string[] {
if (module == null) return hits;
const dirs = mavenModuleScope(root, module, model);
if (!dirs) return hits;
const scoped = hits.filter((path) => dirs.some((dir) => path.startsWith(`${dir}/`)));
return scoped.length ? scoped : hits;
}