Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion .github/instructions/coding-guidelines.instructions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .rulesync/rules/coding-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ globs: ["**/*.ts"]
- The maintainer thinks that ballel files are harmful to tree-shaking and import path transparency.
- The default value of `baseDir` should be `process.cwd()` because it is easier to mock in tests compared to hardcoding `"."`. However, the default value of `relativeDirPath` should be `"."` because it should be relative path to concatenate with `baseDir`.
- When logging errors, you must use `formatError` function in `src/utils/error.ts` to format the error message.
- When writing any path, you must always use `join` function in `node:path` to join the path because it must support both Windows and Unix-like paths.
- When writing any filesystem path, you must always use `join` function in `node:path` to join the path because it must support both Windows and Unix-like paths.
- When writing non-filesystem paths (e.g., API paths, gitignore entries, generated file content), use `path.posix.join` or `.replace(/\\/g, "/")` to ensure forward slashes regardless of platform.
- When writing Rulesync file paths, you must condsider using constants in `src/constants/rulesync-paths.ts` to avoid hardcoding paths.
- You should always use `z.looseObject()` for zod schemas representing frontmatter keys. This is because various AI tools update very quickly and parameters are constantly being added.
10 changes: 5 additions & 5 deletions src/lib/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,10 @@ describe("fetchFiles", () => {
expect(summary.files[0]?.relativePath).toBe("rules/overview.md");
});

it("should send forward-slash paths to GitHub API even when path.join produces backslashes", async () => {
// This test verifies that paths sent to GitHub API always use forward slashes.
// On Windows, path.join("packages/shared", "rules") produces "packages\\shared\\rules",
// which would break the GitHub API call.
it.each([
["POSIX style input", "owner/repo:packages/shared"],
["Windows style input (backslashes in subdir)", "owner/repo:packages\\shared"],

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameterized test covers backslashes in the source path, but it doesn’t cover trailing separators (e.g. owner/repo:packages\\shared\\ / owner/repo:packages/shared/). Those inputs are plausible (especially from Windows shells) and currently interact badly with basePath.length + 1 relative-path slicing. Consider adding trailing-slash/backslash variants here so the normalization logic is exercised and guarded against regressions.

Suggested change
["Windows style input (backslashes in subdir)", "owner/repo:packages\\shared"],
["Windows style input (backslashes in subdir)", "owner/repo:packages\\shared"],
["POSIX style input with trailing slash", "owner/repo:packages/shared/"],
["Windows style input with trailing backslash", "owner/repo:packages\\shared\\"],

Copilot uses AI. Check for mistakes.
])("should send forward-slash paths to GitHub API (%s)", async (_label, source) => {
mockClientInstance.listDirectory.mockImplementation(
(owner: string, repo: string, path: string) => {
// Verify no backslashes in any path sent to GitHub API
Expand Down Expand Up @@ -693,7 +693,7 @@ describe("fetchFiles", () => {

const summary = await fetchFiles({
logger,
source: "owner/repo:packages/shared",
source,
options: { features: ["rules", "mcp"] },
baseDir: testDir,
});
Expand Down
4 changes: 3 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ export async function fetchFiles(params: FetchParams): Promise<FetchSummary> {

// Resolve options
const resolvedRef = options.ref ?? parsed.ref;
const resolvedPath = options.path ?? parsed.path ?? ".";
// Normalize backslashes to forward slashes for GitHub API compatibility.
// On Windows, paths may contain backslashes which are invalid in API requests.
const resolvedPath = (options.path ?? parsed.path ?? ".").replace(/\\/g, "/");

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolvedPath is normalized by replacing backslashes, but it can still end up with leading/trailing / (e.g. user passes packages\\shared\\ or packages/shared/). collectFeatureFiles() later derives relativePath via file.path.substring(basePath.length + 1), which assumes basePath has no trailing slash; a trailing / will drop the first character of the relative path (e.g. ules/overview.md). Consider additionally trimming leading/trailing slashes from resolvedPath (and/or computing the relative path using a safer prefix check / posix.relative) so basePath is in a consistent normalized form.

Suggested change
const resolvedPath = (options.path ?? parsed.path ?? ".").replace(/\\/g, "/");
let resolvedPath = (options.path ?? parsed.path ?? ".").replace(/\\/g, "/");
// Trim leading/trailing slashes so base paths are in a consistent form.
resolvedPath = resolvedPath.replace(/^\/+|\/+$/g, "");
if (resolvedPath === "") {
resolvedPath = ".";
}

Copilot uses AI. Check for mistakes.
const outputDir = options.output ?? RULESYNC_RELATIVE_DIR_PATH;
const conflictStrategy: ConflictStrategy = options.conflict ?? "overwrite";
const enabledFeatures = resolveFeatures(options.features);
Expand Down
4 changes: 4 additions & 0 deletions src/types/ai-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export abstract class AiDir {
}

getRelativePathFromCwd(): string {
// Use path.join + replace instead of path.posix.join because input segments
// (relativeDirPath, dirName) may already contain backslashes on Windows.
// path.posix.join does not normalize backslashes, so we must use path.join
// (which handles OS-native separators) and then normalize to POSIX format.

Copilot AI Mar 30, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new comment mentions (relativeDirPath, dirName) may already contain backslashes on Windows, but dirName is explicitly validated in the constructor to reject any / or \\. To keep the comment accurate (and avoid implying dirName might contain separators), it should refer only to relativeDirPath (or otherwise clarify which segment can contain backslashes).

Suggested change
// Use path.join + replace instead of path.posix.join because input segments
// (relativeDirPath, dirName) may already contain backslashes on Windows.
// path.posix.join does not normalize backslashes, so we must use path.join
// (which handles OS-native separators) and then normalize to POSIX format.
// Use path.join + replace instead of path.posix.join because relativeDirPath
// may already contain backslashes on Windows. dirName is validated to be a
// simple directory name without separators. path.posix.join does not normalize
// backslashes, so we must use path.join (which handles OS-native separators)
// and then normalize to POSIX format.

Copilot uses AI. Check for mistakes.
return path.join(this.relativeDirPath, this.dirName).replace(/\\/g, "/");
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/ai-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export abstract class AiFile {
}

getRelativePathFromCwd(): string {
// Use path.join + replace instead of path.posix.join because input segments
// (relativeDirPath, relativeFilePath) may already contain backslashes on Windows.
// path.posix.join does not normalize backslashes, so we must use path.join
// (which handles OS-native separators) and then normalize to POSIX format.
return path.join(this.relativeDirPath, this.relativeFilePath).replace(/\\/g, "/");
}

Expand Down
Loading