Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

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