Build: [build_runner] execute builder on only a single file

Created on 4 Dec 2020  路  8Comments  路  Source: dart-lang/build

I don't know if this is already possible but I could not find it in the documentation.

We have quite a large codebase making the builder take a very long time to run. Often we only changed a single file and would like to have a way to have the builder only recreate that single file. Is this already possible and if not is it feasible to implement for you guys or is there already a specific reason why this is not possible.

Thank you in advance and cheers :D


For general questions consider using Stack Overflow instead:
https://stackoverflow.com/questions/tagged/dart

Also consider our Gitter channel for light-weight/quick discussions:
https://gitter.im/dart-lang/build


All 8 comments

See https://github.com/dart-lang/build/issues/2815, and comments about the --build-filter argument.

One thing I just remembered which is not captured there is I believe that build to source generators always do run regardless of that argument. Not for a technical reason but because those files ship with packages on pub and for safety we wanted to ensure they were always up to date. We could revisit that decision, or possibly consider an override flag for that behavior, etc.

As a longer term solution here specifying which files the builder needs to run on is almost certainly going to give you the biggest bang for your buck, see my comment here https://github.com/dart-lang/build/issues/2861#issuecomment-712354986.

@jakemac53 Thanks for the response. I didn't know about --build-filter but unfortunately it does not work. I pointed it to a file and it completed with zero actions taken aka it did not generate the file :S

The longest it took for us to run the build runner was almost 16 minutes. :S and that for adding a new field to an enum :S It would be really amazing if we could simply run the build_runner for only a single file :/ Hope that gets added soon :D

@lunaticcoding if you are seeing build times that long, then configuring your build to only run on the more specific files that you need it to is definitely worth while. For small/medium sized projects this is less important, but it becomes important for large apps. If you want help with that let me know.

With regards to building single files note that it will still have to build any transitively imported file of the file you want to build, so depending on the structure of your app this may or may not help that much. As a common example, if your package libraries all import a convenience library which exports everything else from the package, then that means it also imports all other generated files in your package and thus they will all be generated when building any of them.

@jakemac53 oh I see. okay, that makes sense (still would be awesome if we could say we don't care about the imports - but I see as to how this is a bigger change). How would we go about limiting the scope? I would really appreciate the help :D

You can create a build.yaml file, and configure the generate_for setting for each builder. Ultimately it looks something like this:

targets:
  $default:
    builders:
      # Typically the builder key is just the package name, run `pub run build_runner doctor` to check your config.
      <builder-key>:
        generate_for:
          # Example glob for only the Dart files under `lib/models`
          - lib/models/*.dart

The builder key is the identifier for the builder you are configuring, this is of the format <package-name>:<builder-name>, or just <package-name> if the name is the same as the package. You can run pub run build_runner doctor to confirm you set the key right.

@jakemac53 Thank you :D this is already a huge improvement

Glad to hear that helped! I am going to close this issue for now, as we do support building specific outputs (which includes all their inputs...). If you have a case where that feature isn't working as intended please do file an issue about that.

@jakemac53 one more question :) Is there a way to add a single line at the beginning of every file created by a certain builder?
Like Remi is describing it here?

https://github.com/rrousselGit/freezed/pull/333

Was this page helpful?
0 / 5 - 0 ratings