Coder Social home page Coder Social logo

Comments (3)

bwilkerson avatar bwilkerson commented on August 28, 2024 1

Is the original library (the one containing the declaration before it's moved) being imported by the newly created library (that is, is utils/application_support_directory_provider.dart the original library)? I'm guessing the answer is 'no' because otherwise you probably wouldn't have added the part directive by hand, but wanted to ask rather than make an assumption.

If not, then I think that adding an import for the original library is the real solution.

It's absolutely true that the newly created library needs to import the original library if the code being moved references something from the original library that isn't also being moved. The bug is probably that we're not considering declarations in the original library that come from part files when deciding whether to add an import of the original library.

But I think that moving the part file is probably the wrong solution. It would be good if we could recognize that the @riverpod annotation is used by a code generator so that we could add the part directive in the new file, but we don't currently have enough information to be able to do that. I suspect that the best workflow we can support for this kind of situation is:

  1. Select the declaration to be moved and invoke the 'move to file' refactoring.
  2. Run the code generator to create the new generated file and to update the old generated file (unless it's no longer needed).
  3. Add the part directive for the new generated file.
  4. Remove the import of the original library if it's no longer needed.
  5. Remove the old generated file and it's associated part directive if the file is no longer needed.

I'd love to automate more of this process but I don't think we have enough information to be able to do so at this point.

Also, we should evaluate whether macros will solve this problem before putting too much effort into a solution.

from sdk.

bwilkerson avatar bwilkerson commented on August 28, 2024 1

I agree that this is a bad experience, and I'd love to make it better. Unfortunately, our tools have no way of knowing the behavior of any given code generator, or even whether a code generator is being used to generate a file, so it isn't clear what we can do in this situation.

from sdk.

github-worst-company avatar github-worst-company commented on August 28, 2024

Apologies, I had a slight oversight. What happened is that the build runner wasn't running on watch, so moving the file did when work build runner is not running, at the expense that 2 files were left as is, the generated file and the original file which now only contains imports(presumably for the generated file) and the "part file.g.dart"

So basically if you move a generated function to a new file, the .g file will stay there, and the original file will also stay there and only contain imports and the part.

Here's a function in a standalone file called test_function.dart before moving.

import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'test_function.g.dart';

@riverpod
String testFunction(TestFunctionRef ref) => 'A String';

The code when moving the function into a file called test_function_two.dart

// this file is test_function_two.dart
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:salam_app/utils/test_function.dart'; // 

// Function doesn't give error because it imports the generated file
@riverpod
String testFunction(TestFunctionRef ref) => 'A String';

Then only these lines are left in the original file of test_function.dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:the_app/test_function_two.dart'; // Not sure why it imports the file after moving the function

part 'test_function.g.dart';

However when build runner is running on watch

Then test_function.dartfile gets messed up as there's no annotation but there's part and it deletes the generated file, so function that was moved to test_function_two.dart doesn't work anymore unless we add the part.

from sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.