Skip to content

Commit

Permalink
when a module references a file attachment, include the file's lastMo…
Browse files Browse the repository at this point in the history
…dified timestamp in the module's hash.

closes #1836

Note that this allows us to remove from the tests the ts rewrite that we used to do on modules, because their payload’s lastModified is now always set to the fake currentDate injected by the tests. Some tests don't want to use the fake date though! (I hope they never run in parallel?)
  • Loading branch information
Fil committed Nov 22, 2024
1 parent 82922bd commit 8bf9182
Show file tree
Hide file tree
Showing 25 changed files with 88 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function readPages(root: string, md: MarkdownIt): Page[] {
return pages;
}

let currentDate: Date | null = null;
export let currentDate: Date | null = null;

/** For testing only! */
export function setCurrentDate(date: Date | null): void {
Expand Down
3 changes: 2 additions & 1 deletion src/javascript/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {extname, join} from "node:path/posix";
import type {Program} from "acorn";
import type {TransformOptions} from "esbuild";
import {transform, transformSync} from "esbuild";
import {currentDate} from "../config.js";
import {resolveJsrImport} from "../jsr.js";
import {resolveNodeImport} from "../node.js";
import {resolveNpmImport} from "../npm.js";
Expand Down Expand Up @@ -199,7 +200,7 @@ export function getFileInfo(root: string, path: string): FileInfo | undefined {
const stat = statSync(key);
if (!stat.isFile()) return; // ignore non-files
accessSync(key, constants.R_OK); // verify that file is readable
mtimeMs = Math.floor(stat.mtimeMs);
mtimeMs = Math.floor((currentDate ?? stat.mtimeMs) as number);
size = stat.size;
} catch {
fileInfoCache.delete(key); // delete stale entry
Expand Down
2 changes: 1 addition & 1 deletion src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class LoaderResolver {
getOutputFileHash(name: string): string {
const info = this.getOutputInfo(name);
if (!info) throw new Error(`output file not found: ${name}`);
return info.hash;
return createHash("sha256").update(info.hash).update(String(info.mtimeMs)).digest("hex");
}

getSourceInfo(name: string): FileInfo | undefined {
Expand Down
3 changes: 0 additions & 3 deletions test/build-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ class TestEffects extends FileBuildEffects {
contents = contents.replace(/^(\s*<script>\{).*(\}<\/script>)$/gm, "$1/* redacted init script */$2");
contents = contents.replace(/(registerFile\(.*,"lastModified":)\d+(,"size":\d+.*\))/gm, "$1/* ts */1706742000000$2"); // prettier-ignore
}
if (typeof contents === "string" && outputPath.endsWith(".js")) {
contents = contents.replace(/(FileAttachment\(.*,"lastModified":)\d+(,"size":\d+.*\))/gm, "$1/* ts */1706742000000$2"); // prettier-ignore
}
return super.writeFile(outputPath, contents);
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/input/build/params2/[code]/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {FileAttachment} from "npm:@observablehq/stdlib";

FileAttachment("data.json");

2 changes: 2 additions & 0 deletions test/input/build/params2/[code]/data.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
process.stdout.write(JSON.stringify({ a: 1 }));

5 changes: 5 additions & 0 deletions test/input/build/params2/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# test

```js
import "/code/analytics.js"
```
5 changes: 5 additions & 0 deletions test/loader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {mkdir, readFile, rm, stat, unlink, utimes, writeFile} from "node:fs/prom
import os from "node:os";
import {join} from "node:path/posix";
import {sort} from "d3-array";
import {setCurrentDate} from "../src/config.js";
import {clearFileInfo} from "../src/javascript/module.js";
import type {LoadEffects} from "../src/loader.js";
import {LoaderResolver} from "../src/loader.js";
Expand Down Expand Up @@ -118,6 +119,8 @@ describe("LoaderResolver.find(path)", () => {

describe("LoaderResolver.getSourceFileHash(path)", () => {
const time = new Date(Date.UTC(2023, 11, 1));
before(() => setCurrentDate(null));
after(() => setCurrentDate(new Date("2024-01-10T16:00:00")));
it("returns the content hash for the specified file’s data loader", async () => {
await utimes("test/input/build/archives.posix/dynamic.zip.sh", time, time);
await utimes("test/input/build/archives.posix/static.zip", time, time);
Expand All @@ -140,6 +143,8 @@ describe("LoaderResolver.get{Source,Output}Info(path)", () => {
const time1 = new Date(Date.UTC(2023, 11, 1));
const time2 = new Date(Date.UTC(2024, 2, 1));
const loaders = new LoaderResolver({root: "test"});
before(() => setCurrentDate(null));
after(() => setCurrentDate(new Date("2024-01-10T16:00:00")));
it("both return the last modification time for a simple file", async () => {
await utimes("test/input/loader/simple.txt", time1, time1);
assert.deepStrictEqual(loaders.getSourceInfo("input/loader/simple.txt"), {hash: "3b09aeb6f5f5336beb205d7f720371bc927cd46c21922e334d47ba264acb5ba4", mtimeMs: +time1, size: 6}); // prettier-ignore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {test} from "./test.a9a4ef0e.js";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import {FileAttachment} from "../_observablehq/stdlib.00000003.js";

export const test = FileAttachment({"name":"../test.txt","mimeType":"text/plain","path":"../_file/test.f2ca1bb6.txt","lastModified":/* ts */1706742000000,"size":5}, import.meta.url).text();
export const test = FileAttachment({"name":"../test.txt","mimeType":"text/plain","path":"../_file/test.f2ca1bb6.txt","lastModified":1704931200000,"size":5}, import.meta.url).text();
6 changes: 3 additions & 3 deletions test/output/build/data-loaders/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/import-test.e7269c4e.js">
<link rel="modulepreload" href="./_import/test.86a60bc6.js">
<link rel="modulepreload" href="./_import/import-test.3349a02d.js">
<link rel="modulepreload" href="./_import/test.a9a4ef0e.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
Expand All @@ -23,7 +23,7 @@
registerFile("./test.txt", {"name":"./test.txt","mimeType":"text/plain","path":"./_file/test.f2ca1bb6.txt","lastModified":/* ts */1706742000000,"size":5});

define({id: "05e74070", inputs: ["display"], outputs: ["test"], body: async (display) => {
const {test} = await import("./_import/import-test.e7269c4e.js");
const {test} = await import("./_import/import-test.3349a02d.js");

display(await test);
return {test};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {FileAttachment} from "../_observablehq/stdlib.00000003.js";
import * as Plot from "../_npm/@observablehq/[email protected]/cd372fb8.js";

export async function Chart() {
const gistemp = await FileAttachment({"name":"../lib/gistemp.csv","mimeType":"text/csv","path":"../_file/lib/gistemp.1cf298b1.csv","lastModified":/* ts */1706742000000,"size":97}, import.meta.url).csv({typed: true});
const gistemp = await FileAttachment({"name":"../lib/gistemp.csv","mimeType":"text/csv","path":"../_file/lib/gistemp.1cf298b1.csv","lastModified":1704931200000,"size":97}, import.meta.url).csv({typed: true});
return Plot.plot({
y: {grid: true},
color: {scheme: "burd"},
Expand Down
2 changes: 1 addition & 1 deletion test/output/build/embed/chart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "./_observablehq/stdlib.00000003.js";
import "./_npm/@observablehq/[email protected]/cd372fb8.js";
import "./_npm/[email protected]/cd372fb8.js";
export * from "./_import/chart.2ce91e05.js";
export * from "./_import/chart.4140747c.js";

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FileAttachment} from "../_observablehq/stdlib.00000003.js";
export {fooCsvData, fooJsonData} from "./foo/foo.666599bc.js";
export const topJsonData = await FileAttachment({"name":"../top-data.json","mimeType":"application/json","path":"../_file/top-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10}, import.meta.url).json();
export const topCsvData = await FileAttachment({"name":"../top-data.csv","mimeType":"text/csv","path":"../_file/top-data.24ef4634.csv","lastModified":/* ts */1706742000000,"size":72}, import.meta.url).text();
export {fooCsvData, fooJsonData} from "./foo/foo.0cc12e18.js";
export const topJsonData = await FileAttachment({"name":"../top-data.json","mimeType":"application/json","path":"../_file/top-data.67358ed8.json","lastModified":1704931200000,"size":10}, import.meta.url).json();
export const topCsvData = await FileAttachment({"name":"../top-data.csv","mimeType":"text/csv","path":"../_file/top-data.24ef4634.csv","lastModified":1704931200000,"size":72}, import.meta.url).text();
4 changes: 2 additions & 2 deletions test/output/build/fetches/foo.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/foo/foo.666599bc.js">
<link rel="modulepreload" href="./_import/foo/foo.0cc12e18.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
Expand All @@ -23,7 +23,7 @@
registerFile("./foo/foo-data.json", {"name":"./foo/foo-data.json","mimeType":"application/json","path":"./_file/foo/foo-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10});

define({id: "47a695da", inputs: ["display"], outputs: ["fooJsonData","fooCsvData"], body: async (display) => {
const {fooJsonData, fooCsvData} = await import("./_import/foo/foo.666599bc.js");
const {fooJsonData, fooCsvData} = await import("./_import/foo/foo.0cc12e18.js");

display(fooJsonData);
display(fooCsvData);
Expand Down
6 changes: 3 additions & 3 deletions test/output/build/fetches/top.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/top.c85e149a.js">
<link rel="modulepreload" href="./_import/foo/foo.666599bc.js">
<link rel="modulepreload" href="./_import/top.bacf54cc.js">
<link rel="modulepreload" href="./_import/foo/foo.0cc12e18.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
Expand All @@ -26,7 +26,7 @@
registerFile("./top-data.json", {"name":"./top-data.json","mimeType":"application/json","path":"./_file/top-data.67358ed8.json","lastModified":/* ts */1706742000000,"size":10});

define({id: "cb908c08", inputs: ["display"], outputs: ["fooCsvData","fooJsonData","topCsvData","topJsonData"], body: async (display) => {
const {fooCsvData, fooJsonData, topCsvData, topJsonData} = await import("./_import/top.c85e149a.js");
const {fooCsvData, fooJsonData, topCsvData, topJsonData} = await import("./_import/top.bacf54cc.js");

display(fooJsonData);
display(fooCsvData);
Expand Down
1 change: 1 addition & 0 deletions test/output/build/params2/_file/code/data.015abd7f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":1}
4 changes: 4 additions & 0 deletions test/output/build/params2/_import/code/analytics.06ccf9ff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {FileAttachment} from "../../_observablehq/stdlib.00000003.js";

FileAttachment({"name":"../../code/data.json","mimeType":"application/json","path":"../../_file/code/data.015abd7f.json","lastModified":1704931200000,"size":7}, import.meta.url);

Empty file.
Empty file.
Empty file.
Empty file.
46 changes: 46 additions & 0 deletions test/output/build/params2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="generator" content="Observable Framework v1.0.0-test">
<title>test</title>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preload" as="style" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
<link rel="preload" as="style" href="./_observablehq/theme-air,near-midnight.00000004.css">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css2?family=Source+Serif+4:ital,opsz,wght@0,8..60,200..900;1,8..60,200..900&amp;display=swap" crossorigin>
<link rel="stylesheet" type="text/css" href="./_observablehq/theme-air,near-midnight.00000004.css">
<link rel="modulepreload" href="./_observablehq/client.00000001.js">
<link rel="modulepreload" href="./_observablehq/runtime.00000002.js">
<link rel="modulepreload" href="./_observablehq/stdlib.00000003.js">
<link rel="modulepreload" href="./_import/code/analytics.06ccf9ff.js">
<script type="module">

import {define} from "./_observablehq/client.00000001.js";
import {registerFile} from "./_observablehq/stdlib.00000003.js";

registerFile("./code/data.json", {"name":"./code/data.json","mimeType":"application/json","path":"./_file/code/data.015abd7f.json","lastModified":/* ts */1706742000000,"size":7});

define({id: "d2a99c55", body: async () => {
const {} = await import("./_import/code/analytics.06ccf9ff.js");

}});

</script>
</head>
<body>
<div id="observablehq-center">
<aside id="observablehq-toc" data-selector="h1:not(:first-of-type)[id], h2:first-child[id], :not(h1) + h2[id]">
<nav>
</nav>
</aside>
<main id="observablehq-main" class="observablehq">
<h1 id="test" tabindex="-1"><a class="observablehq-header-anchor" href="#test">test</a></h1>
<div class="observablehq observablehq--block"><!--:d2a99c55:--></div>
</main>
<footer id="observablehq-footer">
<div>Built with <a href="https://observablehq.com/" target="_blank" rel="noopener noreferrer">Observable</a> on <a title="2024-01-10T16:00:00">Jan 10, 2024</a>.</div>
</footer>
</div>
</body>
</html>

0 comments on commit 8bf9182

Please sign in to comment.