Skip to content

Commit

Permalink
fix an infinite loop which happens when autometrics is initialized in…
Browse files Browse the repository at this point in the history
… a node service without a git repository (#149)

* fix an infinite loop which happens when autometrics is initialized in a node service without a git repository

* break readClosest after root path attempt

* fix type annotation, as well as path usage
  • Loading branch information
benjibuiltit authored Feb 26, 2024
1 parent 06ff773 commit 2d5e6e8
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/autometrics/src/platform.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { AsyncLocalStorage } from "node:async_hooks";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { dirname, join, parse } from "node:path";

import { AUTOMETRICS_DEFAULT_SERVICE_NAME } from "./constants.ts";
import { getGitRepositoryUrl, getPackageStringField } from "./platformUtils.ts";
Expand Down Expand Up @@ -112,6 +112,15 @@ export function getALSInstance() {
}>();
}

/**
* Returns a boolean indicating whether a given path is the file-system root or not.
*
* @internal
*/
export function isRootPath(pathToCheck: string): boolean {
return pathToCheck == parse(getCwd()).root;

Check failure on line 121 in packages/autometrics/src/platform.node.ts

View workflow job for this annotation

GitHub Actions / test

Use === instead of ==
}

function detectPackageName(): string | undefined {
try {
const gitConfig = readClosest("package.json");
Expand Down Expand Up @@ -139,6 +148,10 @@ function readClosest(path: string): Uint8Array {
try {
return readFileSync(join(basePath, path));
} catch {
// Break once we've tried the file-system root
if (isRootPath(basePath)) {
break;
}
basePath = dirname(basePath);
}
}
Expand Down

0 comments on commit 2d5e6e8

Please sign in to comment.