pi-formatter 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,6 +20,9 @@ pi install npm:pi-formatter
20
20
  `pi-formatter` detects file types and runs the appropriate formatter as
21
21
  best-effort post-processing. Formatter failures never block tool results.
22
22
 
23
+ Formatting is limited to files inside the current working directory. Paths
24
+ outside the current directory and paths that traverse symlinks are ignored.
25
+
23
26
  Formatting modes:
24
27
 
25
28
  - `tool`: format immediately after each successful `write` or `edit` tool call.
@@ -203,6 +203,15 @@ function isTreefmtUnmatchedPathFailure(result: ExecResult): boolean {
203
203
  return /\bno formatter for path:/i.test(`${result.stderr}\n${result.stdout}`);
204
204
  }
205
205
 
206
+ function isTreefmtNixUnmatchedPathSuccess(result: ExecResult): boolean {
207
+ // Some treefmt-nix formatter wrappers exit successfully even when the target
208
+ // path does not match any formatter, but they still report that zero files
209
+ // were emitted for processing.
210
+ return /\bemitted\s+0\s+files?\s+for processing\b/i.test(
211
+ `${result.stderr}\n${result.stdout}`,
212
+ );
213
+ }
214
+
206
215
  function shouldFallbackFromTreefmtNixFailure(result: ExecResult): boolean {
207
216
  const output = `${result.stderr}\n${result.stdout}`;
208
217
  return (
@@ -306,7 +315,8 @@ async function resolveProjectFormatterCandidates(
306
315
  await resolveTreefmtCandidate(filePath, cwd),
307
316
  await resolveTreefmtNixCandidate(filePath, cwd),
308
317
  ].filter(
309
- (candidate): candidate is ProjectFormatterCandidate => candidate !== undefined,
318
+ (candidate): candidate is ProjectFormatterCandidate =>
319
+ candidate !== undefined,
310
320
  );
311
321
 
312
322
  return candidates.sort(compareProjectFormatterCandidates);
@@ -442,6 +452,10 @@ async function executeTreefmtNixCandidate(
442
452
  );
443
453
 
444
454
  if (result.code === 0) {
455
+ if (isTreefmtNixUnmatchedPathSuccess(result)) {
456
+ return "skipped";
457
+ }
458
+
445
459
  summaryReporter?.({
446
460
  runnerId: candidate.runnerId,
447
461
  status: "succeeded",
@@ -1,6 +1,6 @@
1
- import { access } from "node:fs/promises";
1
+ import { access, lstat } from "node:fs/promises";
2
2
  import { homedir } from "node:os";
3
- import { basename, isAbsolute, join, relative, resolve } from "node:path";
3
+ import { basename, isAbsolute, join, relative, resolve, sep } from "node:path";
4
4
  import type { FileKind } from "./types.js";
5
5
 
6
6
  const UNICODE_SPACES = /[\u00A0\u2000-\u200A\u202F\u205F\u3000]/g;
@@ -71,6 +71,43 @@ export function getRelativePathOrAbsolute(
71
71
  return relPath;
72
72
  }
73
73
 
74
+ export async function isPathInFormattingScope(
75
+ filePath: string,
76
+ cwd: string,
77
+ ): Promise<boolean> {
78
+ const rootPath = resolve(cwd);
79
+ const candidatePath = resolve(filePath);
80
+
81
+ if (!isWithinDirectory(candidatePath, rootPath)) {
82
+ return false;
83
+ }
84
+
85
+ const relPath = relative(rootPath, candidatePath);
86
+ if (!relPath || relPath === ".") {
87
+ return false;
88
+ }
89
+
90
+ let currentPath = rootPath;
91
+
92
+ for (const segment of relPath.split(sep)) {
93
+ if (segment.length === 0 || segment === ".") {
94
+ continue;
95
+ }
96
+
97
+ currentPath = join(currentPath, segment);
98
+
99
+ try {
100
+ if ((await lstat(currentPath)).isSymbolicLink()) {
101
+ return false;
102
+ }
103
+ } catch {
104
+ return false;
105
+ }
106
+ }
107
+
108
+ return true;
109
+ }
110
+
74
111
  export function detectFileKind(filePath: string): FileKind | undefined {
75
112
  if (
76
113
  /(\.(c|h|cc|hh|cpp|hpp|cxx|hxx|ixx|ipp|inl|tpp)|\.(c|h|cc|hh|cpp|hpp|cxx|hxx)\.in)$/i.test(
@@ -20,7 +20,7 @@ import {
20
20
  import { type FormatCallSummary, formatFile } from "./formatter/dispatch.js";
21
21
  import {
22
22
  getRelativePathOrAbsolute,
23
- pathExists,
23
+ isPathInFormattingScope,
24
24
  resolveToolPath,
25
25
  } from "./formatter/path.js";
26
26
 
@@ -154,7 +154,7 @@ export default function (pi: ExtensionAPI) {
154
154
  filePath: string,
155
155
  ctx: FormatterContext,
156
156
  ): Promise<string[]> => {
157
- if (!(await pathExists(filePath))) {
157
+ if (!(await isPathInFormattingScope(filePath, ctx.cwd))) {
158
158
  return [];
159
159
  }
160
160
 
@@ -252,7 +252,7 @@ export default function (pi: ExtensionAPI) {
252
252
  }
253
253
 
254
254
  const filePath = resolveEventPath(event.input.path, ctx.cwd);
255
- if (!filePath) {
255
+ if (!filePath || !(await isPathInFormattingScope(filePath, ctx.cwd))) {
256
256
  return;
257
257
  }
258
258
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-formatter",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Pi extension that auto-formats files.",
5
5
  "type": "module",
6
6
  "files": [