Skip to content

Commit 2d5e6e8

Browse files
authored
fix an infinite loop which happens when autometrics is initialized in 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
1 parent 06ff773 commit 2d5e6e8

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/autometrics/src/platform.node.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { AsyncLocalStorage } from "node:async_hooks";
66
import { readFileSync } from "node:fs";
7-
import { dirname, join } from "node:path";
7+
import { dirname, join, parse } from "node:path";
88

99
import { AUTOMETRICS_DEFAULT_SERVICE_NAME } from "./constants.ts";
1010
import { getGitRepositoryUrl, getPackageStringField } from "./platformUtils.ts";
@@ -112,6 +112,15 @@ export function getALSInstance() {
112112
}>();
113113
}
114114

115+
/**
116+
* Returns a boolean indicating whether a given path is the file-system root or not.
117+
*
118+
* @internal
119+
*/
120+
export function isRootPath(pathToCheck: string): boolean {
121+
return pathToCheck == parse(getCwd()).root;
122+
}
123+
115124
function detectPackageName(): string | undefined {
116125
try {
117126
const gitConfig = readClosest("package.json");
@@ -139,6 +148,10 @@ function readClosest(path: string): Uint8Array {
139148
try {
140149
return readFileSync(join(basePath, path));
141150
} catch {
151+
// Break once we've tried the file-system root
152+
if (isRootPath(basePath)) {
153+
break;
154+
}
142155
basePath = dirname(basePath);
143156
}
144157
}

0 commit comments

Comments
 (0)