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");