Skip to content

Create projectname_shared project when generating projects #1256

Open

Description

@lukehutch

I have run into several cases where my client and server need to share some code, e.g. business logic, or data classes / enums that don't need to be passed over the wire but are needed for processing on both the server and the client.

I noticed that the way this is solved by Serverpod itself is by creating a ..._shared directory: packages/serverpod_shared and tests/serverpod_test_shared.

serverpod generate should create a shared project like this automatically (creating ..._shared alongside ..._server, ..._client, and ..._flutter), with instructions for users to put shared code into that project if necessary, automatically adding a dependency on this project from the server and flutter projects.

At a minimum the shared project should contain a configuration file that specifies the host and port of the server. This has to be specified in two places right now:

  1. it is specified in the client when calling the Client constructor;
  2. it has to also be specified in the server, e.g. in config/development.yaml, otherwise file URLs generated by the server will not be valid. (By the way, this is not documented anywhere that I saw, I had to figure out the hard way why uploaded images weren't originally loading from the server...)

The generated starter project should pull from this shared configuration file on both the client and the server.

Activity

  1. Isakdl commented on Aug 4, 2023

    @Isakdl
    Collaborator

    Yes! We had a discussion on this point yesterday and it would indeed be quite nice to have even though there is nothing stopping you from manually setting this up.

    For documentation purposes:
    We could also generate base classes in the shared package of our protocols, this way both the server and client classes could inherit from them and you got a single place where you can add extensions to the generate code (that should be shared).

  2. added
    enhancement
    on Aug 4, 2023
  3. lukehutch commented on Aug 4, 2023

    @lukehutch
    ContributorAuthor

    I'll be manually setting this up, yes, but I couldn't figure out how to do it before I noticed your _shared projects -- I have raised the issue of wanting to share code between client and server once or twice before in Serverpod issues or discussions. Anyway it wasn't intuitive to me to come up with this solution, so that's why I propose generating all projects that way by default. People can delete the _shared project if they don't need it. But at least for host/port config, as I pointed out this is in fact an important usecase for a shared project.

    Although that raises another issue -- when a client or server project is built, how does the compiler know to bundle a config file (e.g. a config yaml file) from a dependency with the build? I would assume only the code that is called in dependencies (after tree shaking) makes it into the final build, not every file that was shipped with every dependency.

  4. lukehutch commented on Sep 3, 2023

    @lukehutch
    ContributorAuthor

    OK, I tried manually setting up a shared project. The flutter and server projects both depend upon the shared project. However, the shared project needs to also depend on the client project, which is generated from the server, because some of the shared functions depend upon generated types (like enums, generated from protocol files on the server).

    Including the shared project on the server now gives errors like:

    The argument type 'FileType (where FileType is defined in /home/luke/Work/click/click_server/lib/src/generated/enum_file_type.dart)' can't be assigned to the parameter type 'FileType (where FileType is defined in /home/luke/Work/click/click_client/lib/src/protocol/enum_file_type.dart)'.dart[argument_type_not_assignable](https://dart.dev/diagnostics/argument_type_not_assignable)
    enum_file_type.dart(11, 6): FileType is defined in /home/luke/Work/click/click_server/lib/src/generated/enum_file_type.dart
    enum_file_type.dart(11, 6): FileType is defined in /home/luke/Work/click/click_client/lib/src/protocol/enum_file_type.dart
    

    So I'm back to the drawing board. The shared project cannot depend upon the client project, therefore it can't use any of the types that should be commonly accessible on both the server and the flutter frontend.

    Really what needs to happen is that there should be a way to mark classes on the server for copying into the client.

  5. vlidholt commented on Sep 13, 2023

    @vlidholt
    Collaborator

    @lukehutch, I think you can manage this using a symlink. E.g. place your shared extensions in a file (or directory) on the server. Use ln -s to create a symlink for the file or the directory in the client or in your flutter code. You may need to use a relative import to import any supporting files (e.g. the generated protocol and models).

    This way you will only need to maintain one file, but it will be present in two places.

  6. Rodsevich commented on Sep 13, 2023

    @Rodsevich
    Contributor

    @vlidholt I'm willing to contribute time and effort on a redesign and rework for this issue. We are currently designing a build_runner's builder for the topic and I'm realizing because of this that it wouldn't be as good as a 15/30 minutes talk and a roadmap of the changes needed. What do you think about this?

  7. lukehutch commented on Sep 13, 2023

    @lukehutch
    ContributorAuthor

    @lukehutch, I think you can manage this using a symlink. E.g. place your shared extensions in a file (or directory) on the server. Use ln -s to create a symlink for the file or the directory in the client or in your flutter code. You may need to use a relative import to import any supporting files (e.g. the generated protocol and models).

    This way you will only need to maintain one file, but it will be present in two places.

    That would work for pure non-generated shared code, but as the error message in my previous comment indicates, it won't work for any shared code that depends upon generated code (presumably because there are different server and client versions of generated classes).

  8. vlidholt commented on Sep 13, 2023

    @vlidholt
    Collaborator

    @lukehutch I'm pretty sure it would work if you make sure to import using the same relative import.

    E.g.

    Server side

    // my_exports.dart
    export '../generated/my_serializable.dart';
    // File with shared extensions (i.e. my_extension.dart)
    import 'my_exports.dart';
    
    extension MyExtension on MySerializable {
      // Added shared methods.
    }

    Client side

    Make a ln -s to my_extension.dart

    Add a second my_exports.dart which references the local generated files.

  9. lukehutch commented on Sep 14, 2023

    @lukehutch
    ContributorAuthor

    @vlidholt so you're saying the client and server version of generated classes are always exactly the same?

    I assumed they wouldn't be, because some fields of generated classes may be api-only, and some may be database-only.

  10. vlidholt commented on Oct 5, 2023

    @vlidholt
    Collaborator

    Sorry I'm a bit slow. They aren't exactly the same, but similar enough that you can extend them with the same code.

  11. lukehutch commented on Oct 5, 2023

    @lukehutch
    ContributorAuthor

    @vlidholt How about allowing for a common directory in the server project, which, if present, is sync'd into the client project whenever serverpod generate is called? (The client should probably have a README file added that says Do not modify the contents of this directory, it is synchronized from the server. Or maybe the client directory should just be a softlink to the server.)

  12. vlidholt commented on Oct 10, 2023

    @vlidholt
    Collaborator

    I think the proper solution is to correctly do the projectname_shared package, with generated abstract classes as a base for the other generated models.

  13. lukehutch commented on Feb 14, 2024

    @lukehutch
    ContributorAuthor

    I have tried to create a shared code project in a number of different ways. Nothing I do works. The fundamental problem is that there are separate server and client versions of all model classes, and shared code can import only one of these two. Actually the server versions are not even exported from the server project, so the only option in a separate project is to import the client version. But then if the server project adds a dependency to the shared project, it now transitively imports the client versions of the model classes -- and the server versions are not compatible with them.

    One stop-gap might be to make the client and server model classes share a common abstract superclass, although I don't think that will work with enums, since they are not extensible.

    The only solution I can see is to allow a user to mark a source file in the server project as something that should be copied across into the generated client project. However, when the file is copied, its imports also need to be updated so that they point to the client project's model classes, rather than the server project's model classes.

    This is pretty important -- I keep having to duplicate code between my server and flutter projects, and having to manually keep both versions in sync is a big risk for breakage.

  14. Frank1016 commented on Jul 24, 2024

    @Frank1016

    Any update on fixing this issue from the Serverpod team?

  15. iampopal commented on Nov 7, 2024

    @iampopal

    We are looking for this too...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions