From 39275369a0b64be55307bb8b8487d5b6cf6a84b1 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:33:13 -0700 Subject: [PATCH 01/20] Add custom GHA report for package size --- .github/workflows/ci.yml | 64 ++++++++----- scripts/checkPackageSize.mjs | 176 +++++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+), 23 deletions(-) create mode 100644 scripts/checkPackageSize.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2d52e2b6fa1a9..3e698c10eb3cd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,38 +89,15 @@ jobs: smoke: runs-on: ubuntu-latest - defaults: - run: - working-directory: ./pr steps: - uses: actions/checkout@v3 - with: - path: pr - - - uses: actions/checkout@v3 - with: - path: base - ref: ${{ github.base_ref }} - if: github.event_name == 'pull_request' - uses: actions/setup-node@v3 with: node-version: "*" check-latest: true - # Pre-build the base branch so we can check lib folder size changes. - # Note that github.sha points to a merge commit, meaning we're testing - # the base branch versus the base branch with the PR applied. - - name: Build base LKG - if: github.event_name == 'pull_request' - run: | - npm ci - npx hereby lkg - rm -rf $GITHUB_WORKSPACE/pr/lib - mv ./lib $GITHUB_WORKSPACE/pr/ - working-directory: ./base - - run: npm ci - run: npx hereby lkg @@ -177,6 +154,47 @@ jobs: node ./smoke.js typescript node ./smoke.js typescript/lib/tsserverlibrary + package_size: + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + + steps: + - uses: actions/checkout@v3 + with: + path: pr + + - uses: actions/checkout@v3 + with: + path: base + ref: ${{ github.base_ref }} + + - uses: actions/setup-node@v3 + with: + node-version: "*" + check-latest: true + + - run: npm ci + working-directory: ./pr + + - run: npm ci + working-directory: ./base + + - run: npx hereby lkg + working-directory: ./pr + + - run: npx hereby lkg + working-directory: ./base + + - run: node ./pr/scripts/checkPackageSize.mjs ./base > report.md + + - uses: LouisBrunner/checks-action@69aaabbcf32668b60dc03b65deabfe23e92d9c41 # v1.6.0 + if: always() + with: + token: ${{ secrets.GITHUB_TOKEN }} + name: Package size + conclusion: ${{ job.status }} + output_text_description_file: report.md + misc: runs-on: ubuntu-latest diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs new file mode 100644 index 0000000000000..72ef7eb3b50a0 --- /dev/null +++ b/scripts/checkPackageSize.mjs @@ -0,0 +1,176 @@ +import cp from "child_process"; +import path from "path"; +import url from "url"; + +const __filename = url.fileURLToPath(new URL(import.meta.url)); +const __dirname = path.dirname(__filename); + +const baseRepo = path.resolve(process.argv[3]); +const headRepo = path.resolve(__dirname, ".."); + +/** @type {{ size: number, unpackedSize: number; files: { path: string; size: number; }[]; }[]} */ +const [before, after] = JSON.parse(cp.execFileSync("npm", ["pack", "--dry-run", "--json", baseRepo, headRepo], { encoding: "utf8" })); + +/** @param {{ path: string; size: number; }[]} files */ +function filesToMap(files) { + return new Map(files.map(f => [f.path, f.size])); +} + +const beforeFiles = filesToMap(before.files); +const afterFiles = filesToMap(after.files); + +/** + * @param {number} before + * @param {number} after + */ +function failIfTooBig(before, after) { + if (after > (before * 1.1)) { + process.exitCode = 1; + } +} + +const units = ["B", "KiB", "MiB", "GiB"]; +/** + * @param {number} size + */ +function prettyPrintSize(size) { + const sign = Math.sign(size); + size = Math.abs(size); + + let i = 0; + while (size > 1024) { + i++; + size /= 1024; + } + + size *= sign; + return `${size.toFixed(2)} ${units[i]}`; +} + +/** + * @param {number} before + * @param {number} after + */ +function percentDiff(before, after) { + const percent = (after - before) / before; + return `${percent.toFixed(2)}%`; +} + +/** + * @param {string[]} header + * @param {string[][]} data + */ +function logTable(header, data) { + /** @type {string[]} */ + const lines = []; + const spacer = new Array(header.length).fill("-").join(" | ").trim(); + + /** + * @param {string[]} row + */ + function addRow(row, first = false) { + if (!first) { + lines.push(spacer); + } + lines.push(row.join(" | ").trim()); + } + + addRow(header, /*first*/ true); + for (const row of data) { + addRow(row); + } + + console.log(lines.join("\n")); +} + +console.log(`# Overall package size`); +console.log(); + +logTable( + ["", "Before", "After", "Diff", "Diff (percent)"], + [ + ["Packed", prettyPrintSize(before.size), prettyPrintSize(after.size), prettyPrintSize(after.size - before.size), percentDiff(before.size, after.size)], + ["Unpacked", prettyPrintSize(before.unpackedSize), prettyPrintSize(after.unpackedSize), prettyPrintSize(after.unpackedSize - before.unpackedSize), percentDiff(before.unpackedSize, after.unpackedSize)], + ] +); + +failIfTooBig(before.size, after.size); +failIfTooBig(before.unpackedSize, after.unpackedSize); + +console.log(); + + +/** @type {Map} */ +const fileCounts = new Map(); +const inBefore = -1; +const inAfter = 1; + +/** + * @param {Iterable} paths + * @param {-1 | 1} marker + */ +function addFiles(paths, marker) { + for (const p in paths) { + fileCounts.set(p, (fileCounts.get(p) ?? 0) + marker); + } +} +addFiles(beforeFiles.keys(), inBefore); +addFiles(afterFiles.keys(), inAfter); + +const allEntries = [...fileCounts.entries()]; +const commonFiles = allEntries.filter(([, count]) => count === 0).map(([path]) => path); +const beforeOnly = allEntries.filter(([, count]) => count === inBefore).map(([path]) => path); +const afterOnly = allEntries.filter(([, count]) => count === inAfter).map(([path]) => path); + + +console.log(`# Common files`); +console.log(); + +logTable( + ["File", "Before", "After", "Diff", "Diff (percent)"], + commonFiles.map(path => { + const beforeSize = beforeFiles.get(path) ?? 0; + const afterSize = afterFiles.get(path) ?? 0; + return { path, beforeSize, afterSize }; + }) + .filter(({ beforeSize, afterSize }) => beforeSize !== afterSize) + .map(({ path, beforeSize, afterSize }) => { + failIfTooBig(beforeSize, afterSize); + return [path, prettyPrintSize(beforeSize), prettyPrintSize(afterSize), prettyPrintSize(afterSize - beforeSize), percentDiff(beforeSize, afterSize)]; + }), +); + +console.log(); + + +console.log(`# New files`); +console.log(); + +logTable( + ["File", "Size"], + afterOnly.map(path => { + const afterSize = afterFiles.get(path) ?? 0; + return { path, afterSize }; + }) + .map(({ path, afterSize }) => { + return [path, prettyPrintSize(afterSize)]; + }), +); + +console.log(); + +console.log(`# Deleted files`); +console.log(); + +logTable( + ["File", "Size"], + beforeOnly.map(path => { + const afterSize = afterFiles.get(path) ?? 0; + return { path, afterSize }; + }) + .map(({ path, afterSize }) => { + return [path, prettyPrintSize(afterSize)]; + }), +); + +console.log(); From 1e55ee48b1f128776fb3ce6b1f733aae92ea5a12 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:36:15 -0700 Subject: [PATCH 02/20] Tweak output --- scripts/checkPackageSize.mjs | 91 +++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index 72ef7eb3b50a0..53908a3842305 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -126,51 +126,54 @@ const afterOnly = allEntries.filter(([, count]) => count === inAfter).map(([path console.log(`# Common files`); console.log(); -logTable( - ["File", "Before", "After", "Diff", "Diff (percent)"], - commonFiles.map(path => { - const beforeSize = beforeFiles.get(path) ?? 0; - const afterSize = afterFiles.get(path) ?? 0; - return { path, beforeSize, afterSize }; - }) - .filter(({ beforeSize, afterSize }) => beforeSize !== afterSize) - .map(({ path, beforeSize, afterSize }) => { - failIfTooBig(beforeSize, afterSize); - return [path, prettyPrintSize(beforeSize), prettyPrintSize(afterSize), prettyPrintSize(afterSize - beforeSize), percentDiff(beforeSize, afterSize)]; - }), -); - -console.log(); - - -console.log(`# New files`); -console.log(); - -logTable( - ["File", "Size"], - afterOnly.map(path => { - const afterSize = afterFiles.get(path) ?? 0; - return { path, afterSize }; - }) - .map(({ path, afterSize }) => { - return [path, prettyPrintSize(afterSize)]; - }), -); - +const commonData = commonFiles.map(path => { + const beforeSize = beforeFiles.get(path) ?? 0; + const afterSize = afterFiles.get(path) ?? 0; + return { path, beforeSize, afterSize }; +}) + .filter(({ beforeSize, afterSize }) => beforeSize !== afterSize) + .map(({ path, beforeSize, afterSize }) => { + failIfTooBig(beforeSize, afterSize); + return ["`" + path + "`", prettyPrintSize(beforeSize), prettyPrintSize(afterSize), prettyPrintSize(afterSize - beforeSize), percentDiff(beforeSize, afterSize)]; + }); + +if (commonData.length === 0) { + console.log("No change."); +} +else { + logTable(["File", "Before", "After", "Diff", "Diff (percent)"], commonData); +} console.log(); -console.log(`# Deleted files`); -console.log(); +if (afterOnly.length > 0) { + console.log(`# New files`); + console.log(); + logTable( + ["File", "Size"], + afterOnly.map(path => { + const afterSize = afterFiles.get(path) ?? 0; + return { path, afterSize }; + }) + .map(({ path, afterSize }) => { + return ["`" + path + "`", prettyPrintSize(afterSize)]; + }), + ); + console.log(); +} -logTable( - ["File", "Size"], - beforeOnly.map(path => { - const afterSize = afterFiles.get(path) ?? 0; - return { path, afterSize }; - }) - .map(({ path, afterSize }) => { - return [path, prettyPrintSize(afterSize)]; - }), -); -console.log(); +if (beforeOnly.length > 0) { + console.log(`# Deleted files`); + console.log(); + logTable( + ["File", "Size"], + beforeOnly.map(path => { + const afterSize = afterFiles.get(path) ?? 0; + return { path, afterSize }; + }) + .map(({ path, afterSize }) => { + return ["`" + path + "`", prettyPrintSize(afterSize)]; + }), + ); + console.log(); +} From 6b77f9fe9e0303b2cbabf03345b30c357c6a43c0 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:42:36 -0700 Subject: [PATCH 03/20] Fix oops --- .github/workflows/ci.yml | 7 +++++-- scripts/checkPackageSize.mjs | 8 ++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e698c10eb3cd..e1ea071ba14df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -154,7 +154,7 @@ jobs: node ./smoke.js typescript node ./smoke.js typescript/lib/tsserverlibrary - package_size: + package-size: runs-on: ubuntu-latest if: github.event_name == 'pull_request' @@ -185,7 +185,10 @@ jobs: - run: npx hereby lkg working-directory: ./base - - run: node ./pr/scripts/checkPackageSize.mjs ./base > report.md + - run: node ./pr/scripts/checkPackageSize.mjs ./base ./pr > report.md + + - run: cat report.md + if: always() - uses: LouisBrunner/checks-action@69aaabbcf32668b60dc03b65deabfe23e92d9c41 # v1.6.0 if: always() diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index 53908a3842305..7fbb3ada4061c 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -1,12 +1,8 @@ import cp from "child_process"; import path from "path"; -import url from "url"; -const __filename = url.fileURLToPath(new URL(import.meta.url)); -const __dirname = path.dirname(__filename); - -const baseRepo = path.resolve(process.argv[3]); -const headRepo = path.resolve(__dirname, ".."); +const baseRepo = path.resolve(process.argv[2]); +const headRepo = path.resolve(process.argv[3]); /** @type {{ size: number, unpackedSize: number; files: { path: string; size: number; }[]; }[]} */ const [before, after] = JSON.parse(cp.execFileSync("npm", ["pack", "--dry-run", "--json", baseRepo, headRepo], { encoding: "utf8" })); From 098a9dcbca8ba3e886950eb843f6d235f8247091 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:46:43 -0700 Subject: [PATCH 04/20] Fix up markdown --- scripts/checkPackageSize.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index 7fbb3ada4061c..c80ca0a459202 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -59,7 +59,7 @@ function percentDiff(before, after) { function logTable(header, data) { /** @type {string[]} */ const lines = []; - const spacer = new Array(header.length).fill("-").join(" | ").trim(); + const spacer = "| " + new Array(header.length).fill("-").join(" | ") + " |"; /** * @param {string[]} row @@ -68,7 +68,7 @@ function logTable(header, data) { if (!first) { lines.push(spacer); } - lines.push(row.join(" | ").trim()); + lines.push("| " + row.join(" | ") + " |"); } addRow(header, /*first*/ true); From f171b29c18745d9be11da63758a31e992f9c5f32 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:50:23 -0700 Subject: [PATCH 05/20] Perms --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1ea071ba14df..9b1a7510b70d4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -157,6 +157,8 @@ jobs: package-size: runs-on: ubuntu-latest if: github.event_name == 'pull_request' + permissions: + pull-requests: write steps: - uses: actions/checkout@v3 From 5dccf6d16c4bc0ff3171626cd981b9239e1749de Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:55:42 -0700 Subject: [PATCH 06/20] Perms again --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9b1a7510b70d4..33cd745cfe088 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -158,7 +158,7 @@ jobs: runs-on: ubuntu-latest if: github.event_name == 'pull_request' permissions: - pull-requests: write + checks: write steps: - uses: actions/checkout@v3 From c29a8218f4b5b6748289aed2d5c1f683617a7eea Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:57:56 -0700 Subject: [PATCH 07/20] Try both --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33cd745cfe088..3ef10801e53c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -158,6 +158,7 @@ jobs: runs-on: ubuntu-latest if: github.event_name == 'pull_request' permissions: + pull-requests: write checks: write steps: From 0ffac73ebb9aebce4efc82956f80c68235558963 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:15:59 -0700 Subject: [PATCH 08/20] Turns out github can do this --- .github/workflows/ci.yml | 16 +--------------- scripts/checkPackageSize.mjs | 11 +++++++---- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3ef10801e53c4..0d115a3f154f8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -157,9 +157,6 @@ jobs: package-size: runs-on: ubuntu-latest if: github.event_name == 'pull_request' - permissions: - pull-requests: write - checks: write steps: - uses: actions/checkout@v3 @@ -188,18 +185,7 @@ jobs: - run: npx hereby lkg working-directory: ./base - - run: node ./pr/scripts/checkPackageSize.mjs ./base ./pr > report.md - - - run: cat report.md - if: always() - - - uses: LouisBrunner/checks-action@69aaabbcf32668b60dc03b65deabfe23e92d9c41 # v1.6.0 - if: always() - with: - token: ${{ secrets.GITHUB_TOKEN }} - name: Package size - conclusion: ${{ job.status }} - output_text_description_file: report.md + - run: node ./pr/scripts/checkPackageSize.mjs ./base ./pr >> $GITHUB_STEP_SUMMARY misc: runs-on: ubuntu-latest diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index c80ca0a459202..7c9187928fd60 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -79,7 +79,10 @@ function logTable(header, data) { console.log(lines.join("\n")); } -console.log(`# Overall package size`); +console.log(`# Package size report`); +console.log(); + +console.log(`## Overall package size`); console.log(); logTable( @@ -119,7 +122,7 @@ const beforeOnly = allEntries.filter(([, count]) => count === inBefore).map(([pa const afterOnly = allEntries.filter(([, count]) => count === inAfter).map(([path]) => path); -console.log(`# Common files`); +console.log(`$# Common files`); console.log(); const commonData = commonFiles.map(path => { @@ -142,7 +145,7 @@ else { console.log(); if (afterOnly.length > 0) { - console.log(`# New files`); + console.log(`## New files`); console.log(); logTable( ["File", "Size"], @@ -159,7 +162,7 @@ if (afterOnly.length > 0) { if (beforeOnly.length > 0) { - console.log(`# Deleted files`); + console.log(`## Deleted files`); console.log(); logTable( ["File", "Size"], From 8775d3985c90275dd6332820b5a67ce0134e0d43 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:19:11 -0700 Subject: [PATCH 09/20] Improve look --- scripts/checkPackageSize.mjs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index 7c9187928fd60..c7f9691bb359f 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -59,19 +59,16 @@ function percentDiff(before, after) { function logTable(header, data) { /** @type {string[]} */ const lines = []; - const spacer = "| " + new Array(header.length).fill("-").join(" | ") + " |"; /** * @param {string[]} row */ - function addRow(row, first = false) { - if (!first) { - lines.push(spacer); - } + function addRow(row) { lines.push("| " + row.join(" | ") + " |"); } - addRow(header, /*first*/ true); + addRow(header); + addRow(new Array(header.length).fill("-")); for (const row of data) { addRow(row); } From 0bb4cac05467e8c62582cc3b49b035726b64c23f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:23:16 -0700 Subject: [PATCH 10/20] Try and link --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0d115a3f154f8..e1fe7da062397 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -187,6 +187,9 @@ jobs: - run: node ./pr/scripts/checkPackageSize.mjs ./base ./pr >> $GITHUB_STEP_SUMMARY + - run: echo "See $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for more info." + if: always() + misc: runs-on: ubuntu-latest From 93b329acb6761b64a239b1543c892d101063a959 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:24:43 -0700 Subject: [PATCH 11/20] Typo --- scripts/checkPackageSize.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index c7f9691bb359f..f948b04bed568 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -119,7 +119,7 @@ const beforeOnly = allEntries.filter(([, count]) => count === inBefore).map(([pa const afterOnly = allEntries.filter(([, count]) => count === inAfter).map(([path]) => path); -console.log(`$# Common files`); +console.log(`## Common files`); console.log(); const commonData = commonFiles.map(path => { From ca66f987fcb5e8c21a88f1dea25436c61d0066ae Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:30:53 -0700 Subject: [PATCH 12/20] Bad change for testing --- src/tsc/tsc.ts | 1 + src/typingsInstaller/nodeTypingsInstaller.ts | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index ce8b356b685d9..314973b5b100b 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -11,6 +11,7 @@ ts.Debug.loggingHost = { if (ts.Debug.isDebugging) { ts.Debug.enableDebugInfo(); + (globalThis as any).ts = ts; } if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) { diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 91be7a4b8c641..7076bba20969b 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -1,6 +1,7 @@ import * as fs from "fs"; import * as path from "path"; +import * as ts from "./_namespaces/ts"; import { combinePaths, createGetCanonicalFileName, @@ -36,6 +37,10 @@ import { TypingsInstaller, } from "./_namespaces/ts.server.typingsInstaller"; +if (ts.Debug.isDebugging) { + (globalThis as any).ts = ts; +} + class FileLog implements Log { constructor(private logFile: string | undefined) { } From a28199a652ae176244bc9ee8d9417bf351324b2f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:50:05 -0700 Subject: [PATCH 13/20] Many fixups --- scripts/checkPackageSize.mjs | 106 +++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index f948b04bed568..6c0a88c34757b 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -1,3 +1,4 @@ +import assert from "assert"; import cp from "child_process"; import path from "path"; @@ -12,8 +13,8 @@ function filesToMap(files) { return new Map(files.map(f => [f.path, f.size])); } -const beforeFiles = filesToMap(before.files); -const afterFiles = filesToMap(after.files); +const beforeFileToSize = filesToMap(before.files); +const afterFileToSize = filesToMap(after.files); /** * @param {number} before @@ -25,13 +26,19 @@ function failIfTooBig(before, after) { } } +/** + * @param {number} value + */ +function sign(value) { + return value > 0 ? "+" : "-"; +} + const units = ["B", "KiB", "MiB", "GiB"]; /** * @param {number} size */ function prettyPrintSize(size) { - const sign = Math.sign(size); - size = Math.abs(size); + assert(size >= 0); let i = 0; while (size > 1024) { @@ -39,7 +46,6 @@ function prettyPrintSize(size) { size /= 1024; } - size *= sign; return `${size.toFixed(2)} ${units[i]}`; } @@ -47,9 +53,18 @@ function prettyPrintSize(size) { * @param {number} before * @param {number} after */ -function percentDiff(before, after) { - const percent = (after - before) / before; - return `${percent.toFixed(2)}%`; +function prettyPrintSizeDiff(before, after) { + const diff = after - before; + return sign(diff) + prettyPrintSize(Math.abs(diff)); +} + +/** + * @param {number} before + * @param {number} after + */ +function prettyPercentDiff(before, after) { + const percent = 100 * (after - before) / before; + return `${sign(percent)}${Math.abs(percent).toFixed(2)}%`; } /** @@ -82,13 +97,30 @@ console.log(); console.log(`## Overall package size`); console.log(); -logTable( - ["", "Before", "After", "Diff", "Diff (percent)"], - [ - ["Packed", prettyPrintSize(before.size), prettyPrintSize(after.size), prettyPrintSize(after.size - before.size), percentDiff(before.size, after.size)], - ["Unpacked", prettyPrintSize(before.unpackedSize), prettyPrintSize(after.unpackedSize), prettyPrintSize(after.unpackedSize - before.unpackedSize), percentDiff(before.unpackedSize, after.unpackedSize)], - ] -); +if (before.size === after.size && before.unpackedSize === after.unpackedSize) { + console.log("No change."); +} +else { + logTable( + ["", "Before", "After", "Diff", "Diff (percent)"], + [ + [ + "Packed", + prettyPrintSize(before.size), + prettyPrintSize(after.size), + prettyPrintSizeDiff(before.size, after.size), + prettyPercentDiff(before.size, after.size), + ], + [ + "Unpacked", + prettyPrintSize(before.unpackedSize), + prettyPrintSize(after.unpackedSize), + prettyPrintSizeDiff(before.unpackedSize, after.unpackedSize), + prettyPercentDiff(before.unpackedSize, after.unpackedSize), + ], + ] + ); +} failIfTooBig(before.size, after.size); failIfTooBig(before.unpackedSize, after.unpackedSize); @@ -106,48 +138,49 @@ const inAfter = 1; * @param {-1 | 1} marker */ function addFiles(paths, marker) { - for (const p in paths) { + for (const p of paths) { fileCounts.set(p, (fileCounts.get(p) ?? 0) + marker); } } -addFiles(beforeFiles.keys(), inBefore); -addFiles(afterFiles.keys(), inAfter); +addFiles(beforeFileToSize.keys(), inBefore); +addFiles(afterFileToSize.keys(), inAfter); const allEntries = [...fileCounts.entries()]; const commonFiles = allEntries.filter(([, count]) => count === 0).map(([path]) => path); const beforeOnly = allEntries.filter(([, count]) => count === inBefore).map(([path]) => path); const afterOnly = allEntries.filter(([, count]) => count === inAfter).map(([path]) => path); - -console.log(`## Common files`); -console.log(); - const commonData = commonFiles.map(path => { - const beforeSize = beforeFiles.get(path) ?? 0; - const afterSize = afterFiles.get(path) ?? 0; + const beforeSize = beforeFileToSize.get(path) ?? 0; + const afterSize = afterFileToSize.get(path) ?? 0; return { path, beforeSize, afterSize }; }) .filter(({ beforeSize, afterSize }) => beforeSize !== afterSize) .map(({ path, beforeSize, afterSize }) => { - failIfTooBig(beforeSize, afterSize); - return ["`" + path + "`", prettyPrintSize(beforeSize), prettyPrintSize(afterSize), prettyPrintSize(afterSize - beforeSize), percentDiff(beforeSize, afterSize)]; + // failIfTooBig(beforeSize, afterSize); + return [ + "`" + path + "`", + prettyPrintSize(beforeSize), + prettyPrintSize(afterSize), + prettyPrintSizeDiff(beforeSize, afterSize), + prettyPercentDiff(beforeSize, afterSize), + ]; }); -if (commonData.length === 0) { - console.log("No change."); -} -else { - logTable(["File", "Before", "After", "Diff", "Diff (percent)"], commonData); +if (commonData.length > 0) { + console.log(`## Common files`); + console.log(); + logTable(["", "Before", "After", "Diff", "Diff (percent)"], commonData); + console.log(); } -console.log(); if (afterOnly.length > 0) { console.log(`## New files`); console.log(); logTable( - ["File", "Size"], + ["", "Size"], afterOnly.map(path => { - const afterSize = afterFiles.get(path) ?? 0; + const afterSize = afterFileToSize.get(path) ?? 0; return { path, afterSize }; }) .map(({ path, afterSize }) => { @@ -157,14 +190,13 @@ if (afterOnly.length > 0) { console.log(); } - if (beforeOnly.length > 0) { console.log(`## Deleted files`); console.log(); logTable( - ["File", "Size"], + ["", "Size"], beforeOnly.map(path => { - const afterSize = afterFiles.get(path) ?? 0; + const afterSize = afterFileToSize.get(path) ?? 0; return { path, afterSize }; }) .map(({ path, afterSize }) => { From 2aa2477a5880d4b010d12120a2c1220644c83e5f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 21:54:35 -0700 Subject: [PATCH 14/20] Tweak wording --- scripts/checkPackageSize.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index 6c0a88c34757b..f0e575c4941cf 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -168,7 +168,7 @@ const commonData = commonFiles.map(path => { }); if (commonData.length > 0) { - console.log(`## Common files`); + console.log(`## Files`); console.log(); logTable(["", "Before", "After", "Diff", "Diff (percent)"], commonData); console.log(); From 12c8a7b5f5762074f7582fbbff213422ec6e419a Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:01:29 -0700 Subject: [PATCH 15/20] Move URL into step so it auto opens to a link --- .github/workflows/ci.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e1fe7da062397..89b2c388e2ac5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -185,10 +185,9 @@ jobs: - run: npx hereby lkg working-directory: ./base - - run: node ./pr/scripts/checkPackageSize.mjs ./base ./pr >> $GITHUB_STEP_SUMMARY - - - run: echo "See $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for more info." - if: always() + - run: | + echo "See $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID for more info." + node ./pr/scripts/checkPackageSize.mjs ./base ./pr >> $GITHUB_STEP_SUMMARY misc: runs-on: ubuntu-latest From a3674e1ab5d482f803d6ca5a1b04e155c423287a Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:05:26 -0700 Subject: [PATCH 16/20] Revert "Bad change for testing" This reverts commit ca66f987fcb5e8c21a88f1dea25436c61d0066ae. --- src/tsc/tsc.ts | 1 - src/typingsInstaller/nodeTypingsInstaller.ts | 5 ----- 2 files changed, 6 deletions(-) diff --git a/src/tsc/tsc.ts b/src/tsc/tsc.ts index 314973b5b100b..ce8b356b685d9 100644 --- a/src/tsc/tsc.ts +++ b/src/tsc/tsc.ts @@ -11,7 +11,6 @@ ts.Debug.loggingHost = { if (ts.Debug.isDebugging) { ts.Debug.enableDebugInfo(); - (globalThis as any).ts = ts; } if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) { diff --git a/src/typingsInstaller/nodeTypingsInstaller.ts b/src/typingsInstaller/nodeTypingsInstaller.ts index 7076bba20969b..91be7a4b8c641 100644 --- a/src/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/typingsInstaller/nodeTypingsInstaller.ts @@ -1,7 +1,6 @@ import * as fs from "fs"; import * as path from "path"; -import * as ts from "./_namespaces/ts"; import { combinePaths, createGetCanonicalFileName, @@ -37,10 +36,6 @@ import { TypingsInstaller, } from "./_namespaces/ts.server.typingsInstaller"; -if (ts.Debug.isDebugging) { - (globalThis as any).ts = ts; -} - class FileLog implements Log { constructor(private logFile: string | undefined) { } From 241e1a3069bd17cec4b6922d496d08bdf218d8c4 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:08:08 -0700 Subject: [PATCH 17/20] Remove old LKG size comparison code --- Herebyfile.mjs | 14 +------------- scripts/build/utils.mjs | 18 ------------------ 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 8181655add479..0bdfa424cfe63 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -15,7 +15,7 @@ import { localizationDirectories } from "./scripts/build/localization.mjs"; import cmdLineOptions from "./scripts/build/options.mjs"; import { buildProject, cleanProject, watchProject } from "./scripts/build/projects.mjs"; import { localBaseline, localRwcBaseline, refBaseline, refRwcBaseline, runConsoleTests } from "./scripts/build/tests.mjs"; -import { Debouncer, Deferred, exec, getDiffTool, getDirSize, memoize, needsUpdate, readJson } from "./scripts/build/utils.mjs"; +import { Debouncer, Deferred, exec, getDiffTool, memoize, needsUpdate, readJson } from "./scripts/build/utils.mjs"; const glob = util.promisify(_glob); @@ -833,19 +833,7 @@ export const produceLKG = task({ throw new Error("Cannot replace the LKG unless all built targets are present in directory 'built/local/'. The following files are missing:\n" + missingFiles.join("\n")); } - /** @type {number | undefined} */ - let sizeBefore; - if (fs.existsSync("lib")) { - sizeBefore = getDirSize("lib"); - } await exec(process.execPath, ["scripts/produceLKG.mjs"]); - - if (sizeBefore !== undefined) { - const sizeAfter = getDirSize("lib"); - if (sizeAfter > (sizeBefore * 1.10)) { - throw new Error("The lib folder increased by 10% or more. This likely indicates a bug."); - } - } } }); diff --git a/scripts/build/utils.mjs b/scripts/build/utils.mjs index be8e862d9adc7..623886bd2df92 100644 --- a/scripts/build/utils.mjs +++ b/scripts/build/utils.mjs @@ -140,24 +140,6 @@ export function getDiffTool() { return program; } -/** - * Find the size of a directory recursively. - * Symbolic links can cause a loop. - * @param {string} root - * @returns {number} bytes - */ -export function getDirSize(root) { - const stats = fs.lstatSync(root); - - if (!stats.isDirectory()) { - return stats.size; - } - - return fs.readdirSync(root) - .map(file => getDirSize(path.join(root, file))) - .reduce((acc, num) => acc + num, 0); -} - /** * @template T */ From 3d1ae31c5a414b940ffe240b21024ed25e6421a3 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:08:24 -0700 Subject: [PATCH 18/20] One more unused import --- scripts/build/utils.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/build/utils.mjs b/scripts/build/utils.mjs index 623886bd2df92..2c8fe8c0c584b 100644 --- a/scripts/build/utils.mjs +++ b/scripts/build/utils.mjs @@ -4,7 +4,6 @@ import chalk from "chalk"; import { spawn } from "child_process"; import fs from "fs"; import JSONC from "jsonc-parser"; -import path from "path"; import which from "which"; /** From 73f77436cd4c486bdee13e9f2ef8612a7c3f0932 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 13 Mar 2023 22:10:46 -0700 Subject: [PATCH 19/20] Remove not-needed path.resolve --- scripts/checkPackageSize.mjs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index f0e575c4941cf..5eba031835a82 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -1,9 +1,8 @@ import assert from "assert"; import cp from "child_process"; -import path from "path"; -const baseRepo = path.resolve(process.argv[2]); -const headRepo = path.resolve(process.argv[3]); +const baseRepo = process.argv[2]; +const headRepo = process.argv[3]; /** @type {{ size: number, unpackedSize: number; files: { path: string; size: number; }[]; }[]} */ const [before, after] = JSON.parse(cp.execFileSync("npm", ["pack", "--dry-run", "--json", baseRepo, headRepo], { encoding: "utf8" })); From eec1bf96a7f3ac84da70cad2bb4920c7cd3a9465 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Tue, 14 Mar 2023 09:06:38 -0700 Subject: [PATCH 20/20] PR feedback --- scripts/checkPackageSize.mjs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/checkPackageSize.mjs b/scripts/checkPackageSize.mjs index 5eba031835a82..9614ca195842a 100644 --- a/scripts/checkPackageSize.mjs +++ b/scripts/checkPackageSize.mjs @@ -4,7 +4,7 @@ import cp from "child_process"; const baseRepo = process.argv[2]; const headRepo = process.argv[3]; -/** @type {{ size: number, unpackedSize: number; files: { path: string; size: number; }[]; }[]} */ +/** @type {Array<{ size: number, unpackedSize: number; files: Array<{ path: string; size: number; }>; }>} */ const [before, after] = JSON.parse(cp.execFileSync("npm", ["pack", "--dry-run", "--json", baseRepo, headRepo], { encoding: "utf8" })); /** @param {{ path: string; size: number; }[]} files */ @@ -156,7 +156,6 @@ const commonData = commonFiles.map(path => { }) .filter(({ beforeSize, afterSize }) => beforeSize !== afterSize) .map(({ path, beforeSize, afterSize }) => { - // failIfTooBig(beforeSize, afterSize); return [ "`" + path + "`", prettyPrintSize(beforeSize),