Skip to content

fix: getFileCategory must strip the directory before matching basename entries#588

Open
chuenchen309 wants to merge 1 commit into
nashsu:mainfrom
chuenchen309:fix/getfilecategory-strip-directory
Open

fix: getFileCategory must strip the directory before matching basename entries#588
chuenchen309 wants to merge 1 commit into
nashsu:mainfrom
chuenchen309:fix/getfilecategory-strip-directory

Conversation

@chuenchen309

Copy link
Copy Markdown
Contributor

Problem

getFileCategory extracts the extension with filePath.split(".").pop() on the full path:

export function getFileCategory(filePath: string): FileCategory {
  const ext = filePath.split(".").pop()?.toLowerCase() ?? ""
  return EXT_MAP[ext] ?? "unknown"
}

EXT_MAP has two extensionless basename entries — dockerfile: "code" and makefile: "code" — but a path-prefixed file with no . returns the whole path as the "extension", so they never match. Every caller passes a full path (preview.path, selectedFile, …):

getFileCategory("Dockerfile")            -> "code"     (bare name works)
getFileCategory("/project/Dockerfile")   -> "unknown"  (bug)
getFileCategory("/a/b/Makefile")         -> "unknown"  (bug)

unknown is in isBinary()'s list, so a Dockerfile/Makefile is treated as a non-previewable binary instead of previewable code.

Fix

Mirror the sibling getFileExtension() (which already strips the directory first), then fall back to the basename itself when it has no dot, so the two basename entries resolve.

Test

Added a case to file-types.test.ts covering bare and path-prefixed Dockerfile/Makefile; it fails before the fix and passes after. Existing tests unchanged.

Prepared with AI assistance and reviewed before submission.

…e entries

getFileCategory extracted the extension with filePath.split(".").pop() on the full
path, so the two extensionless EXT_MAP entries ("dockerfile", "makefile") never
resolved for real, path-prefixed inputs — every caller passes a full path. A
"/project/Dockerfile" fell through to "unknown", which isBinary() treats as a
non-previewable binary, so Dockerfiles/Makefiles were shown as binary instead of
code. Mirror getFileExtension's dir-strip, then fall back to the basename when it
has no dot.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FdqoGNoq6weGqKvPhm9QRd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant