Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

annotate local paths #1731

Merged
merged 18 commits into from
Oct 28, 2024
Merged

annotate local paths #1731

merged 18 commits into from
Oct 28, 2024

Conversation

Fil
Copy link
Contributor

@Fil Fil commented Oct 8, 2024

paths to local files in transpiled JavaScript modules are postfixed with a comment

For example, when building the documentation, we now have in docs/.observablehq/dist/_import/chart.95e875cf.js

path: "../_file/lib/gistemp.08e51068.csv"/* observablehq-file */,

in lieu of

path: "../_file/lib/gistemp.08e51068.csv",

The annotate.js scripts lists a few cases that can be annotated.

* Annotate a path to a local import or file so it can be reworked server-side.
*/
function annotatePath(uri: string): string {
return !uri.startsWith(".") || /(?:[.][/]|(?:[.][.][/])+)(_jsr|_node|_npm|_observablehq)[/]/.test(uri)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be isAssetPath or whatever that’s ['./', '../', '/']. Don’t want to match against .foo.

@Fil Fil requested a review from mythmon October 10, 2024 16:48
@Fil Fil marked this pull request as ready for review October 10, 2024 16:48
@mythmon
Copy link
Member

mythmon commented Oct 15, 2024

Sorry for the delay. I plan to review this tomorrow.

src/javascript/transpile.ts Outdated Show resolved Hide resolved
@@ -210,11 +210,11 @@ describe("transpileModule(input, root, path)", () => {
it("rewrites npm imports", async () => {
const input = 'import "npm:d3-array";';
const output = (await transpileModule(input, options)).split("\n").pop()!;
assert.strictEqual(output, 'import "../_npm/[email protected]/_esm.js";');
assert.strictEqual(output, 'import "../_npm/[email protected]/_esm.js"/* observablehq-file */;');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this link is tagged as observable-file, then we should also rewrite it to tag Observable files in it. If we don't rewrite it, I don't think we should tag it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was initially doing that (see line 307 in 6cfb74d#diff-a479e6903ca67c840829983998e33849155911842cf08fa118b42b2d88fe3c9b), then I think we decided to tag everything and let the server decide what it should do with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is that this is a reference to an _npm file that is tagged, but references to _npm files from other _npm files are not tagged. As a concrete example, in the docs build there is this chain:

  1. dist/chart.js tags its import to the _import file:
  2. docs/.observablehq/dist/_import/chart.95e875cf.js tags its import of an _npm file:
  3. docs/.observablehq/dist/_npm/@observablehq/[email protected]/e828d8c8.js does not tag its import of the _npm file:
  4. ../../[email protected]/c68fbd73.js

It would be helpful if the tagging was done based on the taget of the URL, not the place the URL occurs. As it stands, the server can't really make the decision fully, because links to _npm files aren't consistent tagged/not tagged, so it has to special case them as not needing extra authentication.

test/output/build/data-loaders/_import/test.86a60bc6.js Outdated Show resolved Hide resolved
Copy link
Member

@mythmon mythmon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will work. Thanks!

@mythmon
Copy link
Member

mythmon commented Oct 25, 2024

@Fil is there anything more you want to do on this PR before merging it? It would be nice to have this in Framework's main branch for further development.

@Fil
Copy link
Contributor Author

Fil commented Oct 25, 2024

Not on my side; I just wanted to give Mike a chance to review.

Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be always on, or do we only want to turn it on when building for Cloud? I’d slightly prefer the latter and believe that was what we proposed (via environment variable)?

@Fil
Copy link
Contributor Author

Fil commented Oct 28, 2024

Annotations are now conditioned on the ANNOTATE_FILES=true environment variable. (Any other value throws an error.)

@mootari
Copy link
Member

mootari commented Oct 28, 2024

We should probably make that OBSERVABLE_ANNOTATE_FILES?

@Fil
Copy link
Contributor Author

Fil commented Oct 28, 2024

Sure, or OBSERVABLE_ANNOTATE for short. I don't have an opinion on this :)

@mythmon
Copy link
Member

mythmon commented Oct 28, 2024

I'd prefer if all environment variables that need to be set in Cloud Builds begin with OBSERVABLE_. I don't have a preference for OBSERVABLE_ANNOTATE vs OBSERVABLE_ANNOTATE_FILES.

@Fil Fil enabled auto-merge (squash) October 28, 2024 20:29
@Fil Fil merged commit b66e152 into main Oct 28, 2024
4 checks passed
@Fil Fil deleted the fil/annotate branch October 28, 2024 20:31
Copy link
Member

@mbostock mbostock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting a few things I plan on fixing.

"test:mocha:serial": "yarn test:build && rimraf --glob test/.observablehq/cache test/input/build/*/.observablehq/cache && cross-env OBSERVABLE_TELEMETRY_DISABLE=1 TZ=America/Los_Angeles mocha --timeout 30000 \"test/build/test/**/*-test.js\"",
"test:mocha": "yarn test:build && rimraf --glob test/.observablehq/cache test/input/build/*/.observablehq/cache && cross-env OBSERVABLE_TELEMETRY_DISABLE=1 TZ=America/Los_Angeles mocha --timeout 30000 -p \"test/build/test/**/*-test.js\" && yarn test:annotate",
"test:mocha:serial": "yarn test:build && rimraf --glob test/.observablehq/cache test/input/build/*/.observablehq/cache && cross-env OBSERVABLE_TELEMETRY_DISABLE=1 TZ=America/Los_Angeles mocha --timeout 30000 \"test/build/test/**/*-test.js\" && yarn test:annotate",
"test:annotate": "yarn test:build && cross-env OBSERVABLE_ANNOTATE_FILES=true TZ=America/Los_Angeles mocha --timeout 30000 \"test/build/test/**/annotate.js\"",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern breaks yarn test:mocha:serial with it.only or describe.only because it always runs the test:annotate in addition to your only’d tests.

const annotate = process.env["OBSERVABLE_ANNOTATE_FILES"];
if (typeof annotate === "string" && annotate !== "true")
throw new Error(`unsupported OBSERVABLE_ANNOTATE_FILES value: ${annotate}`);
export default annotate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t use default exports (because there’s no real reason to, and it encourages more inconsistency in how people name an imported symbol). This should be called annotatePath or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants