Skip to content

Commit

Permalink
Handle CI more cleanly
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed May 10, 2024
1 parent 18d1d83 commit c23ed70
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 51 deletions.
18 changes: 18 additions & 0 deletions .github/actions/setup-node-patched/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Set up patched Node

runs:
using: composite

steps:
- name: Set up patched node
shell: bash
run: |
set -exo pipefail
cd $GITHUB_ACTION_PATH
npm init --yes
npm install -D @zkochan/cmd-shim foreground-child
node setup.cjs
- name: Verify patched node
shell: bash
run: node -e 'if (process.getBuiltinModule) { console.log(process.getBuiltinModule) } else { throw new Error("process.getBuiltinModule is not available") }'
9 changes: 9 additions & 0 deletions .github/actions/setup-node-patched/node.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require("path");
const { foregroundChild } = require("foreground-child");

const argv = process.argv.slice(2);

const patch = path.resolve(__dirname, "patch.cjs");
process.env.NODE_OPTIONS = `--no-warnings=ExperimentalWarning --experimental-require-module --require ${patch}`;

foregroundChild(process.execPath, argv);
2 changes: 2 additions & 0 deletions .github/actions/setup-node-patched/patch.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {(name: string) => any} */
process.getBuiltinModule = name => require("module").isBuiltin(name) ? require(name) : undefined;
33 changes: 33 additions & 0 deletions .github/actions/setup-node-patched/setup.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
if (!process.version.startsWith("v22.")) {
throw new Error("This script is only intended to be used with Node.js v22");
}

const fs = require("fs");
const path = require("path");
const cmdShim = require("@zkochan/cmd-shim");

const bin = path.resolve(__dirname, "bin");
fs.mkdirSync(bin, { recursive: true });

const src = path.resolve(__dirname, "node.cjs");
const to = path.resolve(bin, "node");

async function main() {
await cmdShim(src, to, { nodeExecPath: process.execPath });

if (process.env.GITHUB_PATH) {
fs.appendFileSync(process.env.GITHUB_PATH, bin + "\n");
}

for (const f of fs.readdirSync(bin)) {
const contents = fs.readFileSync(path.join(bin, f), "utf8");
console.log(f);
console.log(contents);
console.log("-".repeat(80));
}
}

main().catch(e => {
console.error(e);
process.exitCode = 1;
});
47 changes: 4 additions & 43 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,21 @@ jobs:
node-version: ${{ matrix.node-version }}
check-latest: true

- run: |
echo "value=--no-warnings=ExperimentalWarning --experimental-require-module --require $(node -e 'console.log(path.resolve("patchProcessGetBuiltin.cjs"))')" >> "$GITHUB_OUTPUT"
name: Enable require(ESM) and process.getBuiltinModule polyfill
id: node-options
- name: Verify that process.getBuiltinModule is available
run: node -e 'if (process.getBuiltinModule) { console.log(process.getBuiltinModule) } else { throw new Error("process.getBuiltinModule is not available") }'
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
- uses: ./.github/actions/setup-node-patched

- run: npm ci

- name: Tests
id: test
# run tests, but lint separately
run: npm run test -- --no-lint --bundle=${{ matrix.bundle }}
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}

- name: Print baseline diff on failure
if: ${{ failure() && steps.test.conclusion == 'failure' }}
run: |
npx hereby baseline-accept
git add tests/baselines/reference
git diff --staged --exit-code
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
lint:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -153,10 +141,7 @@ jobs:
node-version: '*'
check-latest: true

- run: |
echo "value=--no-warnings=ExperimentalWarning --experimental-require-module --require $(node -e 'console.log(path.resolve("patchProcessGetBuiltin.cjs"))')" >> "$GITHUB_OUTPUT"
name: Enable require(ESM) and process.getBuiltinModule polyfill
id: node-options
- uses: ./.github/actions/setup-node-patched

- run: |
npm --version
Expand All @@ -167,16 +152,12 @@ jobs:
- run: npm ci

- run: npx hereby lkg
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
- run: |
node ./scripts/addPackageJsonGitHead.mjs package.json
npm pack
mv typescript*.tgz typescript.tgz
echo "package=$(pwd)/typescript.tgz" >> "$GITHUB_OUTPUT"
id: pack
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
- name: Smoke test
run: |
Expand All @@ -192,8 +173,6 @@ jobs:
node $GITHUB_WORKSPACE/scripts/checkModuleFormat.mjs typescript
node $GITHUB_WORKSPACE/scripts/checkModuleFormat.mjs typescript/lib/tsserverlibrary
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
package-size:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -265,27 +244,18 @@ jobs:
node-version: '22'
check-latest: true

- run: |
echo "value=--no-warnings=ExperimentalWarning --experimental-require-module --require $(node -e 'console.log(path.resolve("patchProcessGetBuiltin.cjs"))')" >> "$GITHUB_OUTPUT"
name: Enable require(ESM) and process.getBuiltinModule polyfill
id: node-options
- uses: ./.github/actions/setup-node-patched

- run: npm ci

- name: Build tsc
run: npx hereby tsc
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}

- name: Clean
run: npx hereby clean-src
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}

- name: Self build
run: npx hereby build-src --built
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}

baselines:
runs-on: ubuntu-latest
Expand All @@ -297,10 +267,7 @@ jobs:
node-version: '22'
check-latest: true

- run: |
echo "value=--no-warnings=ExperimentalWarning --experimental-require-module --require $(node -e 'console.log(path.resolve("patchProcessGetBuiltin.cjs"))')" >> "$GITHUB_OUTPUT"
name: Enable require(ESM) and process.getBuiltinModule polyfill
id: node-options
- uses: ./.github/actions/setup-node-patched

- run: npm ci

Expand All @@ -309,15 +276,11 @@ jobs:

- name: Run tests
run: npm test &> /dev/null || exit 0
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}

- name: Accept baselines
run: |
npx hereby baseline-accept
git add tests/baselines/reference
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
- name: Check baselines
id: check-baselines
Expand All @@ -336,8 +299,6 @@ jobs:
git diff --staged > fix_baselines.patch
exit 1
fi
env:
NODE_OPTIONS: ${{ steps.node-options.outputs.value }}
- name: Upload baseline diff artifact
if: ${{ failure() && steps.check-baselines.conclusion == 'failure' }}
Expand Down
9 changes: 1 addition & 8 deletions patchProcessGetBuiltin.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
const _module = require("module");

/** @type {(name: string) => any} */
function getBuiltinModule(name) {
if (!_module.isBuiltin(name)) return undefined;
return require(name);
}

process.getBuiltinModule = getBuiltinModule;
process.getBuiltinModule = name => require("module").isBuiltin(name) ? require(name) : undefined;

0 comments on commit c23ed70

Please sign in to comment.