From d0af42d9d96cdf903618a85b821359337588c546 Mon Sep 17 00:00:00 2001 From: Elliot Goodrich Date: Sun, 7 Jul 2024 12:55:08 +0100 Subject: [PATCH] Fix global variables in `@ninjutsu-build/node/runtime` Replace global variabls with `global` instead as this was causing issues with variables being reinitialized with `undefined`. Additionally, change `addDependency` to do nothing if `open` hasn't yet been called because it is easier to debug and there will be many situations where projects may want to try out `ninjutsu-build` in parallel to their existing system. This allows them to add in `addDependency` where expected, but have this be a no-op in their existing build. --- packages/node/package.json | 2 +- packages/node/src/depfile.cts | 40 ++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/packages/node/package.json b/packages/node/package.json index a7627b6..e63e520 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -1,6 +1,6 @@ { "name": "@ninjutsu-build/node", - "version": "0.12.0", + "version": "0.12.1", "description": "A ninjutsu-build plugin to generate ninja rules to execute JavaScript with node", "author": "Elliot Goodrich", "scripts": { diff --git a/packages/node/src/depfile.cts b/packages/node/src/depfile.cts index aee1503..a0e58df 100644 --- a/packages/node/src/depfile.cts +++ b/packages/node/src/depfile.cts @@ -1,23 +1,30 @@ import { openSync, writeFileSync } from "node:fs"; import { resolve, relative, isAbsolute } from "node:path"; -let fd: number; -let cwd: string; +declare global { + // biome-ignore lint/style/noVar: `let` require to type `global` in TypeScript + var fd: number; + // biome-ignore lint/style/noVar: `let` require to type `global` in TypeScript + var cwd: string | undefined; +} /** * Open the corresponding depfile for the specified `out` file. Note that this - * must be called before any calls to `addDependency`. + * must be called before any calls to `addDependency` in order for that function + * to have any effect. * @private */ export function open(out: string): void { - cwd = resolve(); - fd = openSync(out + ".depfile", "w"); - writeFileSync(fd, out + ":"); + global.cwd = resolve(); + global.fd = openSync(out + ".depfile", "w"); + writeFileSync(global.fd, out + ":"); } /** - * Add to ninja's dynamic dependencies the specified `path` for the currently - * running script. + * If the currently running script has been run inside `node` with the + * appropriate bootstrap scripts injected by `@ninjutsu-build/tsc`, then + * add the specified `path` to ninja's dynamic dependencies the this script. + * Otherwise do nothing. * * For example, if we want to execute a "script.mjs" file with * ninja we can write: @@ -66,11 +73,14 @@ export function open(out: string): void { * ``` */ export function addDependency(path: string): void { - const relPath = relative(cwd, path); - const dependency = ( - relPath && !relPath.startsWith("..") && !isAbsolute(relPath) - ? relPath - : path - ).replaceAll("\\", "/"); - writeFileSync(fd, " " + dependency); + const { cwd, fd } = global; + if (cwd !== undefined) { + const relPath = relative(cwd, path); + const dependency = ( + relPath && !relPath.startsWith("..") && !isAbsolute(relPath) + ? relPath + : path + ).replaceAll("\\", "/"); + writeFileSync(fd, " " + dependency); + } }