-
-
Notifications
You must be signed in to change notification settings - Fork 141
refactor(copilot-rule): reuse toPosixPath and switch sameRelativePath to object params #1644
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
Merged
dyoshikawa
merged 2 commits into
dyoshikawa:main
from
Chen17-sq:refactor/1603-reuse-toposixpath-in-copilot-rule
May 20, 2026
+78
−16
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ import { z } from "zod/mini"; | |
| import { RULESYNC_RULES_RELATIVE_DIR_PATH } from "../../constants/rulesync-paths.js"; | ||
| import { ValidationResult } from "../../types/ai-file.js"; | ||
| import { formatError } from "../../utils/error.js"; | ||
| import { readFileContent } from "../../utils/file.js"; | ||
| import { readFileContent, toPosixPath } from "../../utils/file.js"; | ||
| import { parseFrontmatter, stringifyFrontmatter } from "../../utils/frontmatter.js"; | ||
| import { RulesyncRule, RulesyncRuleFrontmatter } from "./rulesync-rule.js"; | ||
| import { | ||
|
|
@@ -48,12 +48,22 @@ export type CopilotRuleSettablePathsGlobal = ToolRuleSettablePathsGlobal & { | |
| }; | ||
| }; | ||
|
|
||
| const normalizeRelativePath = (path: string): string => | ||
| path.replace(/\\/g, "/").replace(/\/+/g, "/"); | ||
| // toPosixPath converts backslashes to forward slashes so paths compare | ||
| // equally on Windows and POSIX. The extra slash collapse covers a | ||
| // mixed-separator edge case where `node:path.join` on POSIX treats a | ||
| // trailing backslash as a literal character: joining `.github\\` (one | ||
| // literal backslash) with `copilot-instructions.md` produces | ||
| // `.github\\/copilot-instructions.md`, which becomes | ||
| // `.github//copilot-instructions.md` after `toPosixPath`. The slash | ||
| // collapse below normalizes that back to a single `/` so the result | ||
| // compares equal to the canonical `.github/copilot-instructions.md`. | ||
| const normalizeRelativePath = (p: string): string => toPosixPath(p).replace(/\/+/g, "/"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
|
|
||
| const sameRelativePath = (leftDir: string, leftFile: string, rightDir: string, rightFile: string) => | ||
| normalizeRelativePath(join(leftDir, leftFile)) === | ||
| normalizeRelativePath(join(rightDir, rightFile)); | ||
| type RelativePathParts = { dir: string; file: string }; | ||
|
|
||
| const sameRelativePath = (left: RelativePathParts, right: RelativePathParts): boolean => | ||
| normalizeRelativePath(join(left.dir, left.file)) === | ||
| normalizeRelativePath(join(right.dir, right.file)); | ||
|
|
||
| /** | ||
| * Rule generator for GitHub Copilot | ||
|
|
@@ -217,10 +227,8 @@ export class CopilotRule extends ToolRule { | |
| const paths = this.getSettablePaths({ global }); | ||
| const isRoot = relativeDirPath | ||
| ? sameRelativePath( | ||
| relativeDirPath, | ||
| relativeFilePath, | ||
| paths.root.relativeDirPath, | ||
| paths.root.relativeFilePath, | ||
| { dir: relativeDirPath, file: relativeFilePath }, | ||
| { dir: paths.root.relativeDirPath, file: paths.root.relativeFilePath }, | ||
| ) | ||
| : relativeFilePath === paths.root.relativeFilePath; | ||
| const resolvedRelativeDirPath = | ||
|
|
@@ -282,10 +290,8 @@ export class CopilotRule extends ToolRule { | |
| }: ToolRuleForDeletionParams): CopilotRule { | ||
| const paths = this.getSettablePaths({ global }); | ||
| const isRoot = sameRelativePath( | ||
| relativeDirPath, | ||
| relativeFilePath, | ||
| paths.root.relativeDirPath, | ||
| paths.root.relativeFilePath, | ||
| { dir: relativeDirPath, file: relativeFilePath }, | ||
| { dir: paths.root.relativeDirPath, file: paths.root.relativeFilePath }, | ||
| ); | ||
|
|
||
| return new CopilotRule({ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice that
forDeletionnow has coverage for the backslash path. A symmetric test forfromFilewithrelativeDirPath: ".github\\instructions"would close the other half of the issue's coverage ask, sincefromFilealso routes throughsameRelativePath. Not blocking — the helper is shared — but adding it would make the safety net explicit at both call sites.