Skip to content

Commit 7eb0013

Browse files
fix(lint): a missing image, audio or mask file names the composition that references it (#4616)
* fix(lint): a missing image, audio or mask file names the composition that references it * test(cli): a missing file is reported once per composition that references it
1 parent 693c385 commit 7eb0013

3 files changed

Lines changed: 85 additions & 32 deletions

File tree

‎packages/cli/src/utils/lintProject.test.ts‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -514,21 +514,21 @@ describe("audio_src_not_found", () => {
514514
expect(finding).toBeDefined();
515515
});
516516

517-
it("deduplicates missing files across compositions", async () => {
517+
it("reports a missing file once per composition that references it", async () => {
518518
const project = makeProject(validHtmlWithAudio(), {
519519
"captions.html": validHtmlWithAudio("captions"),
520520
});
521521
// Both reference song.mp3 which doesn't exist
522522

523523
const { results } = await lintProject(project);
524524

525-
const first = results[0];
526-
expect(first).toBeDefined();
527-
const finding = first?.result.findings.find((f) => f.code === "audio_src_not_found");
528-
expect(finding).toBeDefined();
529-
// Should mention song.mp3 only once despite two references
530-
const occurrences = (finding?.message.match(/song\.mp3/g) ?? []).length;
531-
expect(occurrences).toBe(1);
525+
const findings = results
526+
.flatMap((entry) => entry.result.findings)
527+
.filter((f) => f.code === "audio_src_not_found");
528+
expect(findings.map((f) => f.file).sort()).toEqual(
529+
[join(project, "compositions", "captions.html"), join(project, "index.html")].sort(),
530+
);
531+
for (const finding of findings) expect(finding.message.match(/song\.mp3/g)).toHaveLength(1);
532532
});
533533

534534
it("resolves sub-composition src relative to the sub-composition file (../assets/...)", async () => {
@@ -715,7 +715,7 @@ describe("missing_local_asset", () => {
715715
expect(finding).toBeUndefined();
716716
});
717717

718-
it("deduplicates the same missing src across multiple compositions", async () => {
718+
it("reports the same missing src once per composition that references it", async () => {
719719
const project = makeProject(
720720
`<html><body>
721721
<div data-composition-id="main" data-width="1920" data-height="1080">
@@ -741,11 +741,17 @@ describe("missing_local_asset", () => {
741741

742742
const { results } = await lintProject(project);
743743

744-
const finding = results[0]?.result.findings.find((f) => f.code === "missing_local_asset");
745-
expect(finding).toBeDefined();
746-
// x.png mentioned only once despite three references
747-
const occurrences = (finding?.message.match(/x\.png/g) ?? []).length;
748-
expect(occurrences).toBe(1);
744+
const findings = results
745+
.flatMap((entry) => entry.result.findings)
746+
.filter((f) => f.code === "missing_local_asset");
747+
expect(findings.map((f) => f.file).sort()).toEqual(
748+
[
749+
join(project, "compositions", "scene-a.html"),
750+
join(project, "compositions", "scene-b.html"),
751+
join(project, "index.html"),
752+
].sort(),
753+
);
754+
for (const finding of findings) expect(finding.message.match(/x\.png/g)).toHaveLength(1);
749755
});
750756

751757
it("emits separate findings per tag type (img + video) for clear messaging", async () => {

‎packages/lint/src/project.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,40 @@ describe("video_media_start_at_or_past_eof", () => {
663663
});
664664
});
665665

666+
describe("missing asset findings name the file that references them", () => {
667+
it("points a draft section's missing image, audio and mask at the draft, not the film", async () => {
668+
const project = makeProject(
669+
`<html><body>
670+
<div data-composition-id="main" data-width="1920" data-height="1080" data-start="0" data-duration="10">
671+
<img src="assets/missing-in-film.png" />
672+
<div data-composition-src="compositions/draft.html" data-composition-id="draft" data-start="0" data-duration="5"></div>
673+
</div>
674+
</body></html>`,
675+
{
676+
"draft.html": `<!doctype html><html><body>
677+
<div data-composition-id="draft" data-width="1920" data-height="1080">
678+
<style>.masked { mask-image: url(../assets/missing-mask.png); }</style>
679+
<img src="../assets/missing-in-draft.png" />
680+
<audio src="../assets/missing-in-draft.mp3" data-start="0" data-duration="5"></audio>
681+
</div>
682+
</body></html>`,
683+
},
684+
);
685+
686+
const { results } = await lintProject(project);
687+
const fileOf = (code: string, name: string) =>
688+
results
689+
.flatMap((entry) => entry.result.findings)
690+
.find((finding) => finding.code === code && finding.message.includes(name))?.file;
691+
692+
const draft = join(project, "compositions", "draft.html");
693+
expect(fileOf("missing_local_asset", "missing-in-draft.png")).toBe(draft);
694+
expect(fileOf("audio_src_not_found", "missing-in-draft.mp3")).toBe(draft);
695+
expect(fileOf("texture_mask_asset_not_found", "missing-mask.png")).toBe(draft);
696+
expect(fileOf("missing_local_asset", "missing-in-film.png")).toBe(join(project, "index.html"));
697+
});
698+
});
699+
666700
describe("audio_src_not_found with templating tokens", () => {
667701
// A src carrying an unresolved templating placeholder is late-bound before render,
668702
// so the static linter cannot resolve it to a file and must not report it missing.

‎packages/lint/src/project.ts‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ function parseSubCompHtml(html: string): ParsableDocumentLike {
3535
return parseHTML(html).document as unknown as ParsableDocumentLike;
3636
}
3737

38+
/** The file a source was read from, as per-file findings carry it. */
39+
const sourceFile = (projectDir: string, compSrcPath?: string) =>
40+
resolve(projectDir, compSrcPath ?? "index.html");
41+
3842
interface HtmlSource {
3943
html: string;
4044
compSrcPath?: string;
@@ -357,7 +361,7 @@ function lintAudioSrcNotFound(
357361

358362
const audioSrcRe = mediaSrcTagRe("audio");
359363

360-
const missingSrcs: string[] = [];
364+
const missingByFile = new Map<string, Set<string>>();
361365
for (const { html, compSrcPath } of htmlSources) {
362366
let match: RegExpExecArray | null;
363367
while ((match = audioSrcRe.exec(html)) !== null) {
@@ -368,16 +372,18 @@ function lintAudioSrcNotFound(
368372
? rewriteAssetPath(compSrcPath, src, (path) => existsSync(join(projectDir, path)))
369373
: src;
370374
if (!existsSync(resolveProjectRelativeSrc(rootRelative, projectDir))) {
371-
missingSrcs.push(src);
375+
const file = sourceFile(projectDir, compSrcPath);
376+
missingByFile.set(file, (missingByFile.get(file) ?? new Set()).add(src));
372377
}
373378
}
374379
}
375380

376-
if (missingSrcs.length > 0) {
377-
const unique = [...new Set(missingSrcs)];
381+
for (const [file, srcs] of missingByFile) {
382+
const unique = [...srcs];
378383
findings.push({
379384
code: "audio_src_not_found",
380385
severity: "error",
386+
file,
381387
message: `<audio> element references file(s) not found in the project: ${unique.join(", ")}. The rendered video will be silent.`,
382388
fixHint:
383389
unique.length === 1
@@ -398,7 +404,10 @@ function lintMissingLocalAsset(
398404

399405
const localAssetSrcRe = mediaSrcTagRe("video|img|source");
400406

401-
const missingByTag = new Map<string, Map<string, string>>();
407+
const missingByTag = new Map<
408+
string,
409+
{ file: string; tagName: string; byResolved: Map<string, string> }
410+
>();
402411

403412
for (const { html, compSrcPath } of htmlSources) {
404413
const scannable = maskNonScannableRanges(html);
@@ -419,20 +428,23 @@ function lintMissingLocalAsset(
419428
if (resolvedAsset) continue;
420429

421430
const resolvedKey = resolve(projectDir, rootRelative);
422-
let bucket = missingByTag.get(tagName);
431+
const file = sourceFile(projectDir, compSrcPath);
432+
const bucketKey = `${file}\0${tagName}`;
433+
let bucket = missingByTag.get(bucketKey);
423434
if (!bucket) {
424-
bucket = new Map<string, string>();
425-
missingByTag.set(tagName, bucket);
435+
bucket = { file, tagName, byResolved: new Map<string, string>() };
436+
missingByTag.set(bucketKey, bucket);
426437
}
427-
if (!bucket.has(resolvedKey)) bucket.set(resolvedKey, src);
438+
if (!bucket.byResolved.has(resolvedKey)) bucket.byResolved.set(resolvedKey, src);
428439
}
429440
}
430441

431-
for (const [tagName, byResolved] of missingByTag) {
442+
for (const { file, tagName, byResolved } of missingByTag.values()) {
432443
const unique = [...byResolved.values()];
433444
findings.push({
434445
code: "missing_local_asset",
435446
severity: "error",
447+
file,
436448
message:
437449
`<${tagName}> element references local file(s) not found in the project: ${unique.join(", ")}. ` +
438450
"The renderer will silently skip these and produce a video with missing visuals.",
@@ -453,9 +465,10 @@ function lintTextureMaskAssetNotFound(
453465
projectDir: string,
454466
htmlSources: HtmlSource[],
455467
): HyperframeLintFinding[] {
456-
const missing = new Map<string, string>();
468+
const missingByFile = new Map<string, Set<string>>();
457469

458470
for (const { html, compSrcPath } of htmlSources) {
471+
const file = sourceFile(projectDir, compSrcPath);
459472
for (const cssSource of collectCssSources(projectDir, html, compSrcPath)) {
460473
let match: RegExpExecArray | null;
461474
const pattern = new RegExp(MASK_IMAGE_URL_RE.source, MASK_IMAGE_URL_RE.flags);
@@ -473,24 +486,24 @@ function lintTextureMaskAssetNotFound(
473486
cssSource.rootRelativePath,
474487
);
475488
if (candidates.some(existsSync)) continue;
476-
missing.set(url, candidates[0] ?? resolve(projectDir, url));
489+
missingByFile.set(file, (missingByFile.get(file) ?? new Set()).add(url));
477490
}
478491
}
479492
}
480493

481-
if (missing.size === 0) return [];
482-
const urls = [...missing.keys()];
483-
return [
484-
{
494+
return [...missingByFile].map(([file, found]): HyperframeLintFinding => {
495+
const urls = [...found];
496+
return {
485497
code: "texture_mask_asset_not_found",
486498
severity: "error",
499+
file,
487500
message: `CSS mask-image references file(s) not found in the project: ${urls.join(", ")}.`,
488501
fixHint:
489502
urls.length === 1
490503
? `Add "${urls[0]}" to the project, or update the mask-image URL to point to an existing texture mask.`
491504
: "Add the missing texture mask files to the project, or update the mask-image URLs to point to existing files.",
492-
},
493-
];
505+
};
506+
});
494507
}
495508

496509
function lintMultipleRootCompositions(projectDir: string): HyperframeLintFinding[] {

0 commit comments

Comments
 (0)