Skip to content
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

Make typegen generate idempotent #8521

Open
larixk opened this issue Feb 6, 2025 · 6 comments
Open

Make typegen generate idempotent #8521

larixk opened this issue Feb 6, 2025 · 6 comments

Comments

@larixk
Copy link

larixk commented Feb 6, 2025

Is your feature request related to a problem? Please describe.
When repeatedly running sanity typegen generate on the same codebase, the generated file differs between runs. Types are ordered seemingly randomly within the file, resulting in unnecessary and often large diffs in version control.

Describe the solution you'd like
Ensure the generated file uses a consistent, controlled order so that multiple runs produce identical output.

Describe alternatives you've considered
A post-processing step to reorder the file was considered, but it adds unnecessary complexity and could easily break on future updates.

Additional context
While keeping generated files in version control may not be ideal, our Next.js TypeScript build process requires these types to be available at build time, and generating them as part of the build process could introduce other issues.

@mtilda
Copy link

mtilda commented Feb 7, 2025

I am not able to reproduce the behavior you described. This command seems to me to be idempotent. Can you provide a minimal reproduceable example?

The order of the types in my sanity.config.ts file seems to match the order in my schema.json, so the question is "how does sanity schema extract order its output?"

I tried changing the order of the imports in sanity.config.ts and the order of the types array in that file, but these changes did not alter the order of schema.json nor sanity.config.ts.

import { defineConfig } from 'sanity';

// Changing the order of these imports has no effect.
import a from 'sanity/schemas/a';
import b from 'sanity/schemas/b';
import c from 'sanity/schemas/c';

export default defineConfig({
    ...
    schema: {
        types: [
            // Changing the order of these items has no effect.
            a,
            b,
            c,
        ],
    },
})

@mtilda
Copy link

mtilda commented Feb 7, 2025

While keeping generated files in version control may not be ideal, our Next.js TypeScript build process requires these types to be available at build time, and generating them as part of the build process could introduce other issues.

For the record, I think it is perfectly acceptable to commit these files to version control. I do this. Without the types file, my code would seem to have broken imports and would be impossible to read without generating that file locally.

@mtilda
Copy link

mtilda commented Feb 7, 2025

I tried changing the order of the imports in sanity.config.ts and the order of the types array in that file, but these changes did not alter the order of schema.json nor sanity.config.ts.

I was wrong. Changing the order of the types array does appear to change the order of the schema.json file and sanity.types.ts file.

import { defineConfig } from 'sanity';

// Changing the order of these imports has no effect.
import a from 'sanity/schemas/a';
import b from 'sanity/schemas/b';
import c from 'sanity/schemas/c';

export default defineConfig({
    ...
    schema: {
        types: [
            // Changing the order of these items effects the order of the schema.json file.
            a,
            b,
            c,
        ],
    },
})

@larixk
Copy link
Author

larixk commented Feb 10, 2025

Thanks for trying to reproduce.

Creating a minimal reproducable example might be tricky. I have a feeling that perhaps this is caused by having a relatively large codebase and our queries spread out in many different files (keeping them close to the components that use the data):

✅ Generated TypeScript types for 32 schema types and 21 GROQ queries in 17 files into: ./sanity.types.ts

To be clear the problem is that even when not changing any code at all, but literally running the typegen command repeatedly in a row, the resulting file may be different every run.

An assumption of mine is that this might be based on the way workers are used in processing the source files in the typegen script. Small changes in the speed of the file system reading the files might then be the determining factor for how the types are ordered in the resulting file.

@larixk
Copy link
Author

larixk commented Feb 12, 2025

Thanks to @sgulseth , A potential fix was merged as #8610.

I will check after the next sanity release if the issue has indeed been resolved.

@ChrisNolan
Copy link

Small world, I had also started a thread on the slack @ https://sanity-io-land.slack.com/archives/CT03N628J/p1739121346906409 about the same issue.

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 a pull request may close this issue.

3 participants