From f7d9a9cf432fe5d9e41fa7f236437a0a86832fa0 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 14:45:13 -0700 Subject: [PATCH 01/13] Adds CI action validating new notebooks --- .github/workflows/validate_new_notebooks.yml | 29 ++++++++++++++ docs/core_docs/docs/how_to/testing.ipynb | 42 ++++++++++++++++++++ docs/core_docs/scripts/quarto-build.js | 2 +- package.json | 1 + 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/validate_new_notebooks.yml create mode 100644 docs/core_docs/docs/how_to/testing.ipynb diff --git a/.github/workflows/validate_new_notebooks.yml b/.github/workflows/validate_new_notebooks.yml new file mode 100644 index 000000000000..e1932e8ca7cb --- /dev/null +++ b/.github/workflows/validate_new_notebooks.yml @@ -0,0 +1,29 @@ +name: Validate new notebooks + +on: + push: + branches: + - main + pull_request: + branches: + - main + paths: + - 'docs/core_docs/**' + workflow_dispatch: + +jobs: + validate-new-notebooks: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v44 + - name: Validate new notebooks + run: | + new_notebooks=$(echo ${{ steps.changed-files.outputs.added_files }} | tr ' ' '\n' | grep '\.ipynb$') + if [ -n "$new_notebooks" ]; then + for notebook in $new_notebooks; do + yarn notebook:validate "$notebook" + done + fi \ No newline at end of file diff --git a/docs/core_docs/docs/how_to/testing.ipynb b/docs/core_docs/docs/how_to/testing.ipynb new file mode 100644 index 000000000000..fc014c0a0c8d --- /dev/null +++ b/docs/core_docs/docs/how_to/testing.ipynb @@ -0,0 +1,42 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "const thingy = \"mabob\";\n", + "\n", + "thingythingy;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "TypeScript", + "language": "typescript", + "name": "tslab" + }, + "language_info": { + "codemirror_mode": { + "mode": "typescript", + "name": "javascript", + "typescript": true + }, + "file_extension": ".ts", + "mimetype": "text/typescript", + "name": "typescript", + "version": "3.7.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/core_docs/scripts/quarto-build.js b/docs/core_docs/scripts/quarto-build.js index 9397d972b592..e502be91dd7d 100644 --- a/docs/core_docs/scripts/quarto-build.js +++ b/docs/core_docs/scripts/quarto-build.js @@ -2,7 +2,7 @@ const fs = require("node:fs"); const { glob } = require("glob"); const { execSync } = require("node:child_process"); -const IGNORED_CELL_REGEX = /```\w*?\n\/\/ ?@ls-docs-hide-cell\n[\s\S]*?```/g; +const IGNORED_CELL_REGEX = /```\w*?\n\/\/ ?@lc-docs-hide-cell\n[\s\S]*?```/g; async function main() { const allIpynb = await glob("./docs/**/*.ipynb"); diff --git a/package.json b/package.json index 7c45191ba7be..1f8124266a93 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "test:standard:int": "turbo test:standard:int", "test:standard": "yarn test:standard:unit && yarn test:standard:int", "example": "yarn workspace examples start", + "notebook:validate": "yarn workspace core_docs validate", "precommit": "turbo precommit", "docs": "yarn workspace core_docs start", "docs:api_refs": "yarn workspace api_refs start", From 36b51546b5b6ea50d79e4d494b5d2668c084187e Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 14:47:26 -0700 Subject: [PATCH 02/13] Update job --- .github/workflows/validate_new_notebooks.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/validate_new_notebooks.yml b/.github/workflows/validate_new_notebooks.yml index e1932e8ca7cb..25334ccd109c 100644 --- a/.github/workflows/validate_new_notebooks.yml +++ b/.github/workflows/validate_new_notebooks.yml @@ -1,5 +1,15 @@ name: Validate new notebooks +# If another push to the same PR or branch happens while this workflow is still running, +# cancel the earlier run in favor of the next run. +# +# There's no point in testing an outdated version of the code. GitHub only allows +# a limited number of job runners to be active at the same time, so it's better to cancel +# pointless jobs early so that more useful jobs can run sooner. +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: @@ -16,6 +26,13 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + cache: "yarn" + - name: Install dependencies + run: yarn install --immutable - name: Get changed files id: changed-files uses: tj-actions/changed-files@v44 From 656dc8857f2d2160accfaf3f9da815ebc43b2f97 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 14:51:30 -0700 Subject: [PATCH 03/13] Hard throw --- docs/core_docs/scripts/validate_notebook.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index d5483c8c2333..678df30a1bb0 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -73,6 +73,7 @@ const run = async () => { } } catch (e) { console.log(e); + throw e; } finally { try { await fs.rm(tempFilepath); From d2a0a4a05bcf3726a12154c60fce20c30ff34416 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 14:57:29 -0700 Subject: [PATCH 04/13] Fix script --- docs/core_docs/scripts/validate_notebook.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 678df30a1bb0..7a1b6c4197f9 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -71,9 +71,6 @@ const run = async () => { } }); } - } catch (e) { - console.log(e); - throw e; } finally { try { await fs.rm(tempFilepath); @@ -83,4 +80,7 @@ const run = async () => { } }; -run(); +run().catch((e) => { + console.error(e); + process.exit(1); +}); From 9a47c0e51b4b6942ec041aae3ad81c7bc12b7682 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:02:29 -0700 Subject: [PATCH 05/13] Make sync --- docs/core_docs/scripts/validate_notebook.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 7a1b6c4197f9..d7394bf51ec7 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -1,9 +1,8 @@ -import * as fs from "node:fs/promises"; +import * as fs from "node:fs"; import * as ts from "typescript"; -import { v4 as uuidv4 } from "uuid"; -export async function extract(filepath: string) { - const cells = JSON.parse((await fs.readFile(filepath)).toString()).cells; +export function extract(filepath: string) { + const cells = JSON.parse((fs.readFileSync(filepath)).toString()).cells; const code = cells .map((cell: Record) => { if (cell.cell_type === "code") { @@ -35,11 +34,11 @@ const run = async () => { try { const typescriptSource = await extract(pathname); try { - await fs.access("./tmp", fs.constants.F_OK); + await fs.existsSync("./tmp"); } catch (err) { - await fs.mkdir("./tmp"); + await fs.mkdirSync("./tmp"); } - await fs.writeFile(tempFilepath, typescriptSource); + await fs.writeFileSync(tempFilepath, typescriptSource); const program = ts.createProgram([tempFilepath], { module: ts.ModuleKind.NodeNext, moduleResolution: ts.ModuleResolutionKind.NodeNext, @@ -73,14 +72,11 @@ const run = async () => { } } finally { try { - await fs.rm(tempFilepath); + await fs.rmSync(tempFilepath); } catch (e) { // Do nothing } } }; -run().catch((e) => { - console.error(e); - process.exit(1); -}); +run(); From 595b2eac83e4f82b4341414755ba0db3ca28722b Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:03:12 -0700 Subject: [PATCH 06/13] Fail --- docs/core_docs/scripts/validate_notebook.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index d7394bf51ec7..06a9a5fcc09d 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -79,4 +79,8 @@ const run = async () => { } }; -run(); +try { + run(); +} catch { + process.exit(1); +} From 36f4d6481e8cdcf3f31c4f04f203615f8107ce65 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:08:01 -0700 Subject: [PATCH 07/13] Fix --- docs/core_docs/scripts/validate_notebook.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 06a9a5fcc09d..247d6b010559 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -32,13 +32,11 @@ const run = async () => { [pathname.split("/").length - 1].replace(".ipynb", ".mts"); const tempFilepath = `./tmp/${filename}`; try { - const typescriptSource = await extract(pathname); - try { - await fs.existsSync("./tmp"); - } catch (err) { - await fs.mkdirSync("./tmp"); + const typescriptSource = extract(pathname); + if (!fs.existsSync("./tmp")) { + fs.mkdirSync("./tmp"); } - await fs.writeFileSync(tempFilepath, typescriptSource); + fs.writeFileSync(tempFilepath, typescriptSource); const program = ts.createProgram([tempFilepath], { module: ts.ModuleKind.NodeNext, moduleResolution: ts.ModuleResolutionKind.NodeNext, @@ -72,7 +70,7 @@ const run = async () => { } } finally { try { - await fs.rmSync(tempFilepath); + fs.rmSync(tempFilepath); } catch (e) { // Do nothing } From 0b3bc4bd4d508c6748c49bd99d84319e1ab7ec69 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:08:49 -0700 Subject: [PATCH 08/13] Format --- docs/core_docs/scripts/validate_notebook.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 247d6b010559..878af7f607c9 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -2,7 +2,7 @@ import * as fs from "node:fs"; import * as ts from "typescript"; export function extract(filepath: string) { - const cells = JSON.parse((fs.readFileSync(filepath)).toString()).cells; + const cells = JSON.parse(fs.readFileSync(filepath).toString()).cells; const code = cells .map((cell: Record) => { if (cell.cell_type === "code") { From 2b4f459d8bd1ba5167ae5f19007e56336f21d109 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:15:25 -0700 Subject: [PATCH 09/13] Fix --- docs/core_docs/scripts/validate_notebook.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 878af7f607c9..a214420a49a3 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -45,6 +45,7 @@ const run = async () => { skipLibCheck: true, }); const diagnostics = ts.getPreEmitDiagnostics(program); + const issueStrings: string[] = []; if (diagnostics.length === 0) { console.log("No type errors found."); } else { @@ -56,18 +57,25 @@ const run = async () => { diagnostic.messageText, "\n" ); - console.log( + issueStrings.push( `${diagnostic.file.fileName} (${line + 1},${ character + 1 }): ${message}` ); } else { - console.log( + issueStrings.push( ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n") ); } }); } + if (issueStrings.length) { + const issues = issueStrings.join("\n"); + console.error(issues); + const err = new Error("Found type errors in new notebook:"); + (err as any).details = issues; + throw err; + } } finally { try { fs.rmSync(tempFilepath); From fc27c70a9768ea758258c193c1b18c636a8225f0 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:18:33 -0700 Subject: [PATCH 10/13] Fix --- docs/core_docs/scripts/validate_notebook.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index a214420a49a3..00ba61aa30d0 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -72,7 +72,7 @@ const run = async () => { if (issueStrings.length) { const issues = issueStrings.join("\n"); console.error(issues); - const err = new Error("Found type errors in new notebook:"); + const err = new Error("Found type errors in new notebook."); (err as any).details = issues; throw err; } From b464d86a21a2e41d83e6187a452482d770911baf Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:18:48 -0700 Subject: [PATCH 11/13] Remove test notebook --- docs/core_docs/docs/how_to/testing.ipynb | 42 ------------------------ 1 file changed, 42 deletions(-) delete mode 100644 docs/core_docs/docs/how_to/testing.ipynb diff --git a/docs/core_docs/docs/how_to/testing.ipynb b/docs/core_docs/docs/how_to/testing.ipynb deleted file mode 100644 index fc014c0a0c8d..000000000000 --- a/docs/core_docs/docs/how_to/testing.ipynb +++ /dev/null @@ -1,42 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "const thingy = \"mabob\";\n", - "\n", - "thingythingy;" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "TypeScript", - "language": "typescript", - "name": "tslab" - }, - "language_info": { - "codemirror_mode": { - "mode": "typescript", - "name": "javascript", - "typescript": true - }, - "file_extension": ".ts", - "mimetype": "text/typescript", - "name": "typescript", - "version": "3.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From b24a160d930d36895b0efa8f83ce1ef3d06ea8d9 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:23:34 -0700 Subject: [PATCH 12/13] Fix --- .github/workflows/validate_new_notebooks.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/validate_new_notebooks.yml b/.github/workflows/validate_new_notebooks.yml index 25334ccd109c..2079d7f661de 100644 --- a/.github/workflows/validate_new_notebooks.yml +++ b/.github/workflows/validate_new_notebooks.yml @@ -39,8 +39,11 @@ jobs: - name: Validate new notebooks run: | new_notebooks=$(echo ${{ steps.changed-files.outputs.added_files }} | tr ' ' '\n' | grep '\.ipynb$') + echo "New notebooks: $new_notebooks" if [ -n "$new_notebooks" ]; then for notebook in $new_notebooks; do yarn notebook:validate "$notebook" done + else + echo "No new notebooks to validate." fi \ No newline at end of file From aaab0858e2960105cd69406f2e00c5b3d9fe2ff5 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 29 Jul 2024 15:33:07 -0700 Subject: [PATCH 13/13] Fix --- .github/workflows/validate_new_notebooks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/validate_new_notebooks.yml b/.github/workflows/validate_new_notebooks.yml index 2079d7f661de..9bd7ae6b153a 100644 --- a/.github/workflows/validate_new_notebooks.yml +++ b/.github/workflows/validate_new_notebooks.yml @@ -38,7 +38,7 @@ jobs: uses: tj-actions/changed-files@v44 - name: Validate new notebooks run: | - new_notebooks=$(echo ${{ steps.changed-files.outputs.added_files }} | tr ' ' '\n' | grep '\.ipynb$') + new_notebooks=$(echo '${{ steps.changed-files.outputs.added_files }}' | tr ' ' '\n' | grep '\.ipynb$' || true) echo "New notebooks: $new_notebooks" if [ -n "$new_notebooks" ]; then for notebook in $new_notebooks; do