facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.36k stars 1.82k forks source link

Relay 13 compiler doesn't delete stale codegen #3864

Open arcticmatt opened 2 years ago

arcticmatt commented 2 years ago

For example, let's say I have Foo.tsx, which contains a GraphQL mutation called FooMutation. This will cause Relay compiler to generate a file called FooMutation.graphql.ts

However, if I delete FooMutation from Foo.tsx, and then re-run the Relay compiler, FooMutation.graphql.ts will still be there. I expected it to be deleted

ch1ffa commented 2 years ago

@arcticmatt it just ignores __generated__ by default. You can add your own exclude to the config without it.

theseyi commented 2 years ago

This seems problematic over time, relay-compiler-language-typescript which is now obsolete, since Relay 13, did this clean up automatically

cedrickring commented 1 year ago

Actually what @ch1ffa said works very well. If you change the relay config to something like

{
    ...
    "exclude": [
      "**/node_modules/**",
      "**/mocks/**"
    ]
}

the "unneeded" files are automatically deleted by the relay compiler which is pretty neat.

oxilor commented 1 year ago

Why the default behaviour in Relay is collecting stale generated files? I think it would be better if one of the things will happen:

  1. The **/__generated__/** directory will be removed from the exclude config, but it's not the best solution because in this case relay will try to find fragments there and waste time for it.
  2. Remove all stale generated files in these directories by default in spite of this directory is specified in the exclude config. I think it's much better. exclude only means "don't try to find fragments here and there", it shouldn't be about ignoring stale files that was generated by relay. It's strange behaviour.

I think it's important to remove all stale generated files by default, because types from there can be used by components (if the TypeScript is used). So if the stale generated files are not deleted, it means that some components may use already stale types that can lead to bugs.

Thus I think the stale generated files must be removed by relay by default in spite of this directory is specified in the exclude config.

kawazoe commented 3 months ago

Additionally, trying to remove all generated files prior to running the relay compiler causes many dev tools, like the WebPack dev server or WebStorm's indexation, to go completely out wack and think the files don't exist even if relay regenerates them.

This means that I have to choose between stale definitions, which means a risk of commiting invalid code, or a broken hot reload on a project where the dev server takes 3 minutes to start due to the size...

The only way to prevent this problem, is for the relay compiler to delete only stale files.