Skip to content

Commit

Permalink
Fix global variables in @ninjutsu-build/node/runtime
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elliotgoodrich committed Jul 7, 2024
1 parent d60313a commit d0af42d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
40 changes: 25 additions & 15 deletions packages/node/src/depfile.cts
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d0af42d

Please sign in to comment.