diff --git a/src/features/build-tools/index.ts b/src/features/build-tools/index.ts index e90ffc814b..5b4b26858c 100644 --- a/src/features/build-tools/index.ts +++ b/src/features/build-tools/index.ts @@ -317,10 +317,10 @@ export function activate(): () => void { id: "maven.importExternal", title: "Import Module from Existing Sources", description: - "Choose a module directory. A pom.xml is linked as a Maven project and a Gradle settings or build script as a Gradle build; a folder with neither is attached as another root. The opened folder stays the project root, and no build file is edited.", + "Choose a module directory. A pom.xml is linked as a Maven project and a Gradle settings or build script as a Gradle build; a folder with neither is attached as another root. In a window with several projects, the directory is attached as another project. The opened folder stays the project root, and no build file is edited.", category: "Maven", icon: "plus", - when: "projectOpen && !workspaceOpen", + when: "projectOpen", keywords: ["import", "module", "existing", "sources", "external", "maven", "pom", "gradle"], run: () => importModuleFromExistingSources(), }, diff --git a/src/features/build-tools/linkage.test.tsx b/src/features/build-tools/linkage.test.tsx index 2083a1b8cf..04d5eb8907 100644 --- a/src/features/build-tools/linkage.test.tsx +++ b/src/features/build-tools/linkage.test.tsx @@ -324,6 +324,38 @@ it("attaches a module directory without a build file outside the project, after expect(mocks.link).not.toHaveBeenCalled(); }); +it("keeps Import Module from Existing Sources in a window with attached projects and attaches the directory", async () => { + const attached = vi.fn(); + const off = registerAction({ id: "project.attachFolder", title: "Attach", run: ({ arg }) => attached(arg) }); + act(() => + useProject.setState({ + project: { root: "/p", workspace: { version: 1, name: "p", projects: ["/p", "/code/web"] } } as ProjectInfo, + }), + ); + setContext("workspaceOpen", true); + try { + expect(isActionEnabled(allActions().find((a) => a.id === "maven.importExternal")!)).toBe(true); + mocks.open.mockResolvedValue("/code/admin"); + await act(async () => { + await executeAction("maven.importExternal"); + }); + expect(mocks.open).toHaveBeenCalledWith(expect.objectContaining({ defaultPath: "/p" })); + expect(attached).toHaveBeenCalledWith("/code/admin"); + expect(mocks.confirm).not.toHaveBeenCalled(); + expect(mocks.set).not.toHaveBeenCalled(); + expect(mocks.link).not.toHaveBeenCalled(); + + attached.mockClear(); + mocks.open.mockResolvedValue("/code/web/src"); + await act(async () => { + await executeAction("maven.importExternal"); + }); + expect(attached).not.toHaveBeenCalled(); + } finally { + off(); + } +}); + it("doesn't offer to attach a folder where another root can't be attached (a remote window)", async () => { const attached = vi.fn(); const off = registerAction({ diff --git a/src/features/build-tools/store.ts b/src/features/build-tools/store.ts index 9c9cff25e2..83e333fd7d 100644 --- a/src/features/build-tools/store.ts +++ b/src/features/build-tools/store.ts @@ -489,14 +489,36 @@ export async function moduleImportKind( * inside the project is already part of it. */ export async function importModuleFromExistingSources() { - const root = useProject.getState().project?.root; - if (!root || useProject.getState().project?.workspace || !isTauri()) return; + const project = useProject.getState().project; + const root = project?.root; + if (!root || !isTauri()) return; const path = await choosePath({ title: tx`Import Module from Existing Sources`, directory: true, - defaultPath: root, + defaultPath: project.workspace?.projects[0] ?? root, }); if (!path) return; + // A window with several projects has no single build to link into: each attached root is a + // project with its own build, so the directory joins as another one, as Attach Project does. + if (project.workspace) { + const dir = lexicalPath(comparablePath(path)).replace(/\/$/, ""); + const inside = project.workspace.projects + .map((p) => lexicalPath(comparablePath(p)).replace(/\/$/, "")) + .find((p) => dir === p || isInsideRoot(p, dir)); + if (inside) { + const name = dir.split("/").pop() || dir; + notifications().notify({ + group: "tools", + level: "info", + title: tx`Already in the project`, + body: tx`${name} is inside the opened project, so its sources are already part of it.`, + key: "module-import-inside", + }); + return; + } + await executeAction("project.attachFolder", path); + return; + } const found = await moduleImportKind(path); if (!found) { notifications().notify({