File Index Tracking
The proxy generator uses inline metadata comments in generated files to enable intelligent cleanup of stale files when commands or queries are removed from your codebase.
Overview
Section titled “Overview”When you rename or delete a command or query from your backend, the corresponding TypeScript proxy file would normally be left behind. File tracking solves this by embedding metadata in each generated file and automatically removing orphaned files that are no longer needed.
How it Works
Section titled “How it Works”-
Metadata Comments: Each generated file includes a first-line comment with metadata:
// @generated by Cratis. Source: MyApp.Commands.CreateOrder. Time: 2024-12-08T12:00:00.0000000Z. Hash: 1E9523083D8C6457F194B1CB045877E19BE204FB42F9F6E5574B77FC79B3ABDA -
Orphan Detection: When running proxy generation, the generator scans all
.tsfiles in the output directory:- Files with the
@generated by Cratismarker that weren’t just generated are identified as orphaned - These orphaned files represent renamed or deleted types and are automatically removed
- Files with the
-
Index.ts Management: The generator intelligently maintains
index.tsfiles:- Only includes exports for files that were actually generated
- Preserves manual edits (comments, custom imports) in existing
index.tsfiles - Automatically removes exports for deleted files
- Deletes the
index.tsfile if the directory becomes empty
-
Legacy Support: The
.cratis/GeneratedFileIndex.jsonfile is still maintained for backwards compatibility but is superseded by the metadata-based approach.
Configuration
Section titled “Configuration”Disabling File Tracking
Section titled “Disabling File Tracking”File tracking is enabled by default. To disable the metadata-based orphan detection, add the following to your .csproj:
<PropertyGroup> <CratisProxiesSkipFileIndexTracking>true</CratisProxiesSkipFileIndexTracking></PropertyGroup>Note: This also disables the legacy .cratis/GeneratedFileIndex.json tracking.
Disabling Index Generation
Section titled “Disabling Index Generation”If you prefer to manage index.ts files manually, you can disable automatic index generation:
<PropertyGroup> <CratisProxiesSkipIndexGeneration>true</CratisProxiesSkipIndexGeneration></PropertyGroup>When index generation is disabled:
- No
index.tsfiles are created or updated - Existing
index.tsfiles are left untouched - You have full control over module exports
This is useful when:
- You have custom export logic in your
index.tsfiles - You want to selectively export certain files
- You’re integrating with existing manual code organization
CLI Usage
Section titled “CLI Usage”When using the proxy generator CLI directly, you can use these flags:
# Disable metadata-based file trackingproxygenerator assembly.dll output-path --skip-file-index-tracking
# Disable index.ts generationproxygenerator assembly.dll output-path --skip-index-generation
# Combine both optionsproxygenerator assembly.dll output-path --skip-file-index-tracking --skip-index-generationYou can also specify a custom project directory for the legacy .cratis folder:
proxygenerator assembly.dll output-path --project-directory=/path/to/projectVersion Control
Section titled “Version Control”Generated Files
Section titled “Generated Files”The generated TypeScript files should be committed to version control:
# Do NOT ignore generated proxies - they're part of your frontend codebase# queries/Each file is self-documenting with its metadata comment, making it easy to understand:
- What generated the file
- When it was last generated
- The source C# type
Legacy Index File
Section titled “Legacy Index File”The .cratis folder contains the legacy GeneratedFileIndex.json file. You can safely add it to .gitignore:
.cratis/This is recommended because:
- The metadata-based tracking approach is now the primary mechanism
- The index is regenerated on each build
- Different developers may have different build outputs
- CI/CD pipelines will generate fresh indexes
Example Scenario
Section titled “Example Scenario”Consider the following scenario:
-
Initial State: You have
CreateOrder.tsandUpdateOrder.tsin your commands folder, both with metadata comments. -
Change: You rename
UpdateOrdertoModifyOrderin your C# code. -
Build: The proxy generator runs and:
- Generates
CreateOrder.tswith updated timestamp (unchanged logic) - Generates
ModifyOrder.ts(new file with metadata) - Detects
UpdateOrder.tshas the@generatedmarker but wasn’t just generated - Identifies it as orphaned and deletes it
- Updates
index.tsto export onlyCreateOrderandModifyOrder
- Generates
-
Result: Your frontend has clean, up-to-date proxies without stale files.
Index.ts File Management
Section titled “Index.ts File Management”The generator intelligently manages index.ts files to balance automation with flexibility:
Generated Files Only
Section titled “Generated Files Only”The index.ts file only includes exports for files that were generated by the proxy generator:
// Manual comment or import you addedimport { customHelper } from './helpers';
export * from './CreateOrder';export * from './UpdateOrder';If you manually create a file like CustomOrder.ts in the same directory, it won’t be automatically added to index.ts. This prevents the generator from managing files it didn’t create.
Preserving Manual Edits
Section titled “Preserving Manual Edits”Any non-export lines (comments, imports, etc.) in your index.ts are preserved:
// This comment will be kept across regenerationsimport { something } from 'somewhere';
// Exports are managed automaticallyexport * from './CreateOrder';export * from './UpdateOrder';Disabling Index Generation
Section titled “Disabling Index Generation”If you want complete control over index.ts files, disable automatic generation:
<PropertyGroup> <CratisProxiesSkipIndexGeneration>true</CratisProxiesSkipIndexGeneration></PropertyGroup>Behavior When Disabled
Section titled “Behavior When Disabled”When file tracking is disabled (CratisProxiesSkipFileIndexTracking=true):
- No metadata-based orphan detection occurs
- Stale files are not automatically cleaned up
- The legacy
.cratisfolder and index file are not created - The
CratisProxiesSkipOutputDeletionoption becomes more important for managing old files - You may need to manually delete old proxy files when renaming or removing backend code
When index generation is disabled (CratisProxiesSkipIndexGeneration=true):
- No
index.tsfiles are created or modified - Existing
index.tsfiles are left completely untouched - You have full manual control over module exports
Relationship with Output Deletion
Section titled “Relationship with Output Deletion”Important: By default, the proxy generator deletes the entire output directory on every build (CratisProxiesSkipOutputDeletion=false), which means:
- File index tracking is not strictly necessary when using default settings
- All proxies are regenerated from scratch on every build
- No orphaned files can exist because the directory is wiped clean
File index tracking becomes essential when you enable incremental generation:
<PropertyGroup> <!-- Enable incremental generation --> <CratisProxiesSkipOutputDeletion>true</CratisProxiesSkipOutputDeletion> <!-- File tracking is now critical for cleanup --> <CratisProxiesSkipFileIndexTracking>false</CratisProxiesSkipFileIndexTracking></PropertyGroup>With incremental generation enabled, file tracking automatically removes orphaned files when you rename or delete commands/queries, keeping your output directory clean without requiring a full deletion on every build.
Recommendation: Use CratisProxiesSkipOutputDeletion=true with file tracking enabled (default) when proxies are intertwined with your feature code. Use the default full deletion when proxies are in a dedicated folder. See Configuration - Output Deletion Behavior for more details.
Troubleshooting
Section titled “Troubleshooting”Files Not Being Cleaned Up
Section titled “Files Not Being Cleaned Up”-
Check if tracking is enabled: Ensure
CratisProxiesSkipFileIndexTrackingis not set totrue. -
Verify metadata comments: Check that generated files have the
// @generated by Cratiscomment on the first line. -
Clean and rebuild: Delete the output folder and rebuild to regenerate all files with metadata.
Index.ts Not Updating
Section titled “Index.ts Not Updating”-
Check if index generation is enabled: Ensure
CratisProxiesSkipIndexGenerationis not set totrue. -
Verify generated files: Only files generated by the proxy generator are included in
index.ts. -
Check for manual files: Files you created manually won’t be added to
index.ts.
Legacy Index File Issues
Section titled “Legacy Index File Issues”If the legacy .cratis/GeneratedFileIndex.json file causes issues:
- Delete the
.cratisfolder - Rebuild your project
- The metadata-based tracking will handle cleanup automatically