From f38e1e6defe59c97d2acec2595bafe34ac97fc9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 18 Nov 2026 12:47:29 +0000 Subject: [PATCH] fix(studio-server): an inserted scene names its file from the project root, as the renderer reads it (#8150) * fix(studio-server): an inserted scene names its file from the project root, as the renderer reads it * test(studio-server): keep a test comment under the line limit --- .../src/helpers/compositionInsertion.test.ts | 44 +++++++++++++++++++ .../src/helpers/compositionInsertion.ts | 18 +++----- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/packages/studio-server/src/helpers/compositionInsertion.test.ts b/packages/studio-server/src/helpers/compositionInsertion.test.ts index b1666bfcb9..b92512a09e 100644 --- a/packages/studio-server/src/helpers/compositionInsertion.test.ts +++ b/packages/studio-server/src/helpers/compositionInsertion.test.ts @@ -167,6 +167,50 @@ describe("insertCompositionIntoSource", () => { expect(result.html).toContain('data-duration="5.9"'); }); + // fails without the fix: inserting into an open scene wrote the path from that scene's folder ("b.html"), which the + // renderer and the preview read from the project root, so the export said the scene was missing. + it("names the source from the project root when inserting into a scene in a folder", () => { + const dir = project(); + const scene = ``; + mkdirSync(join(dir, "compositions")); + mkdirSync(join(dir, "scenes")); + writeFileSync(join(dir, "compositions/a.html"), scene); + writeFileSync(join(dir, "compositions/b.html"), child); + writeFileSync(join(dir, "scenes/c.html"), child.replace('"headline"', '"other"')); + const insert = (sourcePath: string) => + insertCompositionIntoSource({ + projectDir: dir, + targetPath: "compositions/a.html", + sourcePath, + parentSource: scene, + start: 0, + desiredTrack: 0, + }).html; + + expect(insert("compositions/b.html")).toContain('data-composition-src="compositions/b.html"'); + expect(insert("scenes/c.html")).toContain('data-composition-src="scenes/c.html"'); + }); + + // fails without the fix: a source naming its own scene from the root was refused as missing. + it("follows a source's own scenes from the project root", () => { + const dir = project(); + writeFixture(dir); + mkdirSync(join(dir, "compositions")); + const nested = ``; + writeFileSync(join(dir, "compositions/wrap.html"), nested); + writeFileSync(join(dir, "compositions/inner.html"), child); + + const result = insertCompositionIntoSource({ + projectDir: dir, + targetPath: "index.html", + sourcePath: "compositions/wrap.html", + parentSource: parent, + start: 0, + desiredTrack: 0, + }); + expect(result.html).toContain('data-composition-src="compositions/wrap.html"'); + }); + it("rejects self-nesting, transitive cycles, missing files, and invalid durations", () => { const dir = project(); writeFixture(dir); diff --git a/packages/studio-server/src/helpers/compositionInsertion.ts b/packages/studio-server/src/helpers/compositionInsertion.ts index 5ece43dae7..af91900e22 100644 --- a/packages/studio-server/src/helpers/compositionInsertion.ts +++ b/packages/studio-server/src/helpers/compositionInsertion.ts @@ -1,7 +1,7 @@ import { existsSync } from "node:fs"; import { readProjectFile } from "@hyperframes/parsers/asset-resolution"; import { randomUUID } from "node:crypto"; -import { dirname, relative, resolve, sep } from "node:path"; +import { relative, sep } from "node:path"; import { parseHTML } from "linkedom"; import { ProjectRootMissingError } from "@hyperframes/core"; import { folderGone, isSafePath, realpath, resolveWithinProject } from "./safePath.js"; @@ -75,14 +75,6 @@ function canonicalProjectFile(projectDir: string, sourcePath: string): string { return canonicalProjectPath(projectDir, resolveWithinProject(projectDir, sourcePath)); } -function canonicalDependency(projectDir: string, ownerAbs: string, sourcePath: string): string { - validateSourcePath(sourcePath); - return canonicalProjectPath( - projectDir, - resolveWithinProject(projectDir, relative(projectDir, resolve(dirname(ownerAbs), sourcePath))), - ); -} - function validateDependencyGraph(projectDir: string, targetAbs: string, sourceAbs: string): void { const visited = new Set(); const visiting = new Set(); @@ -100,7 +92,7 @@ function validateDependencyGraph(projectDir: string, targetAbs: string, sourceAb for (const host of descendants(document, "[data-composition-src]")) { const dependency = host.getAttribute("data-composition-src"); if (dependency) { - visit(canonicalDependency(projectDir, file, dependency)); + visit(canonicalProjectFile(projectDir, dependency)); } } visiting.delete(file); @@ -165,8 +157,8 @@ function uniqueHostId(root: Element, base: string): string { return `${base}_${suffix}`; } -function relativeSourcePath(targetAbs: string, sourceAbs: string): string { - return relative(dirname(targetAbs), sourceAbs).split(sep).join("/"); +function rootSourcePath(projectDir: string, sourceAbs: string): string { + return relative(realpath(projectDir), sourceAbs).split(sep).join("/"); } export function insertCompositionIntoSource(input: { @@ -219,7 +211,7 @@ export function insertCompositionIntoSource(input: { host.setAttribute("data-hf-id", `hf-${randomUUID()}`); const mountedRootId = sourceCompositionId || hostId; host.setAttribute("data-composition-id", mountedRootId); - host.setAttribute("data-composition-src", relativeSourcePath(targetAbs, sourceAbs)); + host.setAttribute("data-composition-src", rootSourcePath(input.projectDir, sourceAbs)); host.setAttribute("data-start", String(Math.round(input.start * 100) / 100)); host.setAttribute("data-duration", String(duration)); host.setAttribute("data-playback-start", "0");